You are here:   Forums
Register   |  Login

Forum

 
ForumForumSQL Server 2008...SQL Server 2008...SQL Server Perf...SQL Server Perf...Optimized query & its reasoningOptimized query & its reasoning
Previous Previous
 
Next
 Disabled
New Post
 11/30/2009 3:05 PM
 
 Modified By kanupriya  on 11/30/2009 2:36:53 AM


SELECT DISTINCT Customers.CompanyName AS Customer, Products.ProductName, Categories.CategoryName, [OrderDetails].Quantity, Suppliers.CompanyName AS Supplier, Shippers.CompanyName AS Shipper,
Employees.FirstName AS Employee, Region.RegionDescription
FROM Categories INNER JOIN
Products WITH (INDEX(PK_Products)) ON Categories.CategoryID = Products.CategoryID INNER JOIN
EmployeeTerritories INNER JOIN
Employees WITH (INDEX(PK_Employees)) ON EmployeeTerritories.EmployeeID = Employees.EmployeeID INNER JOIN
Orders ON Employees.EmployeeID = Orders.EmployeeID INNER JOIN
Customers WITH (INDEX(PK_Customers)) ON Orders.CustomerID = Customers.CustomerID INNER JOIN
[Order Details] WITH (INDEX(PK_Order_Details)) ON Orders.OrderID = [Order Details].OrderID
ON Products.ProductID = [Order Details].ProductID INNER JOIN
Shippers WITH (INDEX(PK_Shippers)) ON Orders.ShipVia = Shippers.ShipperID INNER JOIN
Suppliers WITH (INDEX(PK_Suppliers)) ON Products.SupplierID = Suppliers.SupplierID INNER JOIN
Territories WITH (INDEX(PK_Territories)) ON EmployeeTerritories.TerritoryID = Territories.TerritoryID INNER JOIN
Region WITH (INDEX(PK_Region)) ON Territories.RegionID = Region.RegionID
WHERE (Customers.CompanyName > N'D') AND (Region.RegionDescription <> N'Western')
ORDER BY Customer, ProductName

The above query is on Northwind database. My requirement is that I want an optimized scenario for the above case with a proper reasoning for the approach taken.

New Post
 11/30/2009 7:00 PM
 
 Modified By Hemantgiri Goswami  on 11/30/2009 6:33:05 AM

Hi,

Welcome to the forums!

If you add below code to your query you can avoid Key Lookup which is occupying 48% cost, this way you will be able to reduce the query cost . Here is the link where in you would be able read and understand what the Key Lookup is http://statisticsio.com/Home/tabid/36... exists (select orders.OrderID from Orders)

I still have to check the code you've emailed me. I'll be able to reply back to you by tomorrow eod.

AND


Who'sOnline

Membership Membership:
Latest New User Latest: Matang
Past 24 Hours Past 24 Hours: 0
Prev. 24 Hours Prev. 24 Hours: 0
User Count Overall: 110

People Online People Online:
Visitors Visitors: 2
Members Members: 0
Total Total: 2

Online Now Online Now:

Spread the Word

Bookmark and Share

);