Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

optimize the schema definition Expand / Collapse
Author
Message
Posted Wednesday, May 15, 2013 12:38 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 21, 2014 3:20 AM
Points: 432, Visits: 1,073
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);

Post #1452936
Posted Wednesday, May 15, 2013 1:39 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:03 AM
Points: 40,425, Visits: 36,873
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1452954
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse