Solved

re-writting SQL scripts

Posted on 2012-04-05
2
212 Views
Last Modified: 2012-04-13
Hi,

I have ask to re-write a sql sripts and improve performance during the run time, can some one give me an ideas on how can I re-write this query the sturcture are pretty the same and do the same thing but the destination of the table is different please see below

DECLARE @start datetime, @end datetime, @YTD datetime, @myDate datetime
--SET @start = '2011-02-01'
--SET @end = '2012-01-01'
--SET @YTD = '2012-01-31'

set @mydate =dateadd(mm,-2,DATEADD(year,-1,getdate())) --change the minus 2 to one

set @start = CONVERT(VARCHAR(10),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101)
set @end = DATEADD(mm,11,@start)
set @YTD = CONVERT(VARCHAR(10),DATEADD(dd,-(DAY(DATEADD(mm,1,@end))),DATEADD(mm,1,@end)),101)

SELECT
             Site
            ,Div
            ,SubDiv
            ,d.DeptCode
            ,c.NPI_Number
            ,Clarity_Provider_Id as ProvNo
            ,c.ProvName
            ,SurveyId
            ,b.Category
            ,QuestionId
            ,CASE WHEN AnsVal = 5 THEN 100
                    WHEN AnsVal = 4 THEN 75
                    WHEN AnsVal = 3 THEN 50
                    WHEN AnsVal = 2 THEN 25
                    WHEN AnsVal = 1 THEN 0 END AS 'Mean',
              ServiceDateMonth
INTO #Mean_Surveys_Convert_Mean
FROM ZZ04M_PS_Base_PatSats_PressGaney_Detail a
            INNER JOIN (select Code, category
                              from SMSSTAGING04.dbo.PS_PressGaney_Dict_Questions
                              where standard = 'Y' and Survey = 'MG'
                              )b on a.QuestionID = b.Code
            INNER JOIN GEN_0010_Base_Provider_Master c on a.Clarity_Provider_Id = c.provno
            INNER JOIN GEN_0020_Base_GLDept_Master_PAMF d on a.Clarity_Department_Id = d.Clarity_Dept_Id
WHERE ServiceDateMonth BETWEEN @start and @end
            AND AnsVal is not NULL
            AND DATEDIFF(dd,ServiceDateMonth,ReceivedDate) <=60

SELECT  
            a.DeptCode+'-'+Facility as 'Site'
            ,Division as 'Div'
            ,SubDivision as 'SubDiv'
            ,a.DeptCode
            ,NPI_Number
            ,a.provno
            ,c.ProvName
            ,SurveyId
            ,b.Category
            ,QuestionId
            ,CASE      WHEN AnsVal = 5 THEN 100
                        WHEN AnsVal = 4 THEN 75
                        WHEN AnsVal = 3 THEN 50
                        WHEN AnsVal = 2 THEN 25
                        WHEN AnsVal = 1 THEN 0 END AS 'Mean'
            ,ServiceDateMonth
INTO #Mean_Surveys_Convert_Mean_Mills
FROM ZZ04M_PS_Base_PatSats_PressGaney_Detail_Mills a
INNER JOIN (select Code, category
                  from SMSSTAGING04.dbo.PS_PressGaney_Dict_Questions
                  where standard = 'Y' and Survey = 'MG'
                  )b on a.QuestionID = b.Code
INNER JOIN GEN_0010_Base_Provider_Master_Mills c on a.provno = c.provno
WHERE ServiceDateMonth BETWEEN @start and @end
            AND AnsVal is not NULL
            AND DATEDIFF(dd,ServiceDateMonth,ReceivedDate) <=60
order by surveyid



SELECT NPI_Number, ProvName, SurveyId, Category, Avg(CAST(Mean as decimal (9,2))) AS Mean, 'Q'+datename(q,ServiceDateMonth)+'-'+datename(yy,ServiceDateMonth) as 'Quarter'
INTO #Mean_Surveys_Create_Category_Mean_Provider
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId, Category, ServiceDateMonth
order by Category


select * from #Mean_Surveys_Create_Category_Mean_Provider
--create overall mean survey provider

SELECT NPI_Number, ProvName, SurveyId, Avg(CAST(Mean as decimal (9,2))) AS Mean, 'Q'+datename(q,ServiceDateMonth)+'-'+datename(yy,ServiceDateMonth) as 'Quarter'
INTO #Mean_Surveys_Create_Overall_Mean_Provider
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId, ServiceDateMonth

--create category mean survey provider YTD

