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 12»»

Which of the two queries is better ??? Expand / Collapse
Author
Message
Posted Thursday, April 4, 2013 6:41 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: Yesterday @ 2:58 AM
Points: 718, Visits: 543
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.


  Post Attachments 
query_table_data.doc (24 views, 50.00 KB)
Post #1438755
Posted Thursday, April 4, 2013 7:02 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:50 PM
Points: 23,045, Visits: 31,570
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1438773
Posted Thursday, April 4, 2013 9:34 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: Yesterday @ 2:58 AM
Points: 718, Visits: 543
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.
Post #1438865
Posted Thursday, April 4, 2013 12:53 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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



 
Post #1438987
Posted Thursday, April 4, 2013 9:32 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: Yesterday @ 2:58 AM
Points: 718, Visits: 543
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.
Post #1439078
Posted Thursday, April 4, 2013 10:14 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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.

 
Post #1439083
Posted Thursday, April 4, 2013 10:39 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: Yesterday @ 2:58 AM
Points: 718, Visits: 543
Thanks Steven.
Post #1439091
Posted Friday, April 5, 2013 1:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:37 AM
Points: 7,127, Visits: 13,504
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1439118
Posted Tuesday, April 9, 2013 11:28 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 4:39 PM
Points: 4,576, Visits: 8,342
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.
Post #1440654
Posted Wednesday, April 10, 2013 1:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:37 AM
Points: 7,127, Visits: 13,504
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1440682
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse