SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


optimize the schema definition


optimize the schema definition

Author
Message
charipg
charipg
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2248 Visits: 1193
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);
GilaMonster
GilaMonster
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: General Forum Members
Points: 225479 Visits: 46321
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

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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search