azure mysql慢查日志,为什么运行的SQL Azure的查询这么慢得多?

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

When I run a pivot query on SmarterAsp\MyDatabase, the results appeared in 2 seconds.

However, running the same query on Azure\MyDatabase took 94 seconds.

I use the SQL Server 2014 Management Studio (trial) to connect to the servers and run query.

Is this difference of speed because my account is a trial account?

Some related info to my question

the query is:

ALTER procedure [dbo].[Pivot_Per_Day]

@iyear int,

@imonth int,

@iddepartment int

as

declare @columnName Nvarchar(max) = ''

declare @sql Nvarchar(max) =''

select @columnName += quotename(iDay) + ','

from (

Select day(idate) as iDay

from kpivalues where year(idate)=@iyear and month(idate)=@imonth

group by idate

)x

set @columnName=left(@columnName,len(@columnName)-1)

set @sql ='

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='+convert(nvarchar(max),@iddepartment)+'

group by kpiname,target, ivalues,idate)x

pivot

(

avg(ivalues)

for iDay in (' + @columnName + ')

) p'

execute sp_executesql @sql

Running this query on 3 different servers gave me different results in terms of Elapsed time till my pivot table appear on the screen:

Azure - Elapsed time = 100.165 sec

Smarterasp.net - Elapsed time = 2.449 sec

LocalServer - Elapsed time = 1.716 sec

Regarding my trial account on Azure, I made it with the main goal to check if I will have a better speed than Smarter when running stored procedure like the above one.

I choose for my database Service Tier - Basic, Performance level -Basic(5DTUs) and Max. Size 2GB.

My database has 16 tables, 1 table has 145284 rows, and the database size is 11mb. Its a test database for my app.

My questions are:

What can I do, to optimize this query (sp)?

Is Azure recommended for small databases (100mb-1Gb)? I mean performance vs. cost!

Conclusions based on your inputs:

I made suggested changes to the query and the performance was improved with more than 50% - Thank you Remus

I tested my query on Azure S2 and the Elapsed time for updated query was 11 seconds.

I tested again my query on P1 and the Elapsed time was 0.5 seconds :)

the same updated query on SmarterASP had Elapsed time 0.8 seconds.

Now its clear for me what are the tiers in Azure and how important is to have a very good query (I even understood what is an Index and his advantage/disadvantage)

Thank you all,

Lucian

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值