SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Slow Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

RichardSteele
Posting Yak Master

158 Posts

Posted - 05/09/2012 :  12:25:15  Show Profile  Reply with Quote
The following query takes 25-30 seconds to return all results.

Here's the query:
SELECT d.StatDetailID, d.StatID, d.DayViewedOn, d.Hits
FROM VS3_Stats AS s INNER JOIN
VS3_Stats_Detail AS d ON s.StatID = d.StatID
WHERE (s.CustNumber = 10221)

VS3_Stats has a nonclustered index on Custnumber.
VS3_Stats_Detail has a nonclustered index on StatID.
There are 6 million records in VS3_Stats_Detail.

Should it take 25-30 seconds to pull the results? If so, is there a better way to make this much faster? Thanks.

RL
Starting Member

USA
15 Posts

Posted - 05/09/2012 :  14:24:05  Show Profile  Reply with Quote
There are multiple issues to take into account. For one thing, other indexes defined on these tables, including primary key indexes, can make a difference. Redundant indexes or the wrong indexes can sometimes hurt query performance. Also, the data distribution (i.e. statistics) can make a difference when you're dealing with millions of rows. So I can't say for sure what will work.

To check this out, I created the two tables with only the columns you listed, and loaded VS3_Stat with 500 rows, and VS3_Stat_Details with 50000 rows. When I checked the estimated execution plan in SSMS, it listed a missing index (lots of info on line about this).

The following results may not be relevant for your situation because the data distribution/statistics are different, but it can still be instructive to look at the relative costs for various indexes:

SELECT d.StatID, d.StatDetailID, d.DayViewedOn, d.Hits
FROM VS3_Stats AS s INNER JOIN VS3_Stats_Detail AS d 
     ON s.StatID = d.StatID 
WHERE (s.CustNumber = 10221)

-- WITH YOUR INDEXES : .60088   -- table scans on both tables
-- WITH MISSING INDEX: .24997   -- index scan on VS3_Stats_Detail

-- Definition of missing index
CREATE INDEX idx01 ON VS3_Stats_Detail (StatID) 
INCLUDE (StatDetailID, DayViewedOn, Hits);

With my data and the recommended index, the estimated execution plan cost was LESS THAN HALF with the recommended index.

NOTES:
1. If you're running SQL2000, with no INCLUDE option, create the index with all columns in the above index.
2. Adding indexes to a big table can slow down load/update procedures.
3. Test this with tables containing subsets of the actual data before putting it into production.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000