Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
Rails fastest way to get table records count

I have a user table in Postgresql database, if run User.count, it takes 150ms to get result. It is too too slow to us. Ideally, it shall take less than 10ms to return me the result. Is there any way to cache sql result in model level? Something like

def self.total_count
  User.count.cached # that's my imagination
end
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In my opinion, there are several ways you could go about this -

  1. You could have another table that stores the count of the total number of users by incrementing the count there when a user is added/deleted or at frequent time intervals.
  2. If your table is extremely big and accuracy is not the most important thing, you also look into Postgres' COUNT ESTIMATE query.

    SELECT reltuples AS approximate_row_count FROM pg_class WHERE relname = 'users';

over 4 years ago · Santiago Trujillo Denunciar

0

You should look into counter_cache. It will work great if your user belongs_to another model http://guides.rubyonrails.org/association_basics.html

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda