September 9, 2009 at 4:31 am
select ltrim(rtrim(col1)) from tab
where col2 = 12
or
select col1 from tab
where col2 = 12
clustered index is present on col2 and non-clustered on col1
which query is more optimal.
September 9, 2009 at 4:37 am
the easiest thing to do is to look at the query plan. if you need help with that, just attach them here
--------------------------------------------------------------------------------------
[highlight]Recommended Articles on How to help us help you and[/highlight]
[highlight]solve commonly asked questions[/highlight]
Forum Etiquette: How to post data/code on a forum to get the best help by Jeff Moden[/url]
Managing Transaction Logs by Gail Shaw[/url]
How to post Performance problems by Gail Shaw[/url]
Help, my database is corrupt. Now what? by Gail Shaw[/url]
September 9, 2009 at 4:45 am
You will notice that the second approach is faster when you are running this query for selecting a larger recordset.
Thanks
Suresh Ramamurthy
September 9, 2009 at 4:45 am
ramuvanparti (9/9/2009)
select ltrim(rtrim(col1)) from tabwhere col2 = 12
or
select col1 from tab
where col2 = 12
clustered index is present on col2 and non-clustered on col1
which query is more optimal.
Both are equal as clustered index seek will happen.
ltrim(rtrim(col1)) will not make much difference.
September 9, 2009 at 4:46 am
please find the attached
September 9, 2009 at 4:58 am
looking at them now
--------------------------------------------------------------------------------------
[highlight]Recommended Articles on How to help us help you and[/highlight]
[highlight]solve commonly asked questions[/highlight]
Forum Etiquette: How to post data/code on a forum to get the best help by Jeff Moden[/url]
Managing Transaction Logs by Gail Shaw[/url]
How to post Performance problems by Gail Shaw[/url]
Help, my database is corrupt. Now what? by Gail Shaw[/url]
September 9, 2009 at 5:00 am
Then trim version is longer by an estimated 0.0000001 cpu cost.
Whats your real issue ?
September 9, 2009 at 5:03 am
Both of them use the clustered index, and both use a clustered index seek.
execution plan2 is marginally faster and doesnt have a separate operation for Compute Scalar.
So not much between them but query 2 would be better. If you had to make the distinction.
--------------------------------------------------------------------------------------
[highlight]Recommended Articles on How to help us help you and[/highlight]
[highlight]solve commonly asked questions[/highlight]
Forum Etiquette: How to post data/code on a forum to get the best help by Jeff Moden[/url]
Managing Transaction Logs by Gail Shaw[/url]
How to post Performance problems by Gail Shaw[/url]
Help, my database is corrupt. Now what? by Gail Shaw[/url]
September 9, 2009 at 5:10 am
Silverfox (9/9/2009)
If you had to make the distinction
Although IMO , its better to base the distinction on functional need. 🙂
If you NEED to trim trailing and leading spaces , then use ltrim/rtrim , if you dont , then dont.
September 9, 2009 at 5:13 am
Dave Ballantyne (9/9/2009)
Silverfox (9/9/2009)
If you had to make the distinctionAlthough IMO , its better to base the distinction on functional need. 🙂
If you NEED to trim trailing and leading spaces , then use ltrim/rtrim , if you dont , then dont.
Only answering the question that was asked 😛
--------------------------------------------------------------------------------------
[highlight]Recommended Articles on How to help us help you and[/highlight]
[highlight]solve commonly asked questions[/highlight]
Forum Etiquette: How to post data/code on a forum to get the best help by Jeff Moden[/url]
Managing Transaction Logs by Gail Shaw[/url]
How to post Performance problems by Gail Shaw[/url]
Help, my database is corrupt. Now what? by Gail Shaw[/url]
September 9, 2009 at 5:29 am
Both queries are same so the plan should be same .But since you are trimming the spaces it will add some overhead .That extra overhead will be in the form of compute Scalar which will be at the cost of CPU.
Regards
Abhay Chaudhary
Sr.DBA (MCITP/MCTS :SQL Server 2005/2008 ,OCP 9i)
September 9, 2009 at 5:44 am
from this i came to conclusion that if i have more than 1 million record and use first query it will cost more compared to second one 🙂
thanks for the reply
September 9, 2009 at 10:27 pm
ramuvanparti (9/9/2009)
select ltrim(rtrim(col1)) from tabwhere col2 = 12
or
select col1 from tab
where col2 = 12
clustered index is present on col2 and non-clustered on col1
which query is more optimal.
I'd recommend fixing the underlying problem of storing leading and trailing spaces. Do an UPDATE on the column to remove both so you don't have to worry about such things nor the impact that leading spaces has on JOIN criteria.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2009 at 3:04 am
ya you are right, if there leading and trailing spaces it would more efficient and overhead of trim function usage is not required
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply