PostgreSQL 索引

索引加速查询:B-tree 默认,还有 hash、GiST、GIN、BRIN 等类型;EXPLAIN 验证是否走索引。

Intermediate
Key PointsPostgreSQL,索引,B-tree,GiST,GIN,EXPLAIN

讲解

索引加速查询:B-tree 默认,还有 hash、GiST、GIN、BRIN 等类型;EXPLAIN 验证是否走索引。

示例

SQL
CREATE INDEX idx_company_name ON company (name);
CREATE INDEX idx_orders_jsonb ON orders USING GIN (attrs);  -- JSONB 用 GIN
EXPLAIN SELECT * FROM company WHERE name = 'Tom';
Output
                        QUERY PLAN
-----------------------------------------------------------
 Index Scan using idx_company_name on company  (cost=0.14..8.16)

Friends & Partners