Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

How to optimize this procedure.

Table - gradestatistics
select Course,Subjectcode,
round(avg(cast(total as decimal(18,5))),2) as 'Avg',
round(stdev(cast(total as decimal(18,5))),2) as 'SD',
round(avg(cast(total as decimal(18,5)))+1.645*stdev(cast(total as decimal(18,5))),2) as 'xbarplus1645sd',
round(avg(cast(total as decimal(18,5)))+stdev(cast(total as decimal(18,5))),2) as 'xbarplussd',
round(avg(cast(total as decimal(18,5)))+.5*stdev(cast(total as decimal(18,5))),2) as 'xbarplus5sd',
round(avg(cast(total as decimal(18,5))),2) as 'xbar',
round(avg(cast(total as decimal(18,5)))-.5*stdev(cast(total as decimal(18,5))),2) as 'xbarminus5sd',
round(avg(cast(total as decimal(18,5)))-stdev(cast(total as decimal(18,5))),2) as 'xbarminussd',
round(avg(cast(total as decimal(18,5)))-1.645*stdev(cast(total as decimal(18,5))),2) as 'xbarminus1645sd'
into gradestatistics
from mstmassstudents
where ems not in('F','E','I','UMC','SS','',' ')  
and  ims not in('F','E','I','UMC','SS','',' ')
group by course,subjectcode
order by 1,2,3


Procedure - why taking time.
I have course, subjectcode, total based on whcih the grades are calculated for each uid,
The code is attached herewith sample data/
Procedure-Attached.txt
smap.xlsx
Avatar of PortletPaul
PortletPaul
Flag of Australia image

why do you use a cursor based stored procedure for this and not an update statement?

one thing that strikes me you do a lot of casting within the calculations - perhaps cast total to decimal through a nested subquery - that might save some cycles.
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

Nesting has to be done, as we need to calculate avg ans stdev per subject code and based on that the formula is applied to generate the grades.
>>calculate avg and stdev per subject code
ok, this has to be done

use of cursor approach however remains a choice

any chance of getting some sample data from [gradestatistics]?
SOLUTION
Avatar of BAKADY
BAKADY
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Paul: thanks...
@BAKADY is there any specific purpose you had in mind for the nested join?
@searchsanjaysharma:
i think i misunderstood the question...
i have optimize your stored procedure... it isn't bad at all, you can use it too.
But, i've noticed in PortletPaul's Comment ID#a39249318 that you generate the table "gradestatistics" with a "SELECT ... INTO ..." before the procedure runs, and you don't want to do this anymore, this because you want to nested it into the procedure, is that correct?
if this is correct, you have to add Paul's Code in my proposal for your stored procedure like this:
USE [dbtestresults]
GO
/****** Object:  StoredProcedure [dbo].[getgrades]    Script Date: 06/14/2013 09:51:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[getgrades]
@c varchar(200)
as
begin

with cte_mstmassstudents as (
  select uid,
    course,
    subjectcode,
    cast(total as bigint) as total
    from mstmassstudents 
  where ems not in('F','E','I','UMC','SS','',' ')
    and  ims not in('F','E','I','UMC','SS','',' ')
    and course in ( @c )
  -- order by 1,2,3 is not really required
), cte_gradestatistics as (
-- >>>>>  PortletPaul's proposal   <<<<< --
  SELECT
        Course
      , Subjectcode
      , round(avg(castedtotal), 2)                              AS 'Avg'
      , round(stdev(castedtotal), 2)                            AS 'SD'
      , round(avg(castedtotal) + 1.645 * stdev(castedtotal), 2) AS 'xbarplus1645sd'
      , round(avg(castedtotal) + stdev(castedtotal), 2)         AS 'xbarplussd'
      , round(avg(castedtotal) + .5 * stdev(castedtotal), 2)    AS 'xbarplus5sd'
      , round(avg(castedtotal), 2)                              AS 'xbar'
      , round(avg(castedtotal) - .5 * stdev(castedtotal), 2)    AS 'xbarminus5sd'
      , round(avg(castedtotal) - stdev(castedtotal), 2)         AS 'xbarminussd'
      , round(avg(castedtotal) - 1.645 * stdev(castedtotal), 2) AS 'xbarminus1645sd'
  -- INTO gradestatistics,  is not more required
  FROM (
          SELECT
            Course
          , Subjectcode
          , cast(total AS DECIMAL(18, 5))  AS castedtotal
          FROM mstmassstudents
          WHERE ems NOT IN ('F', 'E', 'I', 'UMC', 'SS', '', ' ')
          AND ims NOT IN ('F', 'E', 'I', 'UMC', 'SS', '', ' ')
  ) as subset
  GROUP BY course, subjectcode
)
update mstmassstudents
set grade = case
  when cte_mstmassstudents.marks = 0
    then 'F'
  when cte_mstmassstudents.marks = 100
    then 'A+'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarplus1645sd
    then 'A+'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarplussd
   and cte_mstmassstudents.marks < cte_gradestatistics.xbarplus1645sd
    then 'A'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarplus5sd
   and cte_mstmassstudents.marks < cte_gradestatistics.xbarplussd
    then 'B+'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbar
   and cte_mstmassstudents.marks < cte_gradestatistics.xbarplus5sd
    then 'B'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarminus5sd
   and cte_mstmassstudents.marks < cte_gradestatistics.xbar
    then 'C+'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarminussd
   and cte_mstmassstudents.marks < cte_gradestatistics.xbarminus5sd
    then 'C'
  when cte_mstmassstudents.marks >= cte_gradestatistics.xbarminus1645sd
   and cte_mstmassstudents.marks < cte_gradestatistics.xbarminussd
    then 'D'
  when cte_mstmassstudents.marks < cte_gradestatistics.xbarminus1645sd
    then 'F'
from mstmassstudents inner join (
  cte_mstmassstudents inner join cte_gradestatistics
     on cte_mstmassstudents.course =cte_gradestatistics.icourse
    and cte_mstmassstudents.subjectcode = cte_gradestatistics.subjectcode
  )
   on mstmassstudents.uid = cte_mstmassstudents.uid
  and cte_mstmassstudents.course = cte_mstmassstudents.course
  and cte_mstmassstudents.subjectcode = cte_mstmassstudents.subjectcode
-- maybe you need this too to update only the same records selected at cte_mstmassstudents
where ems not in('F','E','I','UMC','SS','',' ')
  and ims not in('F','E','I','UMC','SS','',' ') 
end

Open in new window


If this is correct split the points between experts...

Regards
@PortletPaul:
:) scary BAKADY, exactly the direction I had in mind ... as it should be faster
maybe we are both from the same school, or maybe you are my lost twin...  (just kidding...)  ;)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@PortletPaul: you're right, i forgot to clear "cte_" at the condition's left side and the "i" at icourse, "course" is correct...

@searchsanjaysharma: IMPORTANT!!!
Make this changes to get the code working!!!

PS. I'm posting this from EE's mobile version, this because I didn't post a fixed code.
Hope you have that overall solution in place. Sorry you believe this is a C grade - see below

What grade should I award?
C should only be given for an incomplete solution that does not fully address or answer the question. A C grade should be awarded only after the asker has replied to all expert comments, provided all requested information, tried all suggested solutions, given the experts ample time to reply, and received clarification about the answer given. The asker must justify giving a C grade and give the experts an opportunity to improve it.