<p>If I have a query like:</p>

<pre><code>Select EmployeeId 
From Employee 
Where EmployeeTypeId IN (1,2,3)
</code></pre>

<p>and I have an index on the <code>EmployeeTypeId</code> field, does SQL server still use that index?</p>

[{'answer_id': 31504, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 3, 'selected': False, 'text': '<p>Usually it would, unless the IN clause covers too much of the table, and then it will do a table scan.  Best way to find out in your specific case would be to run it in the query analyzer, and check out the execution plan.</p>\n'}, {'answer_id': 31520, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 6, 'selected': True, 'text': '<p>Yeah, that\'s right.  If your <code>Employee</code> table has 10,000 records, and only 5 records have <code>EmployeeTypeId</code> in (1,2,3), then it will most likely use the index to fetch the records.  However, if it finds that 9,000 records have the <code>EmployeeTypeId</code> in (1,2,3), then it would most likely just do a table scan to get the corresponding <code>EmployeeId</code>s, as it\'s faster just to run through the whole table than to go to each branch of the index tree and look at the records individually.  </p>\n\n<p>SQL Server does a lot of stuff to try and optimize how the queries run.  However, sometimes it doesn\'t get the right answer. If you know that SQL Server isn\'t using the index, by looking at the execution plan in query analyzer, you can tell the query engine to use a specific index with the following change to your query.</p>\n\n<pre class="lang-sql prettyprint-override"><code>SELECT EmployeeId FROM Employee WITH (Index(Index_EmployeeTypeId )) WHERE EmployeeTypeId IN (1,2,3)\n</code></pre>\n\n<p>Assuming the index you have on the <code>EmployeeTypeId</code> field is named <code>Index_EmployeeTypeId</code>. </p>\n'}, {'answer_id': 31620, 'author': 'Dana the Sane', 'author_id': 2567, 'author_profile': 'https://Stackoverflow.com/users/2567', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>So there\'s the potential for an "IN" clause to run a table scan, but the optimizer will \n  try and work out the best way to deal with it?</p>\n</blockquote>\n\n<p>Whether an index is used doesn\'t so much vary on the type of query as much of the type and distribution of data in the table(s), how up-to-date your table statistics are, and the actual datatype of the column.</p>\n\n<p>The other posters are correct that an index will be used over a table scan if:</p>\n\n<ul>\n<li>The query won\'t access more than a certain percent of the rows indexed (say ~10% but should vary between DBMS\'s).</li>\n<li>Alternatively, if there are a lot of rows, but relatively few unique values in the column, it also may be faster to do a table scan.</li>\n</ul>\n\n<p>The other variable that might not be that obvious is making sure that the datatypes of the values being compared are the same. In PostgreSQL, I don\'t think that indexes will be used if you\'re filtering on a float but your column is made up of ints. There are also some operators that don\'t support index use (again, in PostgreSQL, the ILIKE operator is like this).</p>\n\n<p>As noted though, always check the query analyser when in doubt and your DBMS\'s documentation is your friend.</p>\n'}, {'answer_id': 31877, 'author': 'Mike Woodhouse', 'author_id': 1060, 'author_profile': 'https://Stackoverflow.com/users/1060', 'pm_score': 2, 'selected': False, 'text': '<p>Unless technology has improved in ways I can\'t imagine of late, the "IN" query shown will produce a result that\'s effectively the OR-ing of three result sets, one for each of the values in the "IN" list. The IN clause becomes an equality condition for each of the list and will use an index if appropriate. In the case of unique IDs and a large enough table then I\'d expect the optimiser to use an index.</p>\n\n<p>If the items in the list were to be non-unique however, and I guess in the example that a "TypeId" is a foreign key, then I\'m more interested in the distribution. I\'m wondering if the optimiser will check the stats for each value in the list? Say it checks the first value and finds it\'s in 20% of the rows (of a large enough table to matter). It\'ll probably table scan. But will the same query plan be used for the other two, even if they\'re unique?</p>\n\n<p>It\'s probably moot - something like an Employee table is likely to be small enough that it will stay cached in memory and you probably wouldn\'t notice a difference between that and indexed retrieval anyway.</p>\n\n<p>And lastly, while I\'m preaching, beware the query in the IN clause: it\'s often a quick way to get something working and (for me at least) can be a good way to express the requirement, but it\'s almost always better restated as a join. Your optimiser may be smart enough to spot this, but then again it may not. If you don\'t currently performance-check against production data volumes, do so - in these days of cost-based optimisation you can\'t be certain of the query plan until you have a full load and representative statistics. If you can\'t, then be prepared for surprises in production...</p>\n'}, {'answer_id': 33584, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': '<p>@Mike: Thanks for the detailed analysis. There are definately some interesting points you make there. The example I posted is somewhat trivial but the basis of the question came from using NHibernate.</p>\n\n<p>With NHibernate, you can write a clause like this:</p>\n\n<pre><code>int[] employeeIds = new int[]{1, 5, 23463, 32523};\nNHibernateSession.CreateCriteria(typeof(Employee))\n.Add(Restrictions.InG("EmployeeId",employeeIds))\n</code></pre>\n\n<p>NHibernate then generates a query which looks like  </p>\n\n<pre><code>select * from employee where employeeid in (1, 5, 23463, 32523)\n</code></pre>\n\n<p>So as you and others have pointed out, it looks like there are going to be times where an index will be used or a table scan will happen, but you can\'t really determine that until runtime.</p>\n'}, {'answer_id': 44158454, 'author': 'Tushar Rmesh Saindane', 'author_id': 7034587, 'author_profile': 'https://Stackoverflow.com/users/7034587', 'pm_score': 0, 'selected': False, 'text': '<pre><code>Select EmployeeId From Employee USE(INDEX(EmployeeTypeId))\n</code></pre>\n\n<p>This query will search using the index you have created. It works for me. Please do a try.. </p>\n'}]
['https://Stackoverflow.com/questions/31500', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/493/']