I have been looking through some long running queries in our database with the aim of reducing the time taken. One thing I am looking at is the number of logical reads for each table, for each query, but I am not sure why the logical reads are so high for certain tables.
Rather than posting the long queries, I can show a very basic example that recreates the "problem". I have created this query:
SELECT p.Id
FROM
Products p
INNER JOIN StaffMembers sm ON p.CreatedById = sm.Id AND sm.IsDeleted = 0
WHERE p.DateCreated > '01-MAY-2021'
The logical reads for the StaffMembers
table is 6, the number of rows returned is 219
If I change the query slightly, to bring in another table:
SELECT p.Id
FROM
Products p
INNER JOIN StaffMembers sm ON p.CreatedById = sm.Id AND sm.IsDeleted = 0
INNER JOIN ProductCategories pc ON p.Id = pc.ProductId AND pc.IsDeleted = 0
WHERE p.DateCreated > '01-MAY-2021'
The logical reads for StaffMembers
is now 13, the number of rows is still 219.
The number of records returned by the query is exactly the same. From looking at the Execution Plan the Index Scan used for StaffMembers
is exactly the same. So, why is the logical reads higher for the second query?