DBAtlas · Atlas für Datenbankbetrieb

Zeilen- / Dokumentanzahl ermitteln (schnell)

Oracle

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

num_rows spiegelt den letzten ANALYZE/DBMS_STATS-Lauf wider; für einen exakten Wert COUNT(*) verwenden.

PostgreSQL

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

reltuples ist eine Schätzung zum Stand des letzten ANALYZE/VACUUM; ein vollständiges COUNT ist bei großen Tabellen teuer.

MySQL

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

Bei InnoDB ist table_rows eine Schätzung und kann bis zu 50% abweichen.

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) oder 1 (Clustered) liefert die Zeilenanzahl; viel schneller als ein COUNT(*)-Scan.

MongoDB

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

estimatedDocumentCount liest Metadaten (sofort); countDocuments führt einen echten Scan aus.

Redis

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

DBSIZE liefert die Gesamtzahl der Keys in der ausgewählten Datenbank in O(1).

In DBAtlas öffnen →