Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is multiple aspects to consider:</p> <ul> <li>You do not have an <code>index</code> on the bookName column: in this context that does not matter, the query is going to perform the same way by executing a clustered index scan or a table scan. </li> <li>You do have a <code>non clustered index</code> on the bookName column <ul> <li>this is a <code>non covering index</code> (scenario 1): in the second use case with the <code>OR</code> statement, you are likely to hit the <code>index tipping point</code>, where the query optimizer will decide <strong>not</strong> to use the <code>non clustered index</code>. In this context the <code>LIKE</code> will perform better by using an index scan. Read more on the <a href="http://www.sqlskills.com/BLOGS/KIMBERLY/category/The-Tipping-Point.aspx" rel="nofollow noreferrer">Kimberly Tripp's blog</a>. </li> <li>this is a <code>covering index</code> (scenario 2): there is no tipping point for covering indexes. In this case, the second query will perform drastically better by using an <code>index seek</code> where the <code>LIKE</code> query will still have to do an <code>Index Scan</code>.</li> </ul></li> </ul> <p>So if performance is critical, use a covering index</p> <p>Here are the details, using <a href="http://msftdbprodsamples.codeplex.com/releases/view/93587" rel="nofollow noreferrer">Adventure Works</a></p> <p>Scenario 1</p> <pre><code>SELECT * FROM Production.Product AS p WHERE Name LIKE '%mountain seat%' SELECT * FROM Production.Product AS p WHERE Name = 'LL Mountain Seat Assembly' OR Name = 'ML Mountain Seat Assembly' OR Name = 'HL Mountain Seat Assembly' OR Name = 'LL Mountain Seat/Saddle' OR Name = 'ML Mountain Seat/Saddle' OR Name = 'HL Mountain Seat/Saddle' </code></pre> <p><img src="https://i.stack.imgur.com/CPB1K.png" alt="enter image description here"></p> <p>Scenario 2</p> <pre><code>SELECT Name FROM Production.Product AS p WHERE Name LIKE '%mountain seat%' SELECT Name FROM Production.Product WHERE Name = 'LL Mountain Seat Assembly' OR Name = 'ML Mountain Seat Assembly' OR Name = 'HL Mountain Seat Assembly' OR Name = 'LL Mountain Seat/Saddle' OR Name = 'ML Mountain Seat/Saddle' OR Name = 'HL Mountain Seat/Saddle' </code></pre> <p><img src="https://i.stack.imgur.com/nj3TS.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload