The next comment I have concerns your derived table query; specifically the where clause.
Code:
SELECT DISTINCT Ord_no AS Ord_no,
Shipment AS Shipment
from wspikpak
where shipped='Y'
AND (DATEDIFF([day], ship_dt, GETDATE()) = 0)
If I had to guess, I would say that shipped is a status field, with very few statuses available. There may be Y, N, H, etc.. but probably not more than a dozen statuses. As such, this is not a very good column to have an index on because of the selectivity of the index. I don't really have a problem with this part.
The problem I have is with the 2nd condition on ship_dt. There is a concept that you should be aware of when writing queries called SARGABLE.
Basically, you can have multiple indexes on a table. Depending on your query, SQL Server may use an index to speed up certain operations. When you use a function on a column, like you are doing here with the DateDiff function, SQL Server is unable to use an index to speed up the query. Essentially, SQL Server will have to scan every row in this table and apply the function to the data in the row. It would be relatively simple to re-write the query in a way that SQL Server would be able to use an index (if it exists) on the table.
What I would like you to do is run the following queries in a SQL Server Management Studio query window.
Code:
Select * From sys.sysobjects where id = 20
Select * From sys.sysobjects where id + 0 = 20
On your keyboard, press CTRL-M, and then F5 to run the query. Each query will return the exact same data. You should see an "Execution Plan" tab within SQL Server Management Studio. When you click on the execution tab, you will see both queries and a graphical display showing the steps that the query optimizer uses to execute the query. When I run this, I see
[tt]
Query 1: Query cost (relative to the batch): 11%
Query 2: Query cost (relative to the batch): 89%
[/tt]
The percentages you see will add up to 100. But, the important part here is to notice that the first query took 11% of the total time and the second took 89%. Since both queries return the same data, it is obviously better to use the one that is faster, right?
So... how do we re-write your query so that it can effectively use an index? Like this:
Code:
SELECT DISTINCT Ord_no AS Ord_no,
Shipment AS Shipment
from wspikpak
where shipped='Y'
AND ship_dt = DateAdd(Day, DateDiff(Day, 0, GetDate()), 0)
If you run both versions of this code within the same query window and then look at the execution plan, you should see that the new one executes faster. If it doesn't, then there is probably not an index that has ship_dt as the first column.
Please note that you may not notice a difference in performance. It all depends on the indexes that are available and the amount of data in the table. With small tables (few number or rows), everything is going to be fast.
Make sense?
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom