Solved

SQL query

Posted on 2016-11-16
4
53 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 28

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 28

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 2 46
SQL Script to Remove Data from Two Joined Tables 1 20
Current Month Filter in Visual Studio 10 23
MS SQL Conditional WHERE clause 3 17
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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