Switching out connectby() with a CTE #lantern #postgresql
I got annoyed with seeing loglines like this from PostgreSQL:
2022-05-28 01:57:34 CEST [2992011-1] lantern@lantern LOG: duration: 606.988 ms
statement: SELECT article.id, [...] ORDER BY date DESC LIMIT 7
Really? 600 ms for just getting the 7 articles for the frontpage? Admittedly including the comment count, but still!
So after finding the obligatory example code on StackOverflow and fighting my way through adapting it to work, I got the query down to a more reasonable ~20 ms.
Also, the query that generates the Archive page was identical (except for the LIMIT (and sort order)), so I refactored it to use the same base SQL-string. This could be improved to only get the fields needed on the page, but then I need to figure out how to have a partial Article data type, and I don't want to do that now.
There are still 3
places
I use connectby()
; they need to be eliminated as well, but it's late,
so not tonight.
One thing I didn't leave for another day, was to get rid of a warning. Phew.
Adam writes:
Now they are gone as well!
- Adam Sjøgren 🕚︎ - 2022-05-28