Why does a database query only go slow in the application?

后端 未结 2 856
别那么骄傲
别那么骄傲 2021-01-01 06:38

I have a webpage that takes 10 minutes to run one query against a database, but the same query returns in less than a second when run from SQL Server Management Studio.

2条回答
  •  没有蜡笔的小新
    2021-01-01 07:03

    I would suspect parameter sniffing.

    The cached execution plan used for your application's connection probably won't be usable by your SSMS connection due to different set options so it will generate a new different plan.

    You can retrieve the cached plans for the stored procedure by using the query below. Then compare to see if they are different (e.g. is the slow one doing index seeks and bookmark lookups at a place where the other one does a scan?)

    Use YourDatabase;
    
    SELECT *
    FROM sys.dm_exec_cached_plans 
    CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
    CROSS APPLY sys.dm_exec_query_plan(plan_handle) 
    cross APPLY sys.dm_exec_plan_attributes(plan_handle) AS epa
    where sys.dm_exec_sql_text.OBJECTID=object_id('YourProcName') 
             and attribute='set_options'
    

提交回复
热议问题