我有一个 CHAR(250) 列用作 varchar(24) 列的外键。
在 MySQL 中,我记得我可以创建一个指定 column(24) 的索引,以便在最左边的 24 个字符上创建一个索引。这在 MS SQL Server 上似乎是不可能的。
我的问题是这样的:
是否可以在 SQL Server 2008 上使用索引 View 来索引该列的子字符串,如果可以,是否会对表的性能产生任何副作用?
最佳答案
您可以创建一个持久计算列,然后对其进行索引,参见 Creating Indexes on Computed Columns
alter table add newcolumn as cast(oldcolumn as varchar(24)) persisted;
create index table_newcolumn on table (newcolumn);
关于sql - 在部分 CHAR 列上创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028868/