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


Query running slow on prod.


Query running slow on prod.

Author
Message
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1391 Visits: 608
this is the actual plan from prod server.
Attachments
query.sqlplan (9 views, 123.00 KB)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43105 Visits: 20015
T.Ashish (6/17/2013)
this is the actual plan from prod server.


Thanks for posting.
I'd recommend you implement the indexes I posted earlier and repost the actual plan from prod with the indexes in place. Taken in isolation from all other queries against these tables, they are fine and should give you at least an order of magnitude lift in performance for this query: however, you should examine index usage stats and query activity (most frequent/most expensive) and tweak if necessary.

Here's a slightly modified version of the query you ran in prod. I've removed the eccentric and offputting formatting and corrected a filter in the os_contract subquery:

DECLARE @0 BIT, @1 INT
SELECT @0 = 1, @1 = 2

SELECT
activity_flag = case when c.activity_flag = 1 then 'Active' else 'Inactive' end,
msp_flag = case when bsc.msp_flag = 1 then 'true' else 'false' end,
c.name,
c.comp_code,
p.active_users,
p.new_users,
p.inactive_users,
w.worker,
c.test_comp_flag,
c.parent_comp_flag
FROM dbo.comp c ( NOLOCK )

LEFT JOIN (
SELECT
comp_code,
active_users = SUM(case when activity_flag = 1 and reg_flag = 0 then 1 else 0 end),
new_users = SUM(case when activity_flag = 1 and reg_flag = 1 then 1 else 0 end),
inactive_users = SUM(case when activity_flag = 0 then 1 else 0 end)
FROM dbo.person
GROUP BY comp_code
) p
ON p.comp_code = c.comp_code

LEFT JOIN (
SELECT
COUNT(*) worker,
owner_code
FROM dbo.worker
GROUP BY owner_code
) w
ON w.owner_code = c.comp_code

LEFT JOIN (
SELECT
msp_flag,
owner_code
FROM dbo.os_contract
WHERE msp_flag = @0
GROUP BY msp_flag, owner_code
) bsc
ON bsc.owner_code = c.comp_code
--and bsc . msp_flag = @0

WHERE c.comp_type = @1
ORDER BY c.name




“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