Live data from Hacker News

PostgreSQL's Explain Analyze Made Readable

explain.depesz.com

21–30 of 62 posts

Re: PostgreSQL's Explain Analyze Made Readable

#23
I've used this in the past. What I find that it's missing is the _why_, not the _what_. It's not that my query is performing a full table scan that I care about. It's why it's not using one of the six indexes that I want to know.

I would pay good money for an extension that could answer how the query planner makes decisions about a given query.

Re: PostgreSQL's Explain Analyze Made Readable

#24

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…

> Namely how to map the cost to wall time. Based on my research so far, andwer is about "you can't"

That is pretty much it. For other database engines' query planners too.

Everything that goes into the cost estimate is at best an educated guess, so the result is never going to map neatly to wall-clock time or any other measure.

Remember: the query planner's job is not to find the best plan possible for a given statement. This would be an impossible task. The planner's job is to try find a plan that is good enough and do so fast as possible.

To cut a short post long:

The cost is an educated guess, based on index statistics where available. Assuming it is similar to the cost calculations SQL Server uses is a mix of expected CPU use, I/O bandwidth use, memory required for the operation to touch disk as little possible, how large a load of locks the process might need to take (depending on the current isolation level settings), and so forth. Because you might have faster/slower CPU cores, more/less of them, faster/slower RAM, more/less cache per core, a lower/higher latency network, a set of drives that are faster/slower for random/bulk IO, etc., than the reference systems this mix was based upon, the cost is not going to exactly map to anything on your machine even if it does on some reference machine(s) somewhere in the development/testing chain.

Those reference machines might not even really exist. In SQL Server's case they are probably machines that sat under some developer's desks in the late 90s, so the balance might be significantly off for most modern use cases. This is part of why MS are playing around with cardinality estimators and other related bits in recent versions: the guesses they used to make don't make as much sense on modern systems, so they are having to be updated to make them more relevant. In some cases the estimates (those applied to table variables for instance) are still a stab in the dark, but they are now a stab that is more likely to be close to correct on modern kit than the stabs it used to take.

This is why index hints are needed. Because of the many variables involved, all the query planner can hope to do to assess each plan against the other possibilities, without a more exhaustive analysis which for many queries might take longer than running the query on the worst plan ever would, is a best guess. If you asking it to do something complex, sometimes you have to guide it towards the better plan.

Re: PostgreSQL's Explain Analyze Made Readable

#25
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.

Yep... the one very good thing that I got out of pgAdmin was that it finally convinced me to get psql under my fingers so I wouldn't have to use pgAdmin.

Re: PostgreSQL's Explain Analyze Made Readable

#26
post #18

Earlier quoted context omitted.

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.

Yep... the one very good thing that I got out of pgAdmin was that it finally convinced me to get psql under my fingers so I wouldn't have to use pgAdmin.

If you still would like a little bit of GUI take a look at https://dbeaver.io/

Re: PostgreSQL's Explain Analyze Made Readable

#27
My favorite tool for this kind of analysis was pgAdmin3, which had a very nice diagramming output for EXPLAIN ANALYZE which you can see here [0]. Hovering over the various images in the diagram would display their time, rows, and which statement caused them.

There is also this excellent post which shows how to break down an EXPLAIN and reason about the performance [1].

[0] http://www.postgresonline.com/images/journal/explain_plan_5....

[1] http://www.postgresonline.com/journal/archives/27-Reading-Pg...

Re: PostgreSQL's Explain Analyze Made Readable

#28
I use Depesz's Explain tool regularly for discussing queries with distributed teams in Slack. It's an indispensable tool.

By the way, if you think this is cool, Hubert (creator of depesz) is a DBA at Instructure, making software for schools (Canvas) and employee training (Bridge). We open-source a ton of stuff (code.instructure.com).

We're hiring leads + senior engineers in Chicago, Salt Lake City, Seattle, and Budapest! If you're interested, feel free to reach out to neil+hn@instructure.com or check out what's available at https://jobs.lever.co/instructure?lever-via=NiHimSaI8r&team=...

Re: PostgreSQL's Explain Analyze Made Readable

#29

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…

Isn’t this what EXPLAIN ANALYZE is for though?

Re: PostgreSQL's Explain Analyze Made Readable

#30
+1, this tool is great.

Someone made a flame graph analyzer for Oracle queries, which seems like an even better way of visualizing explains, and seems like it would be fairly straightforward to translate to postgres: https://externaltable.blogspot.com/2014/05/flame-graphs-for-...

Post reply on HN