DBAtlas · Atlas de operaciones de bases de datos

Obtener el número de filas / documentos (rápido)

Oracle

-- Statistics-based (fast, estimated):
SELECT num_rows FROM dba_tables WHERE table_name = 'CUSTOMER';
-- Exact: SELECT COUNT(*) FROM customer;

num_rows refleja la última ejecución de ANALYZE/DBMS_STATS; use COUNT(*) para un valor exacto.

PostgreSQL

-- Estimated (instant):
SELECT reltuples::bigint FROM pg_class WHERE relname = 'customer';
-- Exact: SELECT count(*) FROM customer;

reltuples es una estimación a la fecha del último ANALYZE/VACUUM; un COUNT completo es costoso en tablas grandes.

MySQL

-- Estimated (InnoDB):
SELECT table_rows FROM information_schema.tables
WHERE table_name = 'customer';
-- Exact: SELECT COUNT(*) FROM customer;

En InnoDB, table_rows es una estimación y puede desviarse hasta un 50%.

SQL Server

SELECT SUM(row_count) AS rows
FROM sys.dm_db_partition_stats
WHERE object_id = OBJECT_ID('dbo.customer')
  AND index_id IN (0, 1);

index_id 0 (heap) o 1 (clustered) da el número de filas; mucho más rápido que un recorrido con COUNT(*).

MongoDB

// Fast (from metadata):
db.customer.estimatedDocumentCount()
// Exact (can count with a filter): db.customer.countDocuments({})

estimatedDocumentCount lee metadatos (instantáneo); countDocuments realiza un recorrido real.

Redis

DBSIZE
# Specific pattern: count roughly with SCAN (do not use KEYS * in production)

DBSIZE devuelve el número total de claves de la base de datos seleccionada en O(1).

Abrir en DBAtlas →