Sql server sql查询超时…case/if/怎么办
我有以下sql server存储过程:Sql server sql查询超时…case/if/怎么办,sql-server,tsql,Sql Server,Tsql,我有以下sql server存储过程: PROCEDURE [dbo].[GetSoftwareProgramsGrid] @SoftwareTitle varchar(1000)='All', @CategoryID varchar(100)='All', @ManufacturerID varchar(50)='All', @ModelID int=0, -- 0 means all @AssetID int=0, -- 0 means all
PROCEDURE [dbo].[GetSoftwareProgramsGrid]
@SoftwareTitle varchar(1000)='All',
@CategoryID varchar(100)='All',
@ManufacturerID varchar(50)='All',
@ModelID int=0, -- 0 means all
@AssetID int=0, -- 0 means all
@AssetStatus int=0, --0 is active, 1 is inactive, and 2 is all
@Status int=0, --0 is active, 1 is inactive, and 2 is all
@Type varchar(100)='All',
@Site varchar(100)='All',
@Department varchar(100)='All',
@Manager varchar(100)='All',
@Employee varchar(100)='All',
@SortExpression varchar(100)='Software',
@SortOrder int=0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
*
FROM
(
SELECT DISTINCT
Program AS Software
FROM
AssetProgram ap
LEFT JOIN
AssetAssignment aa
ON
aa.AssetID = ap.AssetID
LEFT JOIN
[MyLinkedServer].MyDB.dbo.Login l
ON
l.LoginID = aa.LoginID
LEFT JOIN
Asset a
ON
a.AssetID = ap.AssetID
INNER JOIN Model m
ON
a.ModelID = m.ModelID
INNER JOIN
Category c
ON
c.CategoryID = m.CategoryID
INNER JOIN Manufacturer ma
ON
ma.ManufacturerID = m.ManufacturerID
WHERE
(
--Software filters
(ap.Program = @SoftwareTitle OR @SoftwareTitle='All')
--Asset filters
AND (c.CategoryID = @CategoryID OR @CategoryID='All') --filter category
AND (ma.ManufacturerID = @ManufacturerID OR @ManufacturerID='All') --filter manufacturer
AND (m.ModelID = @ModelID OR @ModelID = 0) --filter model
AND (a.AssetID = @AssetID OR @AssetID = 0) --filter by asset name (the actual asset id)
AND (((a.Inactive=@AssetStatus) OR (@AssetStatus=2)))
AND (aa.Inactive=0)
AND (ap.Inactive=0)
--Employee filters
/*AND ((l.Inactive=@Status) OR (@Status=2)) --status of employee 2 is all, 1 is inactive, and 0 is active
AND (@Type='All' OR (@Type='Contractor' AND l.IsContractor=1) OR (@Type='Regular' AND l.IsContractor=0)) --contractor or regular employee
AND (@Site='All' OR @Site=l.ClientID) --the site
AND (@Department='All' OR @Department=l.FunctionalGroupID) --the department
AND ((l.Manager = @Manager OR l.FullName=@Manager) OR @Manager='All') --the manager
AND (l.FullName = @Employee OR @Employee='All') --the employee
*/
)) ttt
ORDER BY
CASE WHEN @SortExpression='Software' AND @SortOrder=0 THEN Software END ASC,
CASE WHEN @SortExpression='Software' AND @SortOrder=1 THEN Software END DESC
由于我们的设置,此查询必须包含链接服务器。只要我注释掉我的员工参数,查询就会运行良好且快速,即此部分:
--Employee filters
/*AND ((l.Inactive=@Status) OR (@Status=2)) --status of employee 2 is all, 1 is inactive, and 0 is active
AND (@Type='All' OR (@Type='Contractor' AND l.IsContractor=1) OR (@Type='Regular' AND l.IsContractor=0)) --contractor or regular employee
AND (@Site='All' OR @Site=l.ClientID) --the site
AND (@Department='All' OR @Department=l.FunctionalGroupID) --the department
AND ((l.Manager = @Manager OR l.FullName=@Manager) OR @Manager='All') --the manager
AND (l.FullName = @Employee OR @Employee='All') --the employee
*/
当我把那一节的第一行也带进来的时候,比如说这一行:
和((l.Inactive=@Status)或(@Status=2))
整个存储过程将挂起(超时)…我已经为我的表建立了正确的索引,甚至在链接表中的Inactive
字段上有一个索引…如果我在上面的同一行中说:
和(l.Inactive=0)
它运行正常,因此OR条件导致了它(布尔值)。但是,我需要这个条件,因为传递了一个需要满足的参数。如果开始…使用所有这些参数,我还需要
哪些选项?这似乎很麻烦……对于任何人的信息来说,AssetProgram表总共有5万行,所以这并不算太多。是的,您的where子句很疯狂,尤其是所有的“OR”都很疯狂
尝试根据@Status变量将“用户”加载到临时表(或var表)中(如果为1,则只加载非活动项;如果为0,则只加载活动项等等),并在连接上使用临时表,这将大大减少比较次数。检查查询计划。我打赌ORs会导致索引扫描而不是seeksI尝试了这样做……如果(@Status=0)开始插入到#用户(LoginID,不活动)选择LoginID,不活动从[TKKAH-SQL-1].tkkkcommondata.dbo.Login l其中(l.Inactive=0,l.HireDate不为NULL)如果(@Status=1)开始插入到#用户(LoginID,不活动)从[TKKAH-SQL-1]中选择LoginID,不活动。TKKCommonData.dbo.Login l其中(l.Inactive=1且l.HireDate不为NULL)结束,否则开始插入到#用户(LoginID,不活动)中选择LoginID,不活动从[TKKAH-SQL-1]。TKKComonData.dbo.Login l其中(l.HireDate不为NULL)结束
,但结果是我仍然需要这个…((l.Inactive=@Status)或(@Status=2))
如果我不包括和
查询总是返回相同的软件标题…可能是因为它是左连接…错误