WHERE clause is slower with value from CTE than with constant
I want to cache a variable during a query on Postgres 12: -- BEGIN PART 1 with cached_vars as ( select max(datetime) as datetime_threshold from locations where distance > 70 and user_id = 9087 ) -- END PART 1 -- BEGIN PART 2 select * from locations where user_id = 9087 and datetime > (select datetime_threshold from cached_vars) -- END PART 2 I expected runtime to equal part1 runtime + part2 runtime but it takes a lot longer. There is no performance issue when I run only the 2nd part with manual datetime_threshold. locations table columns: id | user_id | datetime | location | distance | ... How
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.