Solved

SQL query

Posted on 2016-11-16
4
46 Views
Last Modified: 2016-12-28
I need ti improve the sql performance

SELECT COUNT(*) as totalCnt,
	SUM(CASE WHEN IR.sessionType = 1 THEN 1 END) BTWRatingCount,
	SUM(CASE WHEN IR.sessionType = 0 THEN 1 END) ClassRatingCount,
	CONVERT(DECIMAL(10, 2),AVG(IRQA.btwAvgRating)) btwAvgRating,
	CONVERT(DECIMAL(10, 2),AVG(IRQA.classAvgRating)) classAvgRating,
	CONVERT(DECIMAL(10, 2),IRQTotal.overallAvgRating) overallAvgRating
FROM dbo.instructorRating IR
CROSS APPLY (SELECT AVG(CASE WHEN IR2.sessionType = 1 THEN CAST(IRQ.scaleID AS FLOAT) ELSE NULL END) as btwAvgRating,
                    AVG(CASE WHEN IR2.sessionType = 0 THEN CAST(IRQ.scaleID AS FLOAT) ELSE NULL END) as classAvgRating
             FROM dbo.instructor_rating_question IRQ
			 INNER JOIN dbo.instructorRating IR2 ON IR2.ratingID = IRQ.ratingID
			 WHERE IR2.sessionUnitKey = IR.sessionUnitKey) IRQA
CROSS JOIN (SELECT AVG(CAST(IRQ.scaleID AS FLOAT)) overallAvgRating
			FROM dbo.instructor_rating_question IRQ
			INNER JOIN dbo.instructorRating IR ON IR.ratingID = IRQ.ratingID) IRQTotal
GROUP BY IRQTotal.overallAvgRating

Open in new window

0
Comment
Question by:erikTsomik
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41890367
Does this inner join approach make any/much difference?
SELECT
      COUNT(*)                                           AS totalCnt
    , SUM(CASE WHEN IR.sessionType = 1 THEN 1 END)       BTWRatingCount
    , SUM(CASE WHEN IR.sessionType = 0 THEN 1 END)       ClassRatingCount
    , CONVERT(decimal(10, 2), AVG(IRQA.btwAvgRating))    btwAvgRating
    , CONVERT(decimal(10, 2), AVG(IRQA.classAvgRating))  classAvgRating
    , CONVERT(decimal(10, 2), IRQTotal.overallAvgRating) overallAvgRating
FROM dbo.instructorRating IR
INNER JOIN (
      SELECT
            IRQ.ratingID
          , AVG(CASE WHEN IR2.sessionType = 1 THEN CAST(IRQ.scaleID AS float) ELSE NULL END) AS btwAvgRating
          , AVG(CASE WHEN IR2.sessionType = 0 THEN CAST(IRQ.scaleID AS float) ELSE NULL END) AS classAvgRating
      FROM dbo.instructor_rating_question IRQ
      INNER JOIN dbo.instructorRating IR2 ON IR2.ratingID = IRQ.ratingID
      GROUP BY
            IRQ.ratingID
) IRQA ON IR.ratingID = IRQA.ratingID
CROSS JOIN (
      SELECT
            AVG(CAST(IRQ.scaleID AS float)) overallAvgRating
      FROM dbo.instructor_rating_question IRQ
      INNER JOIN dbo.instructorRating IR ON IR.ratingID = IRQ.ratingID
) IRQTotal
GROUP BY
      IRQTotal.overallAvgRating

Open in new window

can you provide the execution plan please? (attach as a .sqlplan file)
0
 
LVL 24

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41890606
Here is the fix., Please try..

SELECT COUNT(*) as totalCnt,
	SUM(CASE WHEN IR.sessionType = 1 THEN 1 END) BTWRatingCount,
	SUM(CASE WHEN IR.sessionType = 0 THEN 1 END) ClassRatingCount,
	CONVERT(DECIMAL(10, 2),AVG(IRQA.btwAvgRating)) btwAvgRating,
	CONVERT(DECIMAL(10, 2),AVG(IRQA.classAvgRating)) classAvgRating,
	CONVERT(DECIMAL(10, 2),IRQTotal.overallAvgRating) overallAvgRating
FROM dbo.instructorRating IR
CROSS APPLY (SELECT AVG(CASE WHEN IR.sessionType = 1 THEN (IRQ.scaleID) * 1. ELSE NULL END) as btwAvgRating,
                    AVG(CASE WHEN IR.sessionType = 0 THEN (IRQ.scaleID) * 1. ELSE NULL END) as classAvgRating
             FROM dbo.instructor_rating_question IRQ			 
			 WHERE IR.sessionUnitKey = IR.sessionUnitKey) IRQA
CROSS APPLY (SELECT AVG(CAST(IRQ.scaleID AS FLOAT)) overallAvgRating
			FROM dbo.instructor_rating_question IRQ
			WHERE IR.ratingID = IRQ.ratingID) IRQTotal
GROUP BY IRQTotal.overallAvgRating

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41890643
"here is THE fix"

Really? No other approach would be needed or appropriate?
How can you be so certain ....... when you don't even know what the indexes are?

How does changing a cross join (of a single value) to a cross apply assist overall performance?
0
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41890652
Yes I think this will improve the performance. I changed other things also. Please check again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now