0

I am looking through some table source code, and I'm not sure how indexes work. Here is the example I see.

CREATE INDEX INDEXNAME ON AWARD ( AWARD_ID, CUST_ID );

I don't understand what the parameter values mean. Is the index for each individual column, or is it combined together to become an index.

4
  • 1
    it's an index that happens to index two fields, award_id and cust_id. That's all. Commented Apr 2, 2014 at 15:05
  • appreciated, thank you. could you recommend a good db2 resource to reference? the IBM site is god awful to read and the examples aren't explained very well. Commented Apr 2, 2014 at 15:40
  • for this sort of thing, it's just plain standard sql. Commented Apr 2, 2014 at 16:20
  • This is called a composite key. The key is composed of more than one field. Commented Apr 3, 2014 at 4:06

1 Answer 1

2

It's an index that contains two fields. Indexes in general are used for Selection, Joining, Grouping and/or Ordering.

The key thing to realize is that a multi column index is useful from left to right.

For selection, such an index would be quite useful if you had a where clause that looked like

WHERE AWARD_ID = 123 AND CUST_ID = 456

If would also be helpful for

WHERE AWARD_ID = 123

But probably not (directly) helpful for

WHERE CUST_ID = 456

Since the leftmost column of the index (AWARD_ID) is not referenced.

Joining works in a similar manner.

--index useful
FROM TBLA JOIN AWARD USING (AWARD_ID,CUST_ID)
 or
FROM TBLA JOIN AWARD USING (AWARD_ID)
--index NOT useful
FROM TBLA JOIN AWARD USING (CUST_ID)

as do ordering and grouping.

If you happen to be using DB2 for IBM i, the following paper is an awesome resource:

http://www-304.ibm.com/partnerworld/wps/servlet/ContentHandler/stg_ast_sys_wp_db2_i_indexing_methods_strategies

If DB2 LUW, the parts about bitmap indexs probably apply, but ignore the information about EVI indexes. Also look at the planning and performance sub-sections Indexes section in the DB2 LUW infocenter http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.admin.perf.doc/com.ibm.db2.luw.admin.perf.doc-gentopic8.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.