SELECT NPI_Number, ProvName, SurveyId, Category, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, @YTD, 101) as 'Quarter'
INTO #Mean_Surveys_Create_Category_Mean_Provider_YTD
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId, Category

--create category mean survey provider

SELECT
       NPI_Number
      ,ProvName
      ,SurveyId
      ,Category
      ,Avg(CAST(Mean as decimal (9,2))) AS Mean
      ,'Q'+datename(q,ServiceDateMonth)+'-'+datename(yy,ServiceDateMonth) as 'Quarter'
INTO #Mean_Surveys_Create_Category_Mean_Provider
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY
            NPI_Number
            ,ProvName
            ,SurveyId
            ,Category
      ,ServiceDateMonth
order by Category


select * from #Mean_Surveys_Create_Category_Mean_Provider
--create overall mean survey provider

SELECT NPI_Number, ProvName, SurveyId, Avg(CAST(Mean as decimal (9,2))) AS Mean, 'Q'+datename(q,ServiceDateMonth)+'-'+datename(yy,ServiceDateMonth) as 'Quarter'
INTO #Mean_Surveys_Create_Overall_Mean_Provider
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId, ServiceDateMonth

--create category mean survey provider YTD

SELECT NPI_Number, ProvName, SurveyId, Category, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, @YTD, 101) as 'Quarter'
INTO #Mean_Surveys_Create_Category_Mean_Provider_YTD
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId, Category

--create overall mean survey provider YTD

SELECT NPI_Number, ProvName, SurveyId, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, @YTD, 101) as 'Quarter'
INTO #Mean_Surveys_Create_Overall_Mean_Provider_YTD
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY NPI_Number, ProvName, SurveyId



/**************************************
Run queries to calculate site mean score
**************************************/

--create category mean survey Site

SELECT Site, DeptCode, SurveyId, Category, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, ServiceDateMonth, 101) as 'Month'
INTO #Mean_Surveys_Create_Category_Mean_Site
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY Site,DeptCode,SurveyId, Category, ServiceDateMonth

--create overall mean survey Site

SELECT Site, DeptCode,SurveyId, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, ServiceDateMonth, 101)as 'Month'
INTO #Mean_Surveys_Create_Overall_Mean_Site
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY Site, DeptCode,SurveyId, ServiceDateMonth

--create category mean survey Site YTD

SELECT Site, DeptCode,SurveyId, Category, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, @YTD, 101) as 'Month'
INTO #Mean_Surveys_Create_Category_Mean_Site_YTD
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY Site, DeptCode,SurveyId, Category

--create overall mean survey Site YTD

SELECT Site, DeptCode,SurveyId, Avg(CAST(Mean as decimal (9,2))) AS Mean, convert(varchar, @YTD, 101) as 'Month'
INTO #Mean_Surveys_Create_Overall_Mean_Site_YTD
FROM #Mean_Surveys_Convert_Mean
WHERE category is not NULL
GROUP BY Site, DeptCode,SurveyId


There is more to it and there are at least 2000 lines of code the I need to modify, I wanted make it readable, modular, the performance is great and easy to maintain please give me an idea on how would I tackle this,  I was thinking of doing it on a udf but I am stuck please advise thank you in advance
0
Comment
Question by:apmanzojr1
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
SJCFL-Admin earned 500 total points
Comment Utility
At the end you said the performance is great and it was easy to maintain.  did you mean the current version or the new one you were told to create?  I ask because performance tuning for me does not usually involve looking at a script and deciding to rewite it. I will usually fire up the profiler while the process runs and then evaluate where my most costly operations are and where I think I can get the most bang for my buck.  Most times adding a few strategic indexes or dropping them prior to a bulk load and rebuilding them afterwards can have a big impact on performance.

But if management objects to the 2000 lines of code and wants a parameter driven solution, that is another story and has nothing to do with performance tuning....  it might make it more flexible, but not faster. UDFs are not known for their speed (my opinion).  

And I do not know how you collect and process the data.    Or whether you have DASD available to preprocess and keep data from one iteration to the next so that you only process new data.  Or whether you have the CPU power to just split out the final computations to run in parallel to finish quicker rather than running everything serially.  I did not notice you making use of the with (nolock) clause on the from clauses.  Perhaps I am missing something on this type of table, but you might reduce overhead with this clause a small amount.
0
 
LVL 6

Assisted Solution

by:SJCFL-Admin
SJCFL-Admin earned 500 total points
Comment Utility
I'd also have the activity monitor up while it was running (but not while the profiler was active) and look at what my high waits were....  But then I expect you have already gone through these steps...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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