query optimization
-
Friday, February 01, 2013 11:26 AM
I have a code in a SP as shown below(did not mention all the columns here,as there are many).Currently it is taking more time to execute.I am requested to check if i can optimize the code bit.Can any one see and let me know your thoughts.
CREATE TABLE #temp(
[Source] [varchar](10),
[Currency] [varchar](3),)
-- Add records into temp table
INSERT INTO #temp
Select Source, Currency
From dbo.Fact Where Key = 1Insert Into Fact ( Source, Currency)
SelectPrev.Source,
Prev.CurrencyFrom
(Select Source, Currency) Prev
Left Join
#temp Curr
On (
Curr.Source = Prev.Source And
Curr.Currency = Prev.Currency And
)
Where
Curr.Source Is Null
All Replies
-
Friday, February 01, 2013 11:40 AM
Instead of using temp table you can directly do it in the query..
Insert Into Fact ( Source, Currency) Select Prev.Source, Prev.Currency From (Select Source, Currency) Prev Left Join (select Source, Currency from dbo.Fact Where Key = 1) Curr On ( Curr.Source = Prev.Source And Curr.Currency = Prev.Currency And ) Where Curr.Source Is Null
sarat chandra sahoo
-
Friday, February 01, 2013 12:09 PM
I think ditching the temp table like Ashwin AC suggested is a good first step. But the query you posted has is not correct in that there is no FROM clause for the Prev source, which may be an important detail to correctly answer your question. You might refactore to use NOT EXISTS instead. For performance, it is important to have a composite index on Source and Currency on these tables (ideally clustered and unique).
Dan Guzman, SQL Server MVP, http://www.dbdelta.com
- Proposed As Answer by Kalman TothMicrosoft Community Contributor, Moderator Friday, February 01, 2013 1:59 PM
-
Friday, February 01, 2013 2:00 PMModerator
It is helpful if you post script & DDL so we can test.
The following article is on optimization:
http://www.sqlusa.com/articles/query-optimization/
Kalman Toth SQL 2008 GRAND SLAM
Paperback/Kindle: SQL Server 2012 Pro- Edited by Kalman TothMicrosoft Community Contributor, Moderator Monday, February 04, 2013 12:58 PM
-
Monday, February 04, 2013 12:49 PM
Hi Dan Guzman,
Thanks for the reply.The inner query uses another table (say table2).
CREATE TABLE #temp(
[Source] [varchar](10),
[Currency] [varchar](3),)
-- Add records into temp table
INSERT INTO #temp
Select Source, Currency
From dbo.Fact Where Key = 1Insert Into Fact ( Source, Currency)
SelectPrev.Source,
Prev.CurrencyFrom
<input id="35eebc08-2977-4d9c-8020-7e2e30a6a394_attachments" type="hidden" />
(Select Source, Currency from table2) Prev
Left Join
#temp Curr
On (
Curr.Source = Prev.Source And
Curr.Currency = Prev.Currency And
)
Where
Curr.Source Is Null -
Tuesday, February 05, 2013 12:14 PM
Below are a couple of methods without the temp table. A composite (unique?) index on dbo.Fact columns Key, Source and Currency will help optimize these queries.
INSERT INTO dbo.Fact ( Source, Currency ) SELECT Prev.Source ,Prev.Currency FROM dbo.table2 AS Prev LEFT JOIN (SELECT Source, Currency FROM dbo.Fact WHERE [Key] = 1) AS Curr ON Curr.Source = Prev.Source AND Curr.Currency = Prev.Currency WHERE Curr.Source IS NULL; INSERT INTO dbo.Fact ( Source, Currency ) SELECT Prev.Source ,Prev.Currency FROM dbo.table2 AS Prev WHERE NOT EXISTS( SELECT * FROM dbo.Fact AS Curr WHERE [Key] = 1 AND Curr.Source = Prev.Source AND Curr.Currency = Prev.Currency );Dan Guzman, SQL Server MVP, http://www.dbdelta.com
- Proposed As Answer by Naomi NMicrosoft Community Contributor, Moderator Wednesday, February 06, 2013 2:40 AM
- Marked As Answer by Iric WenModerator Monday, February 11, 2013 4:26 AM
-
Tuesday, February 05, 2013 2:09 PM1) Select Source, Currency
From dbo.Fact Where Key = 1
Here I would try to use filtered Index
2) In fact I would try to use IndexedView instead of this code
Execution plan should be investigated

