title: Add Indexes on WHERE and JOIN Columnsimpact: CRITICALimpactDescription: 100-1000x faster queries on large tablestags: indexes, performance, sequential-scan, query-optimization在 WHERE 和 JOIN 列上创建索引对未建立索引的列进行过滤或连接的查询会导致全表扫描随着表的增长速度会呈指数级下降。错误做法大表上的顺序扫描-- customer_id 上没有索引导致全表扫描select*fromorderswherecustomer_id123;-- EXPLAIN 显示Seq Scan on orders (cost0.00..25000.00 rows100 width85)正确做法索引扫描-- 在频繁过滤的列上创建索引createindexorders_customer_id_idxonorders(customer_id);select*fromorderswherecustomer_id123;-- EXPLAIN 显示Index Scan using orders_customer_id_idx (cost0.42..8.44 rows100 width85)对于 JOIN 列始终为外键一侧建立索引-- 为引用列建立索引createindexorders_customer_id_idxonorders(customer_id);selectc.name,o.totalfromcustomers cjoinorders oono.customer_idc.id;参考查询优化