Out of the two given queries, which will perform faster!!!

  • 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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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

  • Yes, I also feel second query should be slower. But both are working exactly same.

  • 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

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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 .....

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply