40

I have a table with a multicolumn index, and I have doubts about the proper sorting of the indexes to get the maximum performance on the queries.

The scenario:

  • PostgreSQL 8.4, table with about one million rows

  • Values in column c1 can have about 100 different values. We can assume the values are evenly distributed, so we have about 10000 rows for every possible value.

  • Column c2 can have 1000 different values. We have 1000 rows for every possible value.

When searching data, the condition always includes values for these two columns, so the table has a multicolumn index combining c1 and c2. I have read about the importance of properly ordering the columns in a multicolumn index if you have queries using just one column for filtering. This is not the case in our scenario.

My question is this one:

Given the fact that one of the filters selects a much smaller set of data, could I improve performance if the first index is the most selective one (the one which allows a smaller set)? I had never considered this question until I saw the graphics from the referenced article:

enter image description here

Image taken from the referenced article about multicolumn indexes.

The queries use values from the two columns for filtering. I have no queries using just one column for filtering. All of them are: WHERE c1=@ParameterA AND c2=@ParameterB. There are also conditions like this: WHERE c1 = "abc" AND c2 LIKE "ab%"

0

2 Answers 2

43

Answer

Since you refer to the website use-the-index-luke.com, consider the chapter:

Use The Index, Luke › The Where Clause › Searching For Ranges › Greater, Less and BETWEEN

It has an example that matches your situation perfectly (two-column index, one is tested for equality, the other for range), explains (with more of those nice index graphics) why @ypercube's advice is accurate and sums it up:

Rule of thumb: index for equality first — then for ranges.

Also good for just one column?

What to do for queries on just one column seems to be clear. More details and benchmarks concerning that under these related question:

Less selective column first?

Apart from that, what if you have only equality conditions for both columns?

It doesn't matter. Put the column first that is more likely to receive conditions of its own, which actually matters.

Demo

A simple table of two columns with 100k rows. One with very few, the other one with lots of distinct values. Original test run in 2013 with Postgres 9.2:

CREATE TABLE tbl AS
SELECT (random() * 10000)::int AS lots
     , (random() * 4)::int     AS few
FROM generate_series (1, 100000);

DELETE FROM tbl WHERE random() > 0.9;  -- create some dead tuples, more "real-life"

VACUUM ANALYZE tbl;

SELECT count(distinct lots)   -- 9999
     , count(distinct few)    --    5
FROM   tbl;

Query:

SELECT *
FROM   tbl
WHERE  lots = 2345
AND    few = 2;

EXPLAIN ANALYZE output (Best of 10 to exclude caching effects):

Seq Scan on tbl  (cost=0.00..5840.84 rows=2 width=8)
                 (actual time=5.646..15.535 rows=2 loops=1)
  Filter: ((lots = 2345) AND (few = 2))
  Buffers: local hit=443
Total runtime: 15.557 ms

Add index, retest:

CREATE INDEX tbl_lf_idx ON tbl(lots, few);
Index Scan using tbl_lf_idx on t  (cost=0.00..3.76 rows=2 width=8)
                                       (actual time=0.008..0.011 rows=2 loops=1)
  Index Cond: ((lots = 2345) AND (few = 2))
  Buffers: local hit=4
Total runtime: 0.027 ms

Add other index, retest:

DROP INDEX tbl_lf_idx;
CREATE INDEX tbl_fl_idx ON tbl(few, lots);
Index Scan using tbl_fl_idx on tbl  (cost=0.00..3.74 rows=2 width=8)
                                    (actual time=0.007..0.011 rows=2 loops=1)
  Index Cond: ((few = 2) AND (lots = 2345))
  Buffers: local hit=4
Total runtime: 0.027 ms

Repeated 2021 with Postgres 13, same conclusion:

db<>fiddle here

5
  • Is this also the case for 3 (or more) columns in the index?
    – hayd
    Sep 19, 2019 at 7:52
  • @hayd: Not sure what "this" refers to. You might ask a new question. You can always reference this one for context. (And drop a comment here to link back.) Sep 19, 2019 at 15:43
  • By "this" I mean "does ordering of the index definition matter if there are more than 2 columns in the index definition"
    – hayd
    Sep 19, 2019 at 17:33
  • @hayd: Most important point: a btree index is good for queries with equality conditions on leading index expressions. Order among those is mostly irrelevant. Many other details that won't fit in a comment ... Sep 19, 2019 at 23:04
  • Thanks, i will try and write a coherent question and link to it.
    – hayd
    Sep 20, 2019 at 4:30
12

If, as you say, the queries involving these 2 columns, are all equality checks of both columns, e.g.:

WHERE c1=@ParameterA AND c2=@ParameterB

do not bother with this. I doubt there will be any difference and if there is one, it will be negligible. You can always test of course, with your data and your server settings. Different versions of a DBMS can behave slightly differently regarding optimization.

The order inside the index would matter for other types of queries, having checks of one column only, or inequality conditions, or conditions on one column and grouping in the other, etc.

If I were to choose one of the two orders, I'd choose to put the less selective column first. Consider a table with columns year and month. It's more probable that you need a WHERE year = 2000 condition or a WHERE year BETWEEN 2000 AND 2013 or a WHERE (year, month) BETWEEN (1999, 6) AND (2000, 5).

A query of the type WHERE month = 7 GROUP BY year may be wanted sure (Find people born on July), but would be less often. That depends of course on the actual data stored in your table. Choose one order for now, say the (c1, c2) and you can always add another index later (c2, c1).


Update, after the OP's comment:

There are also conditions like this: WHERE c1 = 'abc' AND c2 LIKE 'ab%'

This type of query if exactly a range condition on c2 column and would need a (c1, c2) index. If you also have queries of the reverse type:

WHERE c2 = 'abc' AND c1 LIKE 'ab%'

then it would be good if you had a (c2, c1) index as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.