Reputation: 1
I wrote a SQL query which takes too much time to execute. Actually this query fetches records from millions record. Please suggest me the solution which reduce the execution time of SQL query. The query is given below.
SELECT
e.emailAddress, em.employeeId
FROM
Email e with (Nolock), employee em with (Nolock)
WHERE
e.emailAddress = em.email
AND em.employeeId = @employeeId
Upvotes: 0
Views: 807
Reputation: 1269683
First, it is better to write your query as an explicit join
, rather than using implicit join syntax:
SELECT e.emailAddress, em.employeeId
FROM Email e with (Nolock) join
employee em with (Nolock)
ON e.emailAddress = em.email
WHERE em.employeeId = @employeeId;
To make this query run best, you want indexes. The first that comes to mind is employee(employeeId, emailaddress)
.
Second, doesn't this query do what you want?
SELECT em.email, em.employeeId
FROM employee em with (Nolock)
WHERE em.employeeId = @employeeId;
This query should run quite fast with the aforementioned index.
Upvotes: 4