In SQL, in what situation do we want to Index a field in a table, or 2 fields in a table at the same
- by Jian Lin
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?