sqldba2013
asked on
SQL Query tuning
The attached query is taking around 1 hr 20 min time to return 20,000 records and I have created index but still it was taking more time to give output.
In execution plan, stream aggregate (aggregate) cost is showing 71%. Please suggest how to reduce the attached query execution time (rewrite Joins, index creation..if any other options).
Query
In execution plan, stream aggregate (aggregate) cost is showing 71%. Please suggest how to reduce the attached query execution time (rewrite Joins, index creation..if any other options).
Query
ASKER
I have tried with temp table but i didn't get any improvement in execution time.
How to reduce "stream aggregate (aggregate) " cost?
How to reduce "stream aggregate (aggregate) " cost?
I don't see any query plan, so I can't follow what you're saying.
But, for any query, I can tell you that such a massive GROUP BY will destroy your performance.
But, for any query, I can tell you that such a massive GROUP BY will destroy your performance.
ASKER
Please find the attached query execution plan
execution-plan.sqlplan
execution-plan.sqlplan
There are 2 correlated subqueries in the select clause.
Both use "TOP 1", removing the TOP 1 would help immediately.
Note as it stands these correlated subqueries are causing substantial "rebinds" and the top 1 is causing very expensive sorts.
Replacing both these inefficient subquery types with more efficient join (perhaps as subqueries) is a top prioriity.
-------------
These joins involving functions are inefficient. Is all that processing absolutely necessary?
LEFT JOIN [BCAMDB].dbo.CPOA CPON WITH (NOLOCK)
ON REPLACE(REPLACE(CPON.Inqui ries, '[', ''), ']', '') = PRA.inq_num -- *1
LEFT JOIN [BCAMDB].dbo.BI_CUSTMON CUST WITH (NOLOCK)
ON CPON.[PO Number] = SUBSTRING(CUST.[Customer PO], 1, LEN(CPON.[PO Number])) --*2
Ideally there is no need for such manipulation in joining tables.
If you cannot get rid of those manipulations can you minimize them?
*1
REACT.dbo.ps_requests_asr PRA appears to be the smaller of the 2 tables
could you add '[' & ']' to PRA.inq_num istead?
LEFT JOIN [BCAMDB].dbo.CPOA CPON WITH (NOLOCK)
ON RCPON.Inquiries = '[' + PRA.inq_num + ']'
*2
LEFT JOIN [BCAMDB].dbo.BI_CUSTMON CUST WITH (NOLOCK)
ON CUST.[Customer PO] LIKE CPON.[PO Number] + '%'
-----------
this line:
AND ISNULL(PRA.inq_num, '') <> ''
I think should be:
WHERE PRA.inq_num IS NOT NULL
right now it is being handled as part of a left join, and that join appears redundant (next)
-------------
You join a table but then do not use it in select, group or order clauses
LEFT JOIN [BCAMDB].dbo.SAP_ZVBRP BRP WITH (NOLOCK)
ON INV.SO = BRP.SO
-----------
basically:
You have to find a better way to arrive at those summed values.
Only you will know enough about the data to know if you can make those joins more efficient - but I would be trying to ensure I had better ways to join the tables.
Both use "TOP 1", removing the TOP 1 would help immediately.
Note as it stands these correlated subqueries are causing substantial "rebinds" and the top 1 is causing very expensive sorts.
Replacing both these inefficient subquery types with more efficient join (perhaps as subqueries) is a top prioriity.
-------------
These joins involving functions are inefficient. Is all that processing absolutely necessary?
LEFT JOIN [BCAMDB].dbo.CPOA CPON WITH (NOLOCK)
ON REPLACE(REPLACE(CPON.Inqui
LEFT JOIN [BCAMDB].dbo.BI_CUSTMON CUST WITH (NOLOCK)
ON CPON.[PO Number] = SUBSTRING(CUST.[Customer PO], 1, LEN(CPON.[PO Number])) --*2
Ideally there is no need for such manipulation in joining tables.
If you cannot get rid of those manipulations can you minimize them?
*1
REACT.dbo.ps_requests_asr PRA appears to be the smaller of the 2 tables
could you add '[' & ']' to PRA.inq_num istead?
LEFT JOIN [BCAMDB].dbo.CPOA CPON WITH (NOLOCK)
ON RCPON.Inquiries = '[' + PRA.inq_num + ']'
*2
LEFT JOIN [BCAMDB].dbo.BI_CUSTMON CUST WITH (NOLOCK)
ON CUST.[Customer PO] LIKE CPON.[PO Number] + '%'
-----------
this line:
AND ISNULL(PRA.inq_num, '') <> ''
I think should be:
WHERE PRA.inq_num IS NOT NULL
right now it is being handled as part of a left join, and that join appears redundant (next)
-------------
You join a table but then do not use it in select, group or order clauses
LEFT JOIN [BCAMDB].dbo.SAP_ZVBRP BRP WITH (NOLOCK)
ON INV.SO = BRP.SO
-----------
basically:
You have to find a better way to arrive at those summed values.
Only you will know enough about the data to know if you can make those joins more efficient - but I would be trying to ensure I had better ways to join the tables.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
--
something like we suggested in this post here
https://www.experts-exchange.com/questions/28285583/T-SQL-How-to-speed-up-a-JOIN-using-a-CAST-REPLACE-expression.html
Open in new window