May 15, 2013 at 12:38 am
1. How would i optimize the following schema definition?
CREATE TABLE dbo.Orders (
RowID INT IDENTITY NOT NULL,
OrderID INT
PRIMARY KEY NONCLUSTERED,
OrderTypeNVARCHAR(20) NOT NULL,
OrderDateTime DATETIME NOT NULL
);
CREATE CLUSTERED INDEX ix_cls_Orders_RowID ON dbo.Orders (RowID);
CREATE NONCLUSTERED INDEX ix_Orders_OrderDateTime ON dbo.Orders (OrderDateTime);
CREATE TABLE dbo.LineItems (
LineItemID INT IDENTITY
PRIMARY KEY,
OrderID INT NOT NULL
FOREIGN KEY REFERENCES dbo.Orders (OrderID),
LineItemTypeNVARCHAR(20) NOT NULL,
RegisterID INT NOT NULL,
LineItemCustomValue1 NVARCHAR(20) NULL,
LineItemCustomValue2 NVARCHAR(20) NULL,
LineItemCustomValue3 NVARCHAR(20) NULL,
LineItemCustomValue4 NVARCHAR(20) NULL,
LineItemCustomValue5 NVARCHAR(20) NULL,
LineItemCustomValue6 NVARCHAR(20) NULL,
LineItemCustomValue7 NVARCHAR(20) NULL,
LineItemCustomValue8 NVARCHAR(20) NULL,
LineItemCustomValue9 NVARCHAR(20) NULL,
LineItemCustomValue10 NVARCHAR(20) NULL
);
CREATE NONCLUSTERED INDEX ix_LineItems_OrderID ON dbo.LineItems (OrderID);
2.The following query, which utilizes the schema defined above, performs poorly. How would i optimize it?
SELECT
o.OrderDateTime,
o.OrderType,
li.OrderID,
li.RegisterID,
li.LineItemCustomValue3,
li.LineItemCustomValue4
FROM dbo.Orders o WITH (INDEX(ix_Order_OrderDateTime))
JOIN dbo.LineItems li
ON o.OrderID = li.OrderID
WHERE
li.LineItemCustomValue3 = 'CouponPurchase'
AND o.OrderDateTime>= DATEADD(dd,-1,CURRENT_TIMESTAMP);
May 15, 2013 at 1:39 am
You don't really optimise schemas, so not sure what you're asking. The Line Items table needs either normalisation or sensible column names, can't tell which.
As for the query, you can start by taking out the index hint. Hints should be used with caution and only when you're absolutely sure that the hint improves performance, you know exactly why the hint is better than what the optimiser came up with and you've tested extensively.
Once done, please post the query's execution plan.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply