我正在加入3个不同的表来实现自动完成功能,而且必须快速。建议必须恢复良好和迅速。我需要通过LIKE
运算符查看3个不同的字段以获得答案。请参阅下面的详细信息。
DECLARE @Space AS CHAR(1) = ' ';
SELECT TOP (@QuantityToReturn) *
FROM
(SELECT
C.CMPID, C.CMPNAME, C.CMPTHEIRCODE, AD.ADTOWN
FROM COMPANY C
JOIN [dbo].[COMPADDRESS] CA ON CA.CMPID = C.CMPID
JOIN [dbo].[ADDRESS] ad ON AD.ADID = CA.ADID
JOIN [dbo].SUPPLIER SUP ON C.CMPID = SUP.CMPID
WHERE
(C.CMPID = @LoggedInUserId
OR @LoggedInUserId = dbo.fnIsAParentCompanyOf(@LoggedInUserId, C.CMPID)
OR @LoggedInUserId = 12345)
UNION
SELECT
C.CMPID, C.CMPNAME, C.CMP_THEIRCODE, AD.ADTOWN
FROM COMPANY C
JOIN [dbo].[COMPADDRESS] CA ON ca.CMPID = C.CMPID
JOIN [dbo].[ADDRESS] AD ON AD.ADID = CA.ADID
JOIN [dbo].CUSTOMER CUS ON C.CMPID = CUS.CMPID AND CUS.CUSTISTHIS = 1
WHERE
(C.CMPID = @LoggedInUserId
OR @LoggedInUserId = dbo.fnIsAParentCompanyOf(@LoggedInUserId, C.CMPID)
OR @LoggedInUserId = 12345)
) AS Results
WHERE
(CMPNAME + @Space + ADTOWN + @Space + CMPTHEIRCODE) LIKE '%' + @Query + '%'
上面的代码很慢(约9秒)
WHERE CMPNAME LIKE '%' + @Query + '%'
=快; WHERE ADTOWN LIKE '%' + @Query + '%'
=快; WHERE CMPTHEIRCODE LIKE '%' + @Query + '%'
=快; 当我连接它们时,它会变慢吗?
答案 0 :(得分:0)
我会尝试的是:
WHERE
LEN(@Query) <> LEN(REPLACE(@Query, CMPNAME, ''))
AND (LEN(@Query) <> LEN(REPLACE(@Query, ADTOWN, ''))
AND CHARINDEX(CMPNAME, @Query) = (CHARINDEX(ADTOWN, @Query) - 1 - LEN(CMPNAME))
) -- making sure that the value of ADTOWN comes after CMPNAME
AND (LEN(@Query) <> LEN(REPLACE(@Query, CMPTHEIRCODE, ''))
AND CHARINDEX(ADTOWN, @Query) = (CHARINDEX(CMPTHEIRCODE, @Query) - 1 - LEN(ADTOWN))
) -- making sure that the value of CMPTHEIRCODE comes after ADTOWN
我尝试重新实现您的3个条件的逻辑来运行参数而不是您拥有索引的列。我的假设是在这种情况下将使用索引。
PS:在查找/减去charindex的结果时需要小的调整,以防我在计算中犯了一些错误,但希望你明白这一点。 - {{3} }。
此外,我建议/尝试对查询的顶部进行一些其他更改,以便最终您的脚本如下所示:
DECLARE @Space AS CHAR(1) = ' ';
SELECT TOP (@QuantityToReturn) *
FROM (
SELECT C.CMPID
,C.CMPNAME
,C.CMPTHEIRCODE
,AD.ADTOWN
FROM COMPANY C
INNER JOIN [dbo].[COMPADDRESS] CA
ON CA.CMPID = C.CMPID
INNER JOIN [dbo].[ADDRESS] ad
ON AD.ADID = CA.ADID
WHERE (
C.CMPID = @LoggedInUserId
OR @LoggedInUserId = dbo.fnIsAParentCompanyOf(@LoggedInUserId, C.CMPID)
OR @LoggedInUserId = 12345
)
AND EXISTS (
SELECT 1
FROM [dbo].SUPPLIER SUP
WHERE C.CMPID = SUP.CMPID
)
UNION
SELECT C.CMPID
,C.CMPNAME
,C.CMP_THEIRCODE
,AD.ADTOWN
FROM COMPANY C
INNER JOIN [dbo].[COMPADDRESS] CA
ON ca.CMPID = C.CMPID
INNER JOIN [dbo].[ADDRESS] AD
ON AD.ADID = CA.ADID
WHERE (
C.CMPID = @LoggedInUserId
OR @LoggedInUserId = dbo.fnIsAParentCompanyOf(@LoggedInUserId, C.CMPID)
OR @LoggedInUserId = 12345
)
AND EXISTS (
SELECT 1
FROM [dbo].CUSTOMER CUS
WHERE C.CMPID = CUS.CMPID
AND CUS.CUSTISTHIS = 1
)
) AS Results
WHERE LEN(@Query) <> LEN(REPLACE(@Query, CMPNAME, ''))
AND (
LEN(@Query) <> LEN(REPLACE(@Query, ADTOWN, ''))
AND CHARINDEX(CMPNAME, @Query) = (CHARINDEX(ADTOWN, @Query) - 1 - LEN(CMPNAME))
) -- making sure that the value of ADTOWN comes after CMPNAME
AND (
LEN(@Query) <> LEN(REPLACE(@Query, CMPTHEIRCODE, ''))
AND CHARINDEX(ADTOWN, @Query) = (CHARINDEX(CMPTHEIRCODE, @Query) - 1 - LEN(ADTOWN))
) -- making sure that the value of CMPTHEIRCODE comes after ADTOWN
希望它有所帮助。