Read more

Heads up: counting may be slow in PostgreSQL

Michael Leimstädtner
May 31, 2021Software engineer at makandra GmbH

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

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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?

Posted by Michael Leimstädtner to makandra dev (2021-05-31 08:18)