I was improving some sql query script's performance. For example:
SELECT *
FROM Book b, Library l, [Order] o
WHERE o.bookid = b.bookID
AND o.mode = 'A'
AND o.library_ID = l.library_ID
AND l.library_ID > 19
AND b.publisher_id > 1000
AND b.print_id > 800
AND NOT EXISTS (
SELECT *
FROM ExtBOOK
WHERE b.bookid = extbookid
AND library_ID = l.library_ID
)
AND o.activated = 'Y'
AND b.eisbn13 LIKE '978%'
AND len(o.ext_user_id) > 3
AND b.bookid > 200000
AND b.bookid in (
SELECT bookid
FROM category
WHERE categoryid > 2
)
ORDER BY o.orderid DESC
When I search this sql script in SQL Management Studio with "Include Actual Execution Plan", the result ask me to add below index
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[Order] ([MODE],[ACTIVATED],[LIBRARY_ID],[BOOKID])
INCLUDE ([OrderID],[EXT_USER_ID],[APPROVAL_DATE])
Howevery if I delete some where conditions, like below:
SELECT * FROM Book b, Library l, [Order] o
WHERE o.bookid = b.bookID
AND o.mode = 'A'
AND o.library_ID = l.library_ID
AND l.library_ID > 19
ORDER BY o.orderid DESC
I got another different recommendation, like below:
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[ORDER] ([MODE])
INCLUDE ([LIBRARY_ID],[BOOKID])
Because the conditions are variable, which index should I create? I understand what indexes do, but not the benefit between a field being indexed and a field being included. Why in the first recommended index, BOOKID and LIBRARY_ID are in the index fields, but in the second recommended index, BOOKID and LIBRARY_ID are in the included fields? What are the differences, and which I should I use to cover all possible conditions?
In addition, from my test, I added each of them to test the performance, but cannot see any difference. I appreciate any help.
WHERE
clause. Included columns are only present in the leaf level of the index - you cannot use them to select rows - and they're there so your query can be satisfied (all columns requested by the query) just from the index structure so that you don't have to do an expensive key lookup into the actual table data to get that one or two extra columns that you need. – marc_sorder by
, or I have to use index columns toorder by
, ororder by
does not need index. – danmiaoWHERE
or anORDER BY
clause, it must be part of the actual index and stored in the index navigation structure. If you have a column as "included" column, the index cannot be used forORDER BY
– marc_s