?
Solved

SQL query

Posted on 2016-11-16
4
Medium Priority
?
62 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 29

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 29

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

765 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