In SQL, in what situation do we want to Index a field in a table, or 2 fields in a table at the same

Posted by Jian Lin on Stack Overflow See other posts from Stack Overflow or by Jian Lin
Published on 2010-04-25T10:40:04Z Indexed on 2010/04/25 10:43 UTC
Read the original article Hit count: 299

Filed under:
|
|
|
|

In SQL, it is obvious that whenever we want to do a search on millions of record, say CustomerID in a Transactios table, then we want to add an index for CustomerID.

Is another situation we want to add an index to a field when we need to do inner join or outer join using that field as a criteria? Such as Inner join on t1.custumerID = t2.customerID. Then if we don't have an index on customerID on both tables, we are looking at O(n^2) because we need to loop through the 2 tables sequentially. If we have index on customerID on both tables, then it becomes O( (log n) ^ 2 ) and it is much faster.

Any other situation where we want to add an index to a field in a table?

What about adding index for 2 fields combined in a table. That is, one index, for 2 fields together?

© Stack Overflow or respective owner

Related posts about dbms

Related posts about indexing