patternsqlModerate
Are 'Functional Indexes' Supported in MS SQL Server?
Viewed 0 times
serveraresqlindexesfunctionalsupported
Problem
I've just come across expression based indexes (aka "functional indexes") when reading through the documentation for PostgreSQL (I wasn't aware of such indexes previously).
I'm just wondering if there is anything that is similar/equivalent to this in Microsoft SQL Server? I have read the SQL Server documentation on indexing and I don't see anything that is obviously similar/equivalent.
I'm just wondering if there is anything that is similar/equivalent to this in Microsoft SQL Server? I have read the SQL Server documentation on indexing and I don't see anything that is obviously similar/equivalent.
Solution
While there isn't a feature of Functional Indexes in SQL Server per se, one can mimic a similar implementation via indexing on top of a Computed Column. Computed columns allow functions to be utilized. They also can even be persisted such that the result of a function is stored in the column on disk (as opposed to being evaluated every time at query runtime).
So long as the requirements of the document I linked above are met, it's possible to index on top of a function result similar to Functional Indexes from PostgreSQL. One of the most important requirements is that the function is a deterministic function, meaning it outputs the same result for the same input always. For example,
So long as the requirements of the document I linked above are met, it's possible to index on top of a function result similar to Functional Indexes from PostgreSQL. One of the most important requirements is that the function is a deterministic function, meaning it outputs the same result for the same input always. For example,
ISNULL() is deterministic because the same input parameter will always return the same outputted result. But the GETDATE() function is nondeterministic because the same input (which happens to be no input for this particular function) always results in a different outputted result, one day to the next, when it's called.Context
StackExchange Database Administrators Q#305100, answer score: 14
Revisions (0)
No revisions yet.