Sql server 如何在我的t-sql过程中找到低性能代码

Sql server 如何在我的t-sql过程中找到低性能代码,sql-server,performance,tsql,Sql Server,Performance,Tsql,我有一些嵌套的过程,它们的性能很低。为了找到瓶颈,我在t-sql代码中插入了一些调试标记,用于度量我怀疑性能低下的代码块的性能。此调试标记如下所示: select @start_point = GETDATE() -- start measuring point --- open @session_license_fee_cur -- suspected chunk of code --- select @end_point = GETDATE()-- end measuring point s

我有一些嵌套的过程,它们的性能很低。为了找到瓶颈,我在t-sql代码中插入了一些调试标记,用于度量我怀疑性能低下的代码块的性能。此调试标记如下所示:

select @start_point = GETDATE() -- start measuring point
---
open @session_license_fee_cur -- suspected chunk of code
---
select @end_point = GETDATE()-- end measuring point
select @duration = datediff(ms, @start_point, @end_point)
select @log_info_total = 'Opening cursor license_fee (bills_supp_create_license_fee) (@class_id = ' + cast(@class_id as nvarchar) + ')';

exec bills_supp_save_calculation_log @duration, @log_info_total, @house_id, @account_id, @log_level -- procedure for creating log (simple insert into log table pes_bl_bills_calculation_log_total) 
运行程序后,我从
pes\u bl\u bills\u calculation\u log\u total
表中运行查询,以查找性能最低的代码。看起来像这样

    set @session_license_fee_cur =  cursor static for 
    select activity_id
            , addendum_id
            , service_id
            , active_from
            , active_to
    from dbo.bills_supp_get_activate_license_fee_for_sessions_by_house(@active_from, @active_to, @house_id)

select @start_point = GETDATE()
---
open @session_license_fee_cur
---
select @end_point = GETDATE()
select @duration = datediff(ms, @start_point, @end_point)
select @log_info_total = 'Opening cursor license_fee (bills_supp_create_license_fee) (@class_id = ' + cast(@class_id as nvarchar) + ')';
exec bills_supp_save_calculation_log @duration, @log_info_total, @house_id, @account_id, @log_level
declare @active_from date = '01.03.2014'
declare @active_to date = '01.04.2014'
declare @house_id integer = 11927
        select activity_id
                , addendum_id
                , service_id
                , active_from
                , active_to
        from dbo.bills_supp_get_activate_license_fee_for_sessions_by_house(@active_from, @active_to, @house_id)
换句话说,open
@session\u license\u fee\u cur
工作非常缓慢(大约501980毫秒)

我试图在SQLServerManagementStudio中使用给定参数运行这段代码,以便查看查询计划并尝试对其进行优化。我是这样运行的

    set @session_license_fee_cur =  cursor static for 
    select activity_id
            , addendum_id
            , service_id
            , active_from
            , active_to
    from dbo.bills_supp_get_activate_license_fee_for_sessions_by_house(@active_from, @active_to, @house_id)

select @start_point = GETDATE()
---
open @session_license_fee_cur
---
select @end_point = GETDATE()
select @duration = datediff(ms, @start_point, @end_point)
select @log_info_total = 'Opening cursor license_fee (bills_supp_create_license_fee) (@class_id = ' + cast(@class_id as nvarchar) + ')';
exec bills_supp_save_calculation_log @duration, @log_info_total, @house_id, @account_id, @log_level
declare @active_from date = '01.03.2014'
declare @active_to date = '01.04.2014'
declare @house_id integer = 11927
        select activity_id
                , addendum_id
                , service_id
                , active_from
                , active_to
        from dbo.bills_supp_get_activate_license_fee_for_sessions_by_house(@active_from, @active_to, @house_id)
但它的工作速度非常快(大约在0(零)秒内返回3000条记录)。 在程序中打开光标有什么区别

open @session_license_fee_cur
在SQLServerManagementStudio中运行它

declare @active_from date = '01.03.2014'
declare @active_to date = '01.04.2014'
declare @house_id integer = 11927
        select activity_id
                , addendum_id
                , service_id
                , active_from
                , active_to
        from dbo.bills_supp_get_activate_license_fee_for_sessions_by_house(@active_from, @active_to, @house_id)

我的瓶颈在哪里?

从Read IO的角度查找最昂贵的5个查询


谢谢。但前5个查询并没有显示我在日志中显示的内容。从日志的角度来看,它向我展示了工作速度足够快的程序。也许我错了,但前两个“读取扩展”查询在第3811列“总物理读取”中显示了我。不幸的是,我不知道它是多还是少,但我的直觉告诉我,它不是那么多。我说的对吗?主差速器是发动机,我不需要处理光标。您可以尝试使用FAST_FORWARD提示游标,但通常最好的方法是避免使用游标。顺便说一句,请使用sysdatetime()而不是getdate(),谢谢。看来我发现了问题。函数bills_supp_get_activate_license_fee_for_sessions_by_house中的子查询有一个导致降级的完整扫描查询。但有一件事我没有明白。为什么在SQL Management Studio中,这个没有光标的查询工作时间约为零秒,但在查询中工作时间超过5分钟。为什么会有这样的差异?您必须看到生成的查询计划,这可以为您提供一些线索(包括有关完整表扫描的信息)。可能光标正在分配大量内存/tempdb。尝试监视CPU、内存和IO,以查看游标声明的错误。