と複数の列に索引私は2つの列の2つの指標があります:私は実行するとのPostgres:別の先頭列
create index idx_film_length_rating on film (length, rating);
create index idx_film_rating_length on film (rating, length);
を:
explain analyze select title, length, rating, replacement_cost, rental_rate
from film
where rating = 'G' and length between 60 and 70
Postgresはidx_film_rating_lengthは、常に最良の選択肢だと思う、とこのインデックスを使用します。しかし、2番目のインデックスidx_film_rating_lengthがないと、クエリが遅くなるのはなぜですか?私の理解のために、実行計画は同じで、抽出ブロックは同じで、同じでなければなりません。
唯一のインデックスと結果は次のとおりです。
"Bitmap Heap Scan on film (cost=4.44..35.70 rows=13 width=34) (actual time=0.102..0.120 rows=18 loops=1)"
" Recheck Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
" Heap Blocks: exact=14"
" -> Bitmap Index Scan on idx_film_rating_length (cost=0.00..4.44 rows=13 width=0) (actual time=0.095..0.095 rows=18 loops=1)"
" Index Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
"Planning time: 0.316 ms"
"Execution time: 0.160 ms"
、テーブル内の2つのインデックスと結果:
"Bitmap Heap Scan on film (cost=4.44..35.70 rows=13 width=34) (actual time=0.030..0.041 rows=18 loops=1)"
" Recheck Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
" Heap Blocks: exact=14"
" -> Bitmap Index Scan on idx_film_rating_length (cost=0.00..4.44 rows=13 width=0) (actual time=0.024..0.024 rows=18 loops=1)"
" Index Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
"Planning time: 0.199 ms"
"Execution time: 0.065 ms"
のプランが同じであるにもかかわらずあなたが見ることができる、もう一つは高速です。
================== @a_horse_with_no_nameさんがお勧めします 追加した後バッファと詳細:
"Bitmap Heap Scan on film (cost=4.44..35.70 rows=13 width=34) (actual time=0.692..0.716 rows=18 loops=1)"
" Recheck Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
" Heap Blocks: exact=14"
" Buffers: shared hit=14 read=2"
" -> Bitmap Index Scan on idx_film_cover (cost=0.00..4.44 rows=13 width=0) (actual time=0.680..0.680 rows=18 loops=1)"
" Index Cond: ((rating = 'G'::mpaa_rating) AND (length >= 60) AND (length <= 70))"
" Buffers: shared read=2"
"Planning time: 1.773 ms"
"Execution time: 1.441 ms"
問題は関連付けるかもしれないようだ: "バッファは:= 14読み取り= 2ヒット共有しますか"?
詳細を表示して相違点をピンポイントで表示するために 'explain(analyze、buffers、timing)'を使用してください –
ありがとう、私はバッファとタイミングで結果を追加しました。 –