Heads up: counting may be slow in PostgreSQL

Updated . Posted . Visible to the public. Repeats.

The linked article points out that COUNT queries might be unexpectetly slow in psql.

If you just need to know "are there any records" use any?. This uses SELECT 1 AS one FROM ... LIMIT 1 under the hood.
If you just need to know "are there no records" use empty? or none?. This uses SELECT 1 AS one FROM ... LIMIT 1 under the hood.

In short: Replace foo.count > 0 with foo.any? or foo.count == 0 with foo.none?

Michael Leimstädtner
Last edit
Julian
License
Source code in this card is licensed under the MIT License.
Posted by Michael Leimstädtner to makandra dev (2021-05-31 06:18)