SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Which of the two queries is better ???


Which of the two queries is better ???

Author
Message
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 599
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.
Attachments
query_table_data.doc (27 views, 50.00 KB)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39714 Visits: 38562
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.

Cool
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)
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 599
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.
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)

Group: General Forum Members
Points: 841 Visits: 1721
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





 
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 599
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.
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)SSC Eights! (841 reputation)

Group: General Forum Members
Points: 841 Visits: 1721
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.

 
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 599
Thanks Steven.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16394 Visits: 19554
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
Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10472 Visits: 11963
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.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16394 Visits: 19554
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search