May 2, 2013 at 7:02 am
Out of the two given queries, which will perform faster!!!
1. Query(1)
SELECT
TOP 1000 first_name + ' ' + last_name AS display_name,
person_id,
company_code
FROM
person
2. Query(2)
SELECT
TOP 1000
CASE WHEN
charindex('(', first_name + ' ' + last_name) > 0
THEN
(LEFT (first_name + ' ' + last_name, CHARINDEX('(',first_name + ' ' + last_name) -1))
ELSE
first_name + ' ' + last_name
END
AS display_name,
person_id,
company_code
FROM
person
May 2, 2013 at 7:07 am
they are the same query, but with a some expressions to calculate the field values.
test it yourself by looking at the actual execution plans when you run them.
they will both be table scans (clustered index scan if there is a clustered index) because there is no WHERE statement,
even worse, the TOP 1000 has no ORDER BY, so the results could vary on the very next pass if any data changed, or any reindexing occurred.
TOP does not guarantee repeatable results without an ORDER BY.
Lowell
May 2, 2013 at 10:09 am
You do work in the second query which doesn't happen in the first. I see no reason to expect the second query to not be some (microseconds?) slower than the other.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 2, 2013 at 11:20 pm
Yes, I also feel second query should be slower. But both are working exactly same.
May 3, 2013 at 3:09 am
T.Ashish (5/2/2013)
Yes, I also feel second query should be slower. But both are working exactly same.
How long do they take to run?
What method are you using to measure the execution time?
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
May 3, 2013 at 3:38 am
They should be near-identical in time (and bear in mind that SQL's timers aren't accurate down to the ms level, that's stats time and perfmon). While the second does do more work and will use more CPU, it's likely to be a very small amount compared to the time to retrieve all the data (no filters on those queries)
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
May 3, 2013 at 6:30 am
Yes I agree. Both queries are executing in under 1 sec. so time can't be a measurement factor, may be in a very very big query we can see some difference.
I am finding same issue with DISTINCT and UNION where execution plan, IO and time is exactly identical for these three scenarios (Though query 2 gives different results):
(1)
select DISTINCT .....
UNION
select DISTINCT .....
(2)
select DISTINCT .....
UNION ALL
select DISTINCT .....
(3)
select .....
UNION
select .....
May 3, 2013 at 6:35 am
you really HAVE to compare differnet queries by their execution plans, and not by their perceived speed.
digging into those details are what gives you an understanding of why there are extra sorts or differences between two similar queries.
Lowell
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply