Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Alternatives to `EXPLAIN ANALYZE` for queries that won't complete

Post

Alternatives to `EXPLAIN ANALYZE` for queries that won't complete

+11
−0

I have a large and complex PostgreSQL SELECT query that I would like to make faster. EXPLAIN suggests it should run quickly, with the worst parts being scans of a few thousand rows. When run, it does not complete in any reasonable amount of time (if statement_timeout is set to infinite, it eventually still gives up, complaining about having exceeded temporary file size limits, suggesting something is loading way more data than expected).

Usually, this would suggest to me that EXPLAIN's estimates are horribly inaccurate in some way, and I would try EXPLAIN ANALYZE to see what's really happening. But since this particular query is so bad I can't run it at all, I also can't run it with EXPLAIN ANALYZE.

What other tools are at my disposal for this sort of situation? Can I ask PostgreSQL for some sort of partial or time-limited EXPLAIN ANALYZE, as in "run this for five minutes, then stop and tell me what you spent those five minutes doing"? If I start commenting out bits of the query until it goes fast again, can I rely on the results being accurate, or does PostgreSQL's optimizer work more globally than that?

(Query itself omitted because I've run into this situation a few times, and would like general strategies rather than an answer for this specific query.)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Would running the query on a new table -- same DDL but with, say, 10 rows copied into it from the rea... (4 comments)
Would running the query on a new table -- same DDL but with, say, 10 rows copied into it from the rea...
Monica Cellio‭ wrote about 1 year ago

Would running the query on a new table -- same DDL but with, say, 10 rows copied into it from the real table -- complete? Would analyzing that tell you anything? (The answer probably depends on what that query is doing; if it's some complicated aggregation that only works at all if you've got thousands of rows, this approach probably doesn't help.)

Emily‭ wrote about 1 year ago · edited about 1 year ago

Probably wouldn't have helped in this case, depending on whether I got lucky and copied the right rows. The answer for this particular query (which I eventually found via extensive use of commenting things out and staring really hard) wound up being a series of joins that looked (to both me and EXPLAIN) like they should have matched one right row per left row, but matched a few thousand rows each and combinatorically exploded.

Monica Cellio‭ wrote about 1 year ago

Yikes. Would it be worth sharing what you learned in an answer? Or is it really specific to that particular query?

Emily‭ wrote about 1 year ago

Hrm. I definitely learned some things, but I think I phrased this specific question far too generally for them to make sense as answers to it. I may sit down at some point this week and distill them into a few separate self-answered questions.