Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function-based indexes in SQL Server

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?

like image 426
Rahul Vijay Dawda Avatar asked Mar 04 '14 09:03

Rahul Vijay Dawda


People also ask

What are function based indexes?

Function-based indexes allow you to create an index based on a function or expression. The value of the function or expression is specified by the person creating the index and is stored in the index. Function-based indexes can involve multiple columns, arithmetic expressions, or maybe a PL/SQL function or C callout.

How do I create a functional index in SQL?

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.

Which of the following indexes can be considered as function based index?

Note that a function-based index can be a btree or bitmap index.


1 Answers

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.

like image 194
Rahul Vijay Dawda Avatar answered Sep 28 '22 22:09

Rahul Vijay Dawda