Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL query

Posted on 2016-11-16
4
Medium Priority
?
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 49

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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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 49

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 32

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

636 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