我编写了一个SQL查询,需要花费太多时间来执行。实际上,此查询从数百万条记录中提取记录。请建议我减少SQL查询执行时间的解决方案。查询如下:
SELECT
e.emailAddress, em.employeeId
FROM
Email e with (Nolock), employee em with (Nolock)
WHERE
e.emailAddress = em.email
AND em.employeeId = @employeeId
答案 0 :(得分:4)
首先,最好将查询编写为显式join
,而不是使用隐式连接语法:
SELECT e.emailAddress, em.employeeId
FROM Email e with (Nolock) join
employee em with (Nolock)
ON e.emailAddress = em.email
WHERE em.employeeId = @employeeId;
要使此查询运行最佳,您需要索引。首先想到的是employee(employeeId, emailaddress)
。
其次,这个查询不能做你想要的吗?
SELECT em.email, em.employeeId
FROM employee em with (Nolock)
WHERE em.employeeId = @employeeId;
使用上述索引,此查询应该运行得非常快。