Trung BìnhPostgreSQL iconPostgreSQL

Composite index thứ tự column quan trọng thế nào?

Trong B-tree composite index, thứ tự column rất quan trọng. Index (tenant_id, status, created_at) hỗ trợ tốt query filter theo tenant_id, rồi status, rồi range/order theo created_at. Nó không tương đương với (created_at, status, tenant_id).

Ví dụ:

sql
CREATE INDEX idx_orders_tenant_status_created
ON orders (tenant_id, status, created_at DESC);

Đặt column equality có selectivity tốt trước, range/order sau.

Kiểm chứng bằng query thật và EXPLAIN ANALYZE.

Xem toàn bộ PostgreSQL cùng filter theo level & chủ đề con.

Mở danh sách PostgreSQL