Why is running a query on SQL Azure so much slower?

前端 未结 3 2019
长情又很酷
长情又很酷 2020-12-05 14:01

I created a trial account on Azure, and I deployed my database from SmarterAsp.

When I run a pivot query on SmarterAsp\\MyDatabase, the re

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 14:19

    This is first and foremost a question of performance. You are dealing with a poorly performing code on your part and you must identify the bottleneck and address it. I'm talking about the bad 2 seconds performance now. Follow the guidelines at How to analyse SQL Server performance. Once you get this query to execute locally acceptable for a web app (less than 5 ms) then you can ask the question of porting it to Azure SQL DB. Right now your trial account is only highlighting the existing inefficiencies.

    After update

    ...
    @iddepartment int
    ...
    iddepartment='+convert(nvarchar(max),@iddepartment)+'
    ...
    

    so what is it? is the iddepartment column an int or an nvarchar? And why use (max)?

    Here is what you should do:

    • parameterize @iddepartment in the inner dynamic SQL
    • stop doing nvarchar(max) conversion. Make the iddepartment and @iddertment types match
    • ensure indexes on iddepartment and all idkpis

    Here is how to parameterize the inner SQL:

    set @sql =N'
    Select * from (
    select kpiname, target, ivalues, convert(decimal(18,2),day(idate)) as iDay   
    from kpi
    inner join kpivalues on kpivalues.idkpi=kpi.idkpi
    inner join kpitarget on kpitarget.idkpi=kpi.idkpi
    inner join departmentbscs on departmentbscs.idkpi=kpi.idkpi
    where iddepartment=@iddepartment
    group by kpiname,target, ivalues,idate)x
    pivot
    (
         avg(ivalues)
        for iDay in (' +@columnName + N')
    ) p'
    
    execute sp_executesql @sql, N'@iddepartment INT', @iddepartment;
    

    The covering indexes is, by far, the most important fix. That obviously requires more info than is here present. Read Designing Indexes including all sub-chapters.

    As a more general comment: this sort of queries befit columnstores more than rowstore, although I reckon the data size is, basically, tiny. Azure SQL DB supports updateable clustered columnstore indexes, you can experiment with it in anticipation of serious data size. They do require Enterprise/Development on the local box, true.

提交回复
热议问题