Search code examples
sqldb2indexingcomposite-index

is a db index composite by default?


when I create an index on a db2, for example with the following code:

CREATE INDEX T_IDX ON T(
A,
B)

is it a composite index? if not: how can I then create a composite index? if yes: in order to have two different index should I create them separately as:

CREATE INDEX T1_IDX ON T(A)
CREATE INDEX T2_IDX ON T(A)

EDIT: this discussion is not going in the direction I expect (but in a better one :)) I actually asked how, and not why to create separate indexes, I planed to do that in a different question, but since you anticipated me:

suppose I have a table T(A,B,C) and a search function search() that select from the table using any of the following method

WHERE A = x
WHERE B = x
WHERE C = x
WHERE A = x AND B=y (and so on AC, CB, ABC)

if I create a compose index ABC, is it going to working for example when I select on just C? the table is quite big, and the insert\update not so frequent


Solution

  • Yep multiple fields on create index = composite by definition: Specify two or more column names to create a composite index.

    Understanding when to use composite indexes appears to be your last question...

    If all columns selected by a query are in a composite index, then the dbengine can return these values from the index without accessing the table. so you have faster seek time.

    However if one or the other are used in queries, then creating individual indexes will serve you best. It depends on the types of queries executed and what values they contain/filter/join.

    If you sometimes have one, the other, or both, then creating all 3 indexes is a possibility as well. But keep in mind each additional index increases the amount of time it takes to insert, update or delete, so on highly maintained tables, more indexes are generally bad since the overhead to maintain the indexes effects performance.