Solved

SQL query

Posted on 2016-11-16
4
25 Views
Last Modified: 2016-11-29
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
Comment Utility
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 16

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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
Comment Utility
"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 16

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

7 Experts available now in Live!

Get 1:1 Help Now