pborregg
asked on
MS SQL Server Performance and tuning
I have a stored procedure that I need t have tuned... It takes about 3 seconds to return the Rows... According to the "BOSS" at my compaany... that's too slow... There are other SPs that make up the page that this is attached to. All in All the whole page takes about 11 seconds. This SP is the most complex.
The proc is below....
SELECT p.pid, p.project_name, p.project_Manager, p.project_Manager_email, p.project_start_date, p.project_end_date,
COUNT(DISTINCT r.id) as TTLstaff,
DATEDIFF(day,getDate(), p.project_end_date) AS DaysRemain,
COUNT(DISTINCT d.deliverable_id) as lateDelivs,
COUNT(DISTINCT t.task_id) as TTLTsk,
COUNT(DISTINCT s.issue_id) as TTLIss,
COUNT(DISTINCT rx.risk_id) as TTLRsk
FROM projects p, project_intermediate pi
LEFT OUTER JOIN resources r ON (pi.pid = r.pid AND r.assigned = 1)
LEFT OUTER JOIN deliverables d ON (pi.pid = d.pid AND
(( getDate() > d.due_date
and d.date_submitted IS NULL )
OR
( d.date_submitted > d.due_date
and d.date_submitted IS NOT NULL )))
LEFT OUTER JOIN tasks t ON (pi.pid = t.pid) AND t.spid IS NULL
LEFT OUTER JOIN issues s ON (pi.pid = s.pid) AND s.spid IS NULL
LEFT OUTER JOIN risks rx ON (pi.pid = rx.pid) AND rx.spid IS NULL
WHERE pi.user_id = 'XXX' AND pi.pid = p.pid
AND p.inactive = 0 AND pi.spid IS NULL
GROUP BY p.pid, p.project_name, p.project_Manager, p.project_end_date, p.project_Manager_email, p.project_start_date, p.project_end_date
ORDER BY p.pid ASC;
The proc is below....
SELECT p.pid, p.project_name, p.project_Manager, p.project_Manager_email, p.project_start_date, p.project_end_date,
COUNT(DISTINCT r.id) as TTLstaff,
DATEDIFF(day,getDate(), p.project_end_date) AS DaysRemain,
COUNT(DISTINCT d.deliverable_id) as lateDelivs,
COUNT(DISTINCT t.task_id) as TTLTsk,
COUNT(DISTINCT s.issue_id) as TTLIss,
COUNT(DISTINCT rx.risk_id) as TTLRsk
FROM projects p, project_intermediate pi
LEFT OUTER JOIN resources r ON (pi.pid = r.pid AND r.assigned = 1)
LEFT OUTER JOIN deliverables d ON (pi.pid = d.pid AND
(( getDate() > d.due_date
and d.date_submitted IS NULL )
OR
( d.date_submitted > d.due_date
and d.date_submitted IS NOT NULL )))
LEFT OUTER JOIN tasks t ON (pi.pid = t.pid) AND t.spid IS NULL
LEFT OUTER JOIN issues s ON (pi.pid = s.pid) AND s.spid IS NULL
LEFT OUTER JOIN risks rx ON (pi.pid = rx.pid) AND rx.spid IS NULL
WHERE pi.user_id = 'XXX' AND pi.pid = p.pid
AND p.inactive = 0 AND pi.spid IS NULL
GROUP BY p.pid, p.project_name, p.project_Manager, p.project_end_date, p.project_Manager_email, p.project_start_date, p.project_end_date
ORDER BY p.pid ASC;
how many tables you are joining here, and how many fields each table have and how many records?
create views for the tables u are using in this query with only the needed fields for this query. Then use those views in this query instead of querying the tables directly.
This is a really wide subject and there can be several optimizations can be done to this query. But by just looking at your query it is hard to say what to do without knowing your schema, data and your business requirment.
But as a startup you can look at your query plan and see which part of the query is more costly. Then try to create indexes on columns used in WHERE clause and JOIN statements. For this you can use the Database Engine Tuning Advisor to get index recommendations. (http://msdn.microsoft.com/en-us/library/ms166575(v=sql.90).aspx)
But as a startup you can look at your query plan and see which part of the query is more costly. Then try to create indexes on columns used in WHERE clause and JOIN statements. For this you can use the Database Engine Tuning Advisor to get index recommendations. (http://msdn.microsoft.com/en-us/library/ms166575(v=sql.90).aspx)
That does not make any difference.
ASKER
Ok,
With respect to the number of tables...There are 7
The number of fields... There are 12
Number of rows...So far, less than 20.
When I break up this SP into smaller chunks, the time taken to get back the data is less than a second.
Is there a way to daisy chain to the smaller selects, one after another, to get the same result?
Yes, making VIEWs is something I've considered. I'm currently working on that...
Thanks all for your input so far...
I ran an execution plan and boy oh boy... there is a cost doing those joins...the CPU is being tasked, in milliseconds, but some are only .000001 and others are .03434524 for example.
Peter
With respect to the number of tables...There are 7
The number of fields... There are 12
Number of rows...So far, less than 20.
When I break up this SP into smaller chunks, the time taken to get back the data is less than a second.
Is there a way to daisy chain to the smaller selects, one after another, to get the same result?
Yes, making VIEWs is something I've considered. I'm currently working on that...
Thanks all for your input so far...
I ran an execution plan and boy oh boy... there is a cost doing those joins...the CPU is being tasked, in milliseconds, but some are only .000001 and others are .03434524 for example.
Peter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Are the number of Left Outer Joins causing a problem?
I've got indexes on all the columns I'm querying.
I've got to solve this problem and fast. My boss is furious that this query takes so long to process.
Peter
I've got indexes on all the columns I'm querying.
I've got to solve this problem and fast. My boss is furious that this query takes so long to process.
Peter
if i can get a the structure of your tables without any records , i can construct this query in an entire different manner.
ASKER
Thanks