改進 PostgreSQL 的查詢效能
Amy McCarty
Instructor
是什麼
為什麼
用在哪裡
| ingredient | recipe |
|---|---|
| tomatoes | spaghetti & meatballs |
| green onions | fried rice |
| eggs | fried rice |
| ground beef | spaghetti & meatballs |
| pasta | spaghetti & meatballs |
| rice | fried rice |
| soy sauce | fried rice |
SELECT *
FROM cookbook
WHERE recipe = 'fried rice'

SELECT * FROM pg_indexes
| schemaname | tablename | indexname | tablespace | indexdef |
|---|---|---|---|---|
| food | dinner | recipe_index | null | CREATE INDEX recipe_index ... |

CREATE INDEX recipe_index
ON cookbook (recipe);
CREATE INDEX CONCURRENTLY recipe_index
ON cookbook (recipe, serving_size);
何時使用索引
何時避免索引
經常更新的資料表

查詢規劃器
EXPLAIN
SELECT *
FROM cookbook
查詢計畫
Seq scan on cookbook (cost=0.00...22.70
rows = 1270 width = 36)
改進 PostgreSQL 的查詢效能