-3

我有这样的情况需要即时搜索,但这个查询有更多的 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+'%')

如何优化此查询

谢谢

4

1 回答 1

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+'%'
    );

之后,您将查看索引以加快速度。

于 2013-10-28T14:06:18.583 回答