Re: Query run in 27s with 15.2 vs 37ms with 14.6
Stephen Frost <sfrost@snowman.net>
From: Stephen Frost <sfrost@snowman.net>
To: Charles <peacech@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, pgsql-bugs@lists.postgresql.org
Date: 2023-02-20T20:58:36Z
Lists: pgsql-bugs
Greetings, * Charles (peacech@gmail.com) wrote: > Wrapping the query with a select * from (...) t where length(code) = 4 puts > the execution time back to 27 seconds. > > This is a bit unexpected since I expect that the result from the inner > query to be executed first and then filtered. It's really not- PG will (correctly) attempt to pull in such subselects into the overall optimization, which is generally better for everyone. If you want to force it, you can use a WITH MATERIALIZED CTE, or throw in an 'OFFSET 0' as a hack into your sub-select, but really it's a much better idea to generate extended stats on what you're filtering as has been suggested, or come up with a better data representation where you're not doing a search on a 'length()' as you are. Thanks, Stephen