tags:

views:

326

answers:

2

Lets say I've a table T with columns C1, C2 ... C10.

C1, C2 and C3 are most frequently refered columns in multiple queries. The order in which these columns are refered can't be controlled.

To improve query performance do I need to create multiple indexes such as (C1, C2, C3), (C1, C3, C2), (C2, C1, C3), (C2, C3, C1) etc.?

With example of two similar queries...

select * from t1, t2 where t1.c1 = t2.c1 and t1.c2 = t2.c2 and t1.c3 = t3.c3 and

select * from t1, t2 where t1.c2 = t2.c2 and t1.c1 = t2.c1 and t1.c3 = t3.c3

Do I need two indexes (C1, C2, C3) and (C2, C1, C3) for improving performance of above two different queries or just any one combination is enough for both the queries?

I guess it is not required and assume DB2 will be smart enough to rearrange columns in the query to match any one of the index combination as long as C1, C2 and C3 all are used.

Can anyone confirm and point me some articles related to that.

DB2 version used is 9.5 in AIX

+1  A: 

Database tuning is not a set-and-forget operation, any more than is investing in Bernie Madhoff's hedge funds :-)

Yous should provide a moderately intelligent first guess (based on expected behavior) and then monitor, in production, the queries that are executed.

You should, at a minimum, start with indexes on the individual columns. Then, if someone complains that their queries are taking too long, have DB2 analyse the query and see where the bottleneck is (explain plan or Visual Explain). At that point, you should figure out whether the cost of adding the index is outweighed by the benefit.

That's the only way you can be sure that your database is tuned correctly.

Your assumption that DB2 can re-order thing to use other indexes depends entirely on the query - all but the simplest of queries will invalidate your assumption.

For example, let's say you have the index (c1,c2). The query

select * from t where c2 = '2009-01-01';

will not use that index since all the c2 keyparts are scattered around the index, not in one area.

It may be inteeligent enough to optimize

select * from t where c2 = '2009-01-01' and c1 = 'x';

since it can then just look up x2009-01-01 in the index. However,

select * from t where c2 <= '2009-01-01' and c2 <= '2009-01-05' and c1 = 'x';

suffers the same problem as the first query - the c2 keyparts are not contiguous in the index.

That's why you should measure, not guess.

paxdiablo
Let me rephrase my question.Assuming all the columns C1, C2 and C3 will be used in multiple queries (in different ordering) do I need multiple permutations of index on those three columns.
Murthy
With example ...select * from t1, t2 where t1.c1 = t2.c1 and t1.c2 = t2.c2 and t1.c3 = t3.c3 and select * from t1, t2 where t1.c2 = t2.c2 and t1.c1 = t2.c1 and t1.c3 = t3.c3Do I need two indexes (C1, C2, C3) and (C2, C1, C3) for improving performance of above two different queriesorjust any one combination is enough for both the queries?
Murthy
What DB2 will do will depend on the statistics that have been collected - the order in which those clauses are processed will depend on the cardinality of the result sets which can only be determined with the stats. I'm pretty certain the default (for DB2/z anyway, don't know about the AIX version) is to process in the order they're given if no stats are available. But, it doesn't *matter* - you should be collecting stats *and* doing explains in the target environment, since the behavior will almost certainly change.
paxdiablo
+1  A: 

In DB2 for Linux/UNIX/Windows, the order of the predicates and join criteria in a query does not matter from the optimizer's perspective. The query rewrite portion of the optimizer will account for this.

Depending on the query, the optimizer may select the index, whether it is on (C3,C2,C1) or (C1,C2,C3). However, that's not to say that the column order in the index doesn't matter -- it does. Generally you would want to put columns in the order of highest cardinality to lowest cardinality, but of course there are lots of exceptions. There are entire books written on proper index design (the book by Lahdenmaki / Leach is very good), so I would recommend that you look into these.

Ian Bjorhovde