Isaac
Lifelong Learner
- Local time
- Today, 04:47
- Joined
- Mar 14, 2017
- Messages
- 11,055
Up to this point, I've had a sort of "general rule" for myself. A rule of thumb if you will, not specifically defensible in every situation, but one that I thought was true often enough to just avoid doing it rather than take the effort to assess each time.
And that is, to avoid using Functions in the Where clause, such as:
where Year(columnName) = 2022
or
where IsNull(columnName,'')=''
Preferring instead to replace each of those, respectively, with something likely about like this:
where (columnName between '2022-01-01' and '2022-12-31 11:59:59.999')
and
where columnName is null or columnName = ''
Because it was my understanding, doubtless a comment made by someone more skilled than I a long time ago, that such functions used in the Where clause will often require a complete table scan. (which kind of makes sense to my brain logically).
However, I was just reviewing a junior colleague's query to make suggestions, and their Where clause has where datepart(year,columnname) = 2022
Wanting to be more sure before I said anything, I checked the execution plan, and I don't see anything that indicates to me a labor-intensive occurrence connected to that filter.
Am I missing anything, or has SQL Server 'improved' that area, was I always wrong the whole time, or does it "just depend" a bit more than I thought?
And that is, to avoid using Functions in the Where clause, such as:
where Year(columnName) = 2022
or
where IsNull(columnName,'')=''
Preferring instead to replace each of those, respectively, with something likely about like this:
where (columnName between '2022-01-01' and '2022-12-31 11:59:59.999')
and
where columnName is null or columnName = ''
Because it was my understanding, doubtless a comment made by someone more skilled than I a long time ago, that such functions used in the Where clause will often require a complete table scan. (which kind of makes sense to my brain logically).
However, I was just reviewing a junior colleague's query to make suggestions, and their Where clause has where datepart(year,columnname) = 2022
Wanting to be more sure before I said anything, I checked the execution plan, and I don't see anything that indicates to me a labor-intensive occurrence connected to that filter.
Am I missing anything, or has SQL Server 'improved' that area, was I always wrong the whole time, or does it "just depend" a bit more than I thought?