DBAtlas · 데이터베이스 운영 아틀라스

행 / 문서 수 조회(빠른 방법)

Oracle

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

num_rows는 마지막 ANALYZE/DBMS_STATS 실행 기준이다. 정확한 값은 COUNT(*)를 사용한다.

PostgreSQL

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

reltuples는 마지막 ANALYZE/VACUUM 시점의 추정치다. 대형 테이블에서 전체 COUNT는 비용이 크다.

MySQL

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

InnoDB에서 table_rows는 추정치이며 최대 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) 또는 1(clustered)이 행 수를 보여 준다. COUNT(*) 스캔보다 훨씬 빠르다.

MongoDB

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

estimatedDocumentCount는 메타데이터를 읽는다(즉시). countDocuments는 실제 스캔을 수행한다.

Redis

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

DBSIZE는 선택된 데이터베이스의 전체 키 수를 O(1)로 반환한다.

DBAtlas에서 열기 →