Обсуждение: Multicolumn indexes and equal conditions

Поиск
Список
Период
Сортировка

Multicolumn indexes and equal conditions

От
Bruno Wolff III
Дата:
I noticed that when a multicolumn index exists it isn't necessarily
fully used when the first column is constrained by an equals condition.
However by adding a redundant sort condition you can get both columns
used.

In the following examples crate has an index on gameid and areaid.

The examples below are for 7.4 development, but 7.3.2 behaves similarly.

explain analyze select areaid from crate where gameid = 'TTN' order by areaid;
                                                         QUERY PLAN
    

----------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=132.93..133.02 rows=36 width=11) (actual time=5.44..5.57 rows=287 loops=1)
   Sort Key: areaid
   ->  Index Scan using crate_game on crate  (cost=0.00..132.00 rows=36 width=11) (actual time=0.06..1.94 rows=287
loops=1)
         Index Cond: (gameid = 'TTN'::text)
 Total runtime: 5.81 msec
(5 rows)


explain analyze select areaid from crate where gameid = 'TTN' order by gameid, areaid;
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
 Index Scan using crate_game on crate  (cost=0.00..132.00 rows=36 width=18) (actual time=0.08..2.06 rows=287 loops=1)
   Index Cond: (gameid = 'TTN'::text)
 Total runtime: 2.51 msec
(3 rows)