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


Need to optimize/Rewrite this query..


Need to optimize/Rewrite this query..

Author
Message
Sri8143
Sri8143
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1143 Visits: 2469
Implement a function with below logic in an efficient way ...Pass @var1 and return @var2 ...How can it be done as it does table scan now ??

DECLARE @Var1 VARCHAR(10)
DECLARE @Var2 VARCHAR(100)

SELECT @var2= var2 FROM #test
WHERE var1 = @Var1



Thanks in advance

Thanks,
Chinna

Its the Journey which gives you Happiness not the Destination-- Dan Millman
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51454 Visits: 21158
Sri8143 (7/5/2013)
Implement a function with below logic in an efficient way ...Pass @var1 and return @var2 ...How can it be done as it does table scan now ??

DECLARE @Var1 VARCHAR(10)
DECLARE @Var2 VARCHAR(100)

SELECT @var2= var2 FROM #test
WHERE var1 = @Var1



Thanks in advance


Try creating an index on your temp table before running that query - something like this:

create nonclustered index ix_test_var1 on #test(var1) include(var2)




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41345 Visits: 20003
ALTER FUNCTION [dbo].[IF_GetVar2]
(@Var1 VARCHAR(10))
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
SELECT var2
FROM test
WHERE var1 = @Var1
;
GO

-- Testing:
SELECT var2 FROM IF_GetVar2('Somevalue')
-- Examine the execution plan and create a new index
-- on table 'test' to support seeks if necessary - as in Phil's post.



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Sri8143
Sri8143
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1143 Visits: 2469
Thanks Guys for the response.. But just want to know if there is a workaround for this issue using crossapply ?

AS it is looping through all the records now ..

Thanks,
Chinna

Its the Journey which gives you Happiness not the Destination-- Dan Millman
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41345 Visits: 20003
What do you mean by 'looping'? A table or clustered index scan? You can incorporate an iTVF into a query using APPLY but that won't help at all if there isn't a supporting index for the query.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
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