我如何在没有加入的情况下优化这个存储过程
Posted
技术标签:
【中文标题】我如何在没有加入的情况下优化这个存储过程【英文标题】:How can i optimize this stored procedure Without Join 【发布时间】:2013-10-28 13:57:01 【问题描述】:我在即时搜索时遇到了这样的情况,但这个查询有更多的 NOT IN 检查
ALTER PROC [dbo].[DPR_SP_GetEmployeeDetailsByKeyword]
@keyword varchar(10),
@hotelId int
AS
BEGIN
SELECT EmployeeDetails.EmployeeId,EmployeeDetails.SageEmployeeId,EmployeeDetails.FirstName,EmployeeDetails.LastName
FROM EmployeeDetails
WHERE (EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=@hotelId
)
AND
EmployeeDetails.EmployeeId LIKE @keyword+'%')
OR
(EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=@hotelId
)
AND
OR
(EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=@hotelId
)
AND
EmployeeDetails.DateOfBirth LIKE @keyword+'%')
如何优化这个查询
谢谢
【问题讨论】:
当你分析这个时,瓶颈在哪里?使用 SQL Management Studio,您可以查看执行计划以确定这一点。 【参考方案1】:你可以把它改写成
alter proc dbo.DPR_SP_GetEmployeeDetailsByKeyword
@keyword varchar(10),
@hotelId int
As
Select
e.EmployeeId,
e.SageEmployeeId,
e.FirstName,
e.LastName
From
EmployeeDetails e
Left Outer Join
EmployeeWorksIn w
on e.EmployeeId = w.EmployeeId and w.HotelId = @hotelId
Where
w.EmployeeID Is Null And (
e.EmployeeId Like @keyword+'%' Or
e.Something Like @keyword+'%' Or -- appears to be missing from question
e.DateOfBirth Like @keyword+'%'
);
之后,您将研究索引以加快速度。
【讨论】:
以上是关于我如何在没有加入的情况下优化这个存储过程的主要内容,如果未能解决你的问题,请参考以下文章
我可以在没有服务器的情况下在 access 2010 中创建存储过程吗? [复制]