代码之家  ›  专栏  ›  技术社区  ›  Paul

在C#和SQL Server Management Studio中执行SQL查询的时间非常不同

  •  5
  • Paul  · 技术社区  · 14 年前

    我已经跑了 EXEC sp_who2

    查询是:

    SELECT
        c.lngId,
        ...
    FROM tblCase c
        INNER JOIN tblCaseStatus s ON s.lngId = c.lngId
        INNER JOIN tblCaseStatusType t ON t.lngId = s.lngId
        INNER JOIN [Another Database]..tblCompany cm ON cm.lngId = cs.lngCompanyId
    WHERE t.lngId = 25
        AND c.IsDeleted = 0
        AND s.lngStatus = 1
    
    1 回复  |  直到 10 年前
        1
  •  5
  •   Remus Rusanu    14 年前

    首先,从C#运行查询时,提取查询的查询计划:

    select p.query_plan, *
    from sys.dm_exec_requests r
    cross apply sys.dm_exec_query_plan(r.plan_handle) p
    where r.session_id = <spid of C# connection>
    

    然后将其与SSMS会话下执行的计划进行比较(只需单击showtactplain工具栏)。

    Wait and Queues 是一种非常好的、经过验证的SQL Server性能故障排除方法。