SQL Server 中基于函数的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22168213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function-based indexes in SQL Server
提问by Rahul Vijay Dawda
I'm trying to understand if function based indexes similar to the one's in Oracle or PostgreSQL exist in SQL Server
我试图了解 SQL Server 中是否存在类似于 Oracle 或 PostgreSQL 中的基于函数的索引
In PostgreSQL, I can create a function based index using by using the following syntax:
在 PostgreSQL 中,我可以使用以下语法创建基于函数的索引:
CREATE INDEX sample ON "TestDB" (("expression1" || ' ' || "expression2"));
I found a articlewhere 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?
我找到了一篇文章,其中在 SQL Server 中找到了称为“计算列索引”的内容。这是一个基于函数的索引,就像在 Oracle/PostgreSQL 中一样吗?任何人都可以向我提供一个示例查询来创建/查看这样的索引吗?
回答by Rahul Vijay Dawda
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.
我根据 Damien 的评论进行了进一步研究,并找到了一个非常接近匹配 Oracle 的/PostgreSQL 的基于函数的索引的答案。
I have a table named PARCELS
where I created a new column COMPUTEDPARCELS
by using the alter statement as given below:
我有一个名为的表PARCELS
,我COMPUTEDPARCELS
使用如下所示的 alter 语句在其中创建了一个新列:
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.
当然,这个例子非常简单,但行为就像一个基于函数的索引。