我在「操作員」字段有一個300K行和b-tree索引的表。 雖然我運行這個查詢它不使用索引。 「運算符」與dict.vw_dict_operator.id具有相同的數據類型。爲什麼這個查詢沒有使用索引?
EXPLAIN SELECT
id,
name
FROM
dict.vw_dict_operator self
WHERE
EXISTS (
SELECT 42 FROM ti.ti_flight_availability flight_avail
WHERE flight_avail.operator = self.id
)
ORDER BY
self.name
"Sort (cost=3349.66..3351.02 rows=545 width=18)"
" Sort Key: dict_operator.name"
" -> Seq Scan on dict_operator (cost=0.00..3324.89 rows=545 width=18)"
" Filter: ((NOT trash) AND (subplan))"
" SubPlan"
" -> Seq Scan on ti_flight_availability flight_avail (cost=0.00..8513.66 rows=3750 width=0)"
" Filter: (operator = $0)"
UPD:thanks @gbn。當連接表時也不會使用索引
EXPLAIN SELECT self.id, self.name
FROM dict.vw_dict_operator self JOIN ti.ti_flight_availability flight_avail
ON flight_avail.operator = self.id
"Nested Loop (cost=0.00..92988.47 rows=228639 width=18)"
" -> Seq Scan on ti_flight_availability flight_avail (cost=0.00..7754.33 rows=303733 width=4)"
" -> Index Scan using pk_dict_operator on dict_operator (cost=0.00..0.27 rows=1 width=18)"
" Index Cond: (dict_operator.id = flight_avail.operator)"
" Filter: (NOT dict_operator.trash)"
我不知道如何使用索引,但依賴子查詢通常是不好的mojo。 – Powerlord 2010-08-31 19:24:20
@ R. Bemrose你可以在沒有子查詢的情況下編寫這個任務的實現嗎? – noxvile 2010-08-31 19:39:48