Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a quick question. If I have a table with a multiple column index(col1, col2, col3) and I would do a query like:

SQL
SELECT * FROM someTable WHERE col3 = 15


Would it use the index or will it still read the table from front to back? Or would I be better off using 3 single column indexes?

Willem
Posted

SQL should use your index just fine. Here is a Stack Overflow question with a number of great answers that will help you even more:

http://stackoverflow.com/questions/179085/multiple-indexes-vs-multi-column-indexes[^]

The one thing I would add is that you should always test these things out to be sure that SQL is doing what you expect it to be doing.
 
Share this answer
 
The question was quick indeed, but the answer must be slow...
Use the 'show actual execution plan' in SQL Management Studio, to analyze a particular query. The execution plan will depend, among other things, on:

  1. How many records are there on the table? For a tiny table with a few thousand records indexes will not be used.
  2. Using statistics, which percentage of the records in the table are expected to return from your query? For above 15% of the records, indexes will seldom if ever be used.
  3. More...


Hope this helps,
Pablo.
 
Share this answer
 
Comments
willempipi 19-Jun-12 9:58am    
Table contains about 14 million records, is 12 columns wide and has a varchar(2048) column, datetime column and all others are bit and int columns. Most of the queries will return < 0.1%, some about 3%.
Thanks for pointing me out in the right direction...


For those who googled this page, the answer is: No, it will not use the index. Because "col3" is the 3th and last column of the index the sql server doesn't save any time searching for the index. This will lead to very long queries.

I'm better off having a index on the column AND have a index on the pair.

http://www.mysqlperformanceblog.com/2008/08/22/multiple-column-index-vs-multiple-indexes/[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900