The Problem
I have a query that will always only hit the data from the past two weeks. It is business critical, so it must be fast. The table is inserted into very many times per hour. The table is huge, so I am very reluctant to write a non-filtered index for the sake of such a relatively small amount of data. What I really want to write is
CREATE NONCLUSTERED INDEX [FIX_MyTable] ON [MyTable]
(
[My],
[Stupid],
[Columns]
)
WHERE [StupidDate] > DATEADD(Day, -14, GETDATE())
but non-deterministic filters are illegal.
What I've Tried
- As above, filtered indexes are not applicable. Playing around with a columnstore index's
COMPRESSION_DELAYmight be helpful, but is not a solution in of itself. - Partitioning doesn't solve it. Like with filtered indexes, you cannot make a partitioning schema have dynamic boundaries.
- Indexed views can't solve it because they must be deterministic.
- Computed columns don't work. They don't inter-operate with anything useful, so they're no better than just indexing
[StupidDate] - Caching in the application is a partial solution at best and tempts towards the expensive non-filtered index solution.
- I could create a script to drop and redefine my filtered index each day, but I expected to find an easier path. For the same reason, I have ruled out any trickery with copying the table or writing triggers.