Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 query enhancement

Author  Topic 

android.sm
Starting Member

36 Posts

Posted - 2011-07-12 : 19:04:10
Anything I can change to improve these queries. time to execute - around 4mins, for about 6m records. thanks


SELECT dbo.ContactBase.New_Title, dbo.Contact.FirstName, dbo.Contact.LastName, dbo.Contact.MiddleName,
dbo.Contact.MobilePhone, dbo.CustomerAddress.Telephone1, dbo.Contact.BirthDate, dbo.Contact.GenderCode,
dbo.Contact.FamilyStatus, dbo.CustomerAddress.Line1, dbo.CustomerAddress.Line2, dbo.CustomerAddress.Line3,
dbo.CustomerAddress.City, dbo.CustomerAddress.County, dbo.CustomerAddress.PostalCode,
dbo.CustomerAddress.Country, dbo.Contact.ContactId

FROM dbo.CustomerAddress INNER JOIN
dbo.Contact INNER JOIN
dbo.ContactBase ON dbo.Contact.ContactId = dbo.ContactBase.ContactId ON
dbo.CustomerAddress.ParentId = dbo.Contact.ContactId
WHERE (dbo.CustomerAddress.AddressNumber = 1)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-12 : 19:19:13
What indexes do you have on these tables? How many rows is it returning?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

android.sm
Starting Member

36 Posts

Posted - 2011-07-13 : 05:15:36
Hey,

There is index on CustomerAddress.

Returning about 6million rows.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-13 : 12:41:45
You can't expect a query to be fast if it is going to return 6 million rows. Add conditions to restrict the number of rows it will return. No one can possibly process that many rows for this to even be needed.

You need to add a bunch of indexes to support this query even after you fix the 6 million rows issue.

You need these:

Contact.ContactId
ContactBase.ContactId
CustomerAddress.ParentId
CustomerAddress.AddressNumber

And you need to consider making a covering index for CustomerAddress table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -