我有一个正在继承并尝试优化的搜索查询。我很想知道是否有人对此有任何最佳实践和建议。生产服务器仍然是 SQL Server 2000。
该查询是一个高级客户搜索存储过程,它接受 5 个不同的搜索条件参数(即名字、姓氏、地址、电话等)来搜索数百万条记录表。WHERE 子句中的所有连接列和列都有索引。此外,初始查询将记录转储到表变量中以获取分页容量。
INSERT INTO @tempCustTable (CustomerID, FirstName, LastName, City, StateProvince, Zip, PhoneNumber)
SELECT DISTINCT cu.CustomerID, cu.FirstName, cu.LastName, a.City,
a.StateProvince, a.Zip, p.PhoneNumber
FROM Customer cu WITH(NOLOCK)
LEFT OUTER JOIN Address a WITH(NOLOCK) ON cu.CustomerID = a.CustomerID
LEFT OUTER JOIN Phone p WITH(NOLOCK) ON cu.CustomerID = p.CustomerID
WHERE (cu.LastName = @LastName OR cu.LastName LIKE @LastName + '%')
AND (@FirstName IS NULL OR cu.FirstName = @FirstName OR cu.FirstName LIKE @FirstName + '%')
AND (@StateProvince = '' OR a.StateProvince LIKE @StateProvince)
AND (@City = '' OR a.City LIKE @City + '%')
AND (@Zip = '' OR a.Zip = @Zip OR a.Zip LIKE @Zip + '%')
ORDER BY cu.LastName, cu.FirstName
有人对如何提高查询性能有任何建议吗?