Which of the two queries is better ???

  • Hi All,

    I have attached two queries with table definition and sample data.

    Query 2 is working better then query 1.

    Now the actual question is:

    Should I follow query 2 in big and very complex queries?

    Is there any other better way to handle this type of scenarios?

    Thanks.

  • T.Ashish (4/4/2013)


    Hi All,

    I have attached two queries with table definition and sample data.

    Query 2 is working better then query 1.

    Now the actual question is:

    Should I follow query 2 in big and very complex queries?

    Is there any other better way to handle this type of scenarios?

    Thanks.

    What is the code supposed to be doing?

    Hard to tell you anything when we don't what it is you are trying to accomplish.

    In the first query the CTE selects one record then cross joins it to one hundred records from another table. The second query selects the top 1 record based on a value and includes it with 100 records from another table.

    In both queries there is no apparent relation between the two tables. And also, the value you are using in your queries doesn't exist in your data.

  • This query is part of a very big and complicated query where we have more than 60 such small queries.

    All these queries are fatching value from different tables. And together these queries are killing the performance.

    My plan is to use WITH to collect all values and then pass them in main query. I hope, this is going to improve the performance as it is showing in attached example.

    I have deleted few rows from insert query to reduced the size of attachment, thats why you are not getting the value. You can put any value from SITE_AC table.

  • I agree with the other posters that it's not quite clear what you are trying to achieve, but I think a simple CROSS APPLY will give you the result you want (at least it gives me the same result set as your queries).

    SELECT TOP 100

    c.comp_id,

    c.sup_code,

    c.sup_name,

    c.sup_ac_code,

    s.site_id AS ac_id1

    FROM

    COMP_AC c

    CROSS APPLY

    SITE_AC s

    WHERE

    s.site_id = '0a1903120217062077241419'

    ORDER BY

    c.comp_id

     

  • As I mentioned earlier, This query is part of a very big and complicated query where we have more than 60 such small queries.

    My requirement is to remove all these sub queries and use WITH clause to fatch all values in the begining of query and then pass all these values in main query. I hope it is going to Improve the performance.

    (Same thing we can do in stored procedure by fatching all values in variables but I can't use SP)

    I need to know, what are other possible way outs for the same issue.

  • Then it's basically:

    WITH

    cte1

    AS

    (

    SELECT * FROM Blah1

    ),

    cte2

    AS

    (

    SELECT * FROM Blah2

    ),

    cte3

    AS

    (

    SELECT * FROM Blah3

    ),

    cte4

    AS

    (

    SELECT * FROM Fubar

    WHERE ID = cte2.ID -- notice that you can refer to earlier queries as you go down.

    )

    SELECT

    *

    FROM

    cte1

    INNER JOIN cte2

    ON cte1.ID = cte2.ID

    ETC.

    The WITH clause creates virtual tables that you can join together to your hearts content. Also, a great feature is that you can use data from one cte in another that follows it. I'm sure there's some upper limit to how many with clauses you can chain/nest together but I've never run into that barrier. If you do, let us know.

     

  • Thanks Steven.

  • Post the actual execution plan of the "Big Query" as a .sqlplan attachment.

    “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

  • This would do the same:

    SELECT

    TOP 100

    comp_id, sup_code, sup_name, sup_ac_code, '0a1705011715553107773972'

    FROM

    comp_ac

    ORDER BY

    comp_id

    It does not seem to make any sense to supply a parameter value to a query to get exactly the same value back.

    There must be something missing from your description.

    _____________
    Code for TallyGenerator

  • T.Ashish (4/4/2013)


    Hi All,

    I have attached two queries with table definition and sample data.

    Query 2 is working better then query 1.

    Now the actual question is:

    Should I follow query 2 in big and very complex queries?

    Is there any other better way to handle this type of scenarios?

    Thanks.

    Where does the hardcoded value '0a1705011715553107773972' come from?

    “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

  • Hard coded value is coming from SITE_AC table. Now it is not there because I had removed some rows from script to reduce the size of attachment. you can put any value.

  • T.Ashish (4/10/2013)


    Hard coded value is coming from SITE_AC table. Now it is not there because I had removed some rows from script to reduce the size of attachment. you can put any value.

    When you run your Big Query, do you have a WHERE clause as you do in your sample? Like this:

    WHERE site_id =

    If so, where does the value ('0a1705011715553107773972') come from? Is it a parameter?

    “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

  • It comes from Java Code, like ......where Site_id = :lablesiteid....

  • T.Ashish (4/10/2013)


    It comes from Java Code, like ......where Site_id = :lablesiteid....

    -- Query 2

    SELECT

    TOP 100 comp_id,

    sup_code,

    sup_name,

    sup_ac_code,(

    SELECT

    top 1 site_id

    FROM

    site_ac

    WHERE

    site_id = '0a1705011715553107773972'

    ) AS z

    FROM

    comp_ac

    ORDER BY

    comp_id

    -- why not just do this?

    SELECT

    TOP 100 comp_id,

    sup_code,

    sup_name,

    sup_ac_code,

    '0a1705011715553107773972' AS z

    FROM

    comp_ac

    ORDER BY

    comp_id

    “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

Viewing 14 posts - 1 through 13 (of 13 total)

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