January 28th, 2013 by depesz | Tags: , , , | 7 comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

Whenever you are considering creation of multicolumn index, there is a question what should be the order of columns in this index. I'll try to analyze various cases of this situation.

We need to consider two separate issues:

  • what operators are you going to use
  • What is selectivity of the fields

First, let's consider that you want to use inequality operators (<, >, <= or &gr;=), and you're using both columns in your query. In this case, you can generally assume both conditions use index.

Some test data:

$ create table t1 ( a int4, b int4 );
CREATE TABLE
$ insert into t1 (a,b)
    select random()*100000, random()*100000
    from generate_series(1,1000000);
INSERT 0 1000000
$ create index i1 on t1 (a,b);
CREATE INDEX
$ create table t2 ( a int4, b int4 );
CREATE TABLE
$ insert into t2 (a,b)
    select random()*100000, random()*100000
    from generate_series(1,1000000);
INSERT 0 1000000
$ create index i2 on t2 (b,a);
CREATE INDEX

And let's see – simple select that should return now much rows:

$ explain analyze
select *
from
t1
where a < 1000
and b > 99000;
                                                   QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on t1  (cost=424.96..1074.04 rows=182 width=8) (actual time=1.673..2.879 rows=205 loops=1)
   Recheck Cond: ((a < 1000) AND (b > 99000))
   ->  Bitmap Index Scan on i1  (cost=0.00..424.92 rows=182 width=0) (actual time=1.624..1.624 rows=205 loops=1)
         Index Cond: ((a < 1000) AND (b > 99000))
 Total runtime: 2.940 ms
(5 rows)

And with index the other way around:

$ explain analyze
select *
from
t2
where a < 1000
and b > 99000;
                                                   QUERY PLAN
----------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on t2  (cost=229.76..592.85 rows=103 width=8) (actual time=1.095..1.768 rows=95 loops=1)
   Recheck Cond: ((b > 99000) AND (a < 1000))
   ->  Bitmap Index Scan on i2  (cost=0.00..229.74 rows=103 width=0) (actual time=1.056..1.056 rows=95 loops=1)
         Index Cond: ((b > 99000) AND (a < 1000))
 Total runtime: 1.824 ms
(5 rows)

Now, what about a mix of equality and inequality?

First, with equality being used on first column in index:

$ explain analyze select * from t1 where a = 123 and b >= 99000;
                                                 QUERY PLAN
------------------------------------------------------------------------------------------------------------
 Index Only Scan using i1 on t1  (cost=0.43..8.45 rows=1 width=8) (actual time=0.027..0.027 rows=0 loops=1)
   Index Cond: ((a = 123) AND (b >= 99000))
   Heap Fetches: 0
 Total runtime: 0.083 ms
(4 rows)

and what about the case where inequality is first?

$ explain analyze select * from t2 where a = 123 and b >= 99000;
                                                  QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Index Only Scan using i2 on t2  (cost=0.42..233.75 rows=1 width=8) (actual time=0.831..1.005 rows=1 loops=1)
   Index Cond: ((b >= 99000) AND (a = 123))
   Heap Fetches: 1
 Total runtime: 1.049 ms
(4 rows)

Hurray – it also works. But one thing – the second case is more expensive. To show it, let's make a special case:

$ create table t3 (a int4, b int4);
CREATE TABLE
 
$ insert into t3 select i, j from generate_series(1,10000) i, generate_series(1,100) j;
INSERT 0 1000000
 
$ create index i3 on t3 (a, b);
CREATE INDEX
 
$ create table t4 (a int4, b int4);
CREATE TABLE
 
$ insert into t4 select i, j from generate_series(1,10000) i, generate_series(1,100) j;
INSERT 0 1000000
 
$ create index i4 on t4 (b, a);
CREATE INDEX

As you can see column a has pretty good selectivity, while column b much lower.

Now, when I'll first use equality, and then inequality:

explain analyze
select * from t3 where a = 123 and b < 50;
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Index Only Scan using i3 on t3  (cost=0.42..91.64 rows=48 width=8) (actual time=0.064..0.084 rows=49 loops=1)
   Index Cond: ((a = 123) AND (b < 50))
   Heap Fetches: 49
 Total runtime: 0.141 ms
(4 rows)

If it's the other way around:

explain analyze
select * from t4 where a = 123 and b < 50;
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Index Only Scan using i4 on t4  (cost=0.42..10434.23 rows=48 width=8) (actual time=0.077..32.651 rows=49 loops=1)
   Index Cond: ((b < 50) AND (a = 123))
   Heap Fetches: 49
 Total runtime: 32.676 ms
(4 rows)

Of course one can say – OK, you used equality for very selective, and inequality for not really selective column. What would happen if the conditions were swapped?

Let's see:

$ explain analyze
select * from t3
where a < 100
and b = 80;
                                                   QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
 Index Only Scan using i3 on t3  (cost=0.42..402.17 rows=107 width=8) (actual time=0.045..2.292 rows=99 loops=1)
   Index Cond: ((a < 100) AND (b = 80))
   Heap Fetches: 99
 Total runtime: 2.353 ms
(4 rows)
 
$ explain analyze
select * from t4
where a < 100
and b = 80;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on t4  (cost=5.32..318.02 rows=87 width=8) (actual time=0.094..0.283 rows=99 loops=1)
   Recheck Cond: ((b = 80) AND (a < 100))
   ->  Bitmap Index Scan on i4  (cost=0.00..5.29 rows=87 width=0) (actual time=0.067..0.067 rows=99 loops=1)
         Index Cond: ((b = 80) AND (a < 100))
 Total runtime: 0.344 ms
(5 rows)

This shows, in my opinion that in mixed cases (one field using =, and the other some inequality), it's better to have index first on field used for equality.

Which leaves us with relatively simple case – what about case where we have both columns compared using equality, just their selectivity is widely different.

And let's make it really very different. Like:

$ create table t5 (a int4, b int4);
CREATE TABLE
 
$ insert into t5 select i, j from generate_series(1,5000000) i, generate_series(1,2) j;
INSERT 0 10000000
 
$ create index i5 on t5 (a, b);
CREATE INDEX
 
$ create table t6 (a int4, b int4);
CREATE TABLE
 
$ insert into t6 select i, j from generate_series(1,5000000) i, generate_series(1,2) j;
INSERT 0 10000000
 
$ create index i6 on t6 (b, a);
CREATE INDEX

Another set of tables. In both, a has 500,000 different values, while b has just two.

How are the speeds now?

explain analyze
select * from t5 where a = 2314 and b = 1;
                                                 QUERY PLAN
------------------------------------------------------------------------------------------------------------
 Index Only Scan using i5 on t5  (cost=0.43..8.46 rows=1 width=8) (actual time=0.009..0.009 rows=1 loops=1)
   Index Cond: ((a = 2314) AND (b = 1))
   Heap Fetches: 1
 Total runtime: 0.028 ms
(4 rows)
 
explain analyze
select * from t6 where a = 2314 and b = 1;
                                                 QUERY PLAN
------------------------------------------------------------------------------------------------------------
 Index Only Scan using i6 on t6  (cost=0.43..8.46 rows=1 width=8) (actual time=0.010..0.010 rows=1 loops=1)
   Index Cond: ((b = 1) AND (a = 2314))
   Heap Fetches: 1
 Total runtime: 0.028 ms
(4 rows)

(No, I didn't edit the results – I made script which called both explains 3 times in a row, and stored output of last one)

Looks like there is no difference. Of course – it might be that my sample size is too small, but still – 5 million rows in an index is pretty good test case.

So, to wrap it up:

  • in case you're using a = ? and b = ? – there is no time difference between index on (a,b) and (b,a)
  • in case you're using a = ? and b </> ? – index on (a,b) is better
  • In case you're using a </>, b </> – both searches should be done using index, but, given results from t3/t4 test above – I would put first the column that has better selectivity

Hope it helps.

  1. 7 comments

  2. # Capt. Obvious
    Jan 28, 2013

    What about if both (a,b) and (b,a) indexes exist? Will PostgreSQL use both in a single query or prefer to scan just one index or is this choice dependent on index scan cost?

  3. Jan 28, 2013

    @Capt.:
    PostgreSQL has the capability to use two indexes in single query, but I don’t see any scenario where using them both would be beneficial (over single index usage). As for test – sure, I can make it – just tell me what data distribution you’re curious about, because I just don’t see it.

  4. # Capt. Obvious
    Jan 28, 2013

    I am curious about what choices the planner will make when faced with the above distributions and both (a,b) and (b,a) indexes are available and how this choice is affected by index scan cost.

  5. # Emanuel
    Jan 29, 2013

    Brief: order the columns in the index accordingly to the selectivity (Better goes to the left).

    Pretty nice hack Depesz! Hope see you at FOSDEM.

  6. Jan 30, 2013

    @Emanuel:
    generally your brief is OK, unless your less-selective column is used for “=”, and the more selective from >/=/<=.

  7. Jan 30, 2013

    @Capt.:
    Well, it’s simple to check. So:

    1. for random distribution (1st case), and search for inequality/inequality, Pg uses single index.
    2. for the 2nd cast (t3 table), it’s using index on (a,b), ignoring (b,a)
    3. for 3rd case (t5 table): it uses, quite surprisingly, index on (b,a) (b is much less selective).

  8. # MarcinP
    Jan 31, 2013

    There is a very nice website/book about using indices. Have a look at http://use-the-index-luke.com/ if you already have not known it.

Leave a comment