Function-based indexes in SQL Server
Asked Answered
D

1

19

I'm trying to understand if function based indexes similar to the one's in Oracle or PostgreSQL exist in SQL Server

In PostgreSQL, I can create a function based index using by using the following syntax:

CREATE INDEX sample ON "TestDB" (("expression1" || ' ' || "expression2"));

I found a article where I found something called "index on computed columns" in SQL Server. Is this a function based index just like in Oracle/PostgreSQL? Can anybody provide me a sample query to create/view such an index?

Diplex answered 4/3, 2014 at 9:43 Comment(1)
It's similar to the function based index, except you're first naming the complete expression and associating it with the table (creating a computed column), and then as a separate step you create an index on that column. And your queries have to use that column to benefit from the index.Vise
D
31

I researched a bit further based on Damien's comment and found an answer that comes very close to matching Oracle's/PostgreSQL's function based indexes.

I have a table named PARCELS where I created a new column COMPUTEDPARCELS by using the alter statement as given below:

ALTER TABLE [PARCELS] ADD COMPUTEDPARCELS AS CONVERT(CHAR(8), [MAPNO], 112);

And then create an index on the computed column:

CREATE INDEX function_index ON [PARCELS](COMPUTEDPARCELS);

Of course the example is pretty simple but behaves just like a function based index.

Diplex answered 4/3, 2014 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.