Does the index indicate the DATEDIFF computation speed of two datetime values?

I have a table with three columns:

id | start_date | end_date

I have a view that shows idand DATEDIFF(hour, start_date, end_date):

id | timespan

By placing the index in the date columns, will this query be queried? Does this only work with DATEADD? If so, why? I understand that arithmetic queries like this are required TABLE SCAN.

+3
source share
1 answer

No. This is not true.

You can create a calculated column and create an index for it:

alter table <table> add duration as datediff(hour, start_date, end_date);

create index on <table>(duration);

Of course, the index will be most useful if durationused in where, a onor order by.

+4
source

All Articles