Live data from Hacker News

PostgreSQL's Explain Analyze Made Readable

explain.depesz.com

11–20 of 62 posts

Re: PostgreSQL's Explain Analyze Made Readable

#11

In the "possibly unknown Postgres tools" category, I'm also a huge fan of PG Hero [0]. Gives you a nice sortable UI to look at running queries, see what the longest ones are, the most frequently called, etc. This is all just reading from pg_stats and such but the UI is very nice. [0] https://github.com/ankane/pghero

Hah, I just discovered this one independently after submitting this URL!

Re: PostgreSQL's Explain Analyze Made Readable

#12
I am a bit frustrated with one question regarding explain cost. Namely how to map the cost to wall time. Based on my research so far, andwer is about "you can't". But that is difficult for me to understand. I mean, I do understand that it does not map with high (or even relatively low) accuracy. But the cases where I have been needing the information are more like me wondering after 15 minutes of runtime whether the query is just heavy and takes more time than expected (up to even hours) or did I mess up something and the query in its current format is going to run approximately until the heat death of the universe and I better rewrite it. It just feels weird if I can't get even that accuracy out of the cost number.

Re: PostgreSQL's Explain Analyze Made Readable

#14

I am a bit frustrated with one question regarding explain cost. Namely how to map the cost to wall time. Based on my research so far, andwer is about "you can't". But that is difficult for me to understand. I mean, I do understand that it does not map with high (or even relatively low) accuracy. But the cases where I have been needing the information are more like me wondering after 15 minutes of runtime whether the…

So here is a little bit of explanation (which it sounds like you may already know). The general reason is that the cost reflects a lot of factors, and, if number of rows + available resources (CPU, memory, IO) + locks are constant, for the same logical result set, the cost does map to wall time. For example, given those constants I mentioned, if the execution time for a cost of 100 is 10 minutes, then you tweak the query (getting the same logical results) and the new cost is 50, the new execution time would be ~5 minutes (I think).

But most of those things I mentioned aren't constant. Available resources/system load changes, number of rows in tables change, and # of locks and length of holding on rows/tables change. So what you're left with is a cost that reflects the relative performance of a query compared to the same result set obtained by a different query.

For reference, in Oracle, this SO link [1] explains how cost is calculated.

Note: this is all my understanding, and someone more knowledgeable might know better.

[1] https://stackoverflow.com/a/8177490

Re: PostgreSQL's Explain Analyze Made Readable

#15

I am a bit frustrated with one question regarding explain cost. Namely how to map the cost to wall time. Based on my research so far, andwer is about "you can't". But that is difficult for me to understand. I mean, I do understand that it does not map with high (or even relatively low) accuracy. But the cases where I have been needing the information are more like me wondering after 15 minutes of runtime whether the…

The costs are there to choose a plan. Relative ordering (individually and in aggregate) is all that's really needed for this, rather than some function of time.

Actual run time is heavily dependent on distribution of data. There may be characteristics of your data not modelled in statistics used to calculate costs.

If I were running a query that might take 15 minutes, I'd break it down, and put limit clauses in pinch points to figure out which bits of the query are sensitive (e.g. how much run time changes with increments to limits).

But often just looking at the plan will tell me if the database is doing joins in the order I think would be more or less efficient, given the extra context I have on the data distribution.

Re: PostgreSQL's Explain Analyze Made Readable

#16

In the "possibly unknown Postgres tools" category, I'm also a huge fan of PG Hero [0]. Gives you a nice sortable UI to look at running queries, see what the longest ones are, the most frequently called, etc. This is all just reading from pg_stats and such but the UI is very nice. [0] https://github.com/ankane/pghero

On Heroku there is pg-extras: https://github.com/heroku/heroku-pg-extras

Re: PostgreSQL's Explain Analyze Made Readable

#18

pgAdmin has a really good EXPLAIN visualisation tool built-in which makes it into a neat little diagram

pgAdmin by itself is a _really_ horrible program to use otherwise though.

I do wish that it was as polished as the database core itself seems to be.

Re: PostgreSQL's Explain Analyze Made Readable

#19
post #18

pgAdmin has a really good EXPLAIN visualisation tool built-in which makes it into a neat little diagram

pgAdmin by itself is a _really_ horrible program to use otherwise though. I do wish that it was as polished as the database core itself seems to be.

pgAdmin 3 feels much more polished than version 4 :/
Post reply on HN