Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Out of the two given queries, which will perform faster!!! Expand / Collapse
Author
Message
Posted Thursday, May 2, 2013 7:02 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, November 3, 2014 1:52 AM
Points: 720, Visits: 553
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

Post #1448796
Posted Thursday, May 2, 2013 7:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:08 PM
Points: 12,927, Visits: 32,333
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1448797
Posted Thursday, May 2, 2013 10:09 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 9:01 AM
Points: 4,439, Visits: 6,347
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 at GMail
Post #1448894
Posted Thursday, May 2, 2013 11:20 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, November 3, 2014 1:52 AM
Points: 720, Visits: 553
Yes, I also feel second query should be slower. But both are working exactly same.
Post #1449051
Posted Friday, May 3, 2013 3:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:52 AM
Points: 6,872, Visits: 14,185
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?


“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
Post #1449090
Posted Friday, May 3, 2013 3:38 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 8:31 AM
Points: 40,456, Visits: 36,912
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 2008, MVP
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

Post #1449101
Posted Friday, May 3, 2013 6:30 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, November 3, 2014 1:52 AM
Points: 720, Visits: 553
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 .....
Post #1449164
Posted Friday, May 3, 2013 6:35 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:08 PM
Points: 12,927, Visits: 32,333

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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1449166
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse