是否为table中的每个外键隐式创建了一个非聚集索引?
Is a non-clustered index implicitly created for each foreign key in a table?
如果我有一个像这样的 table:
CREATE TABLE MyTable
(
Id INT PRIMARY KEY IDENTITY(1, 1),
FooId INT NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data NVARCHAR(10) NOT NULL
);
可能会做出以下观察:
将在 table MyTable
的主键列 Id
上创建聚簇索引。 =27=]
另外,可以推断,会在tableFoo
上为其主键创建一个聚簇索引Id
.
问题:
在 table MyTable 上还会为外键 Foo.Id
创建索引吗?
换句话说,是为依赖 table.
上的每个外键 隐式创建的非聚集索引
换句话说,在这个架构中创建的索引总数是否为2,如下所示:
- table
MyTable
上主键 Id
的聚簇索引。
-
Foo
table 上的 Id
主键上的聚簇索引。
或者会不会有以下3个指标:
- table
MyTable
上主键 Id
的聚簇索引。
-
Foo
table 上的 Id
主键上的聚簇索引。
- 外键
Foo(Id)
上的非聚集索引table上MyTable
.
我的问题与 Microsoft SQL Server 2014 有关。
不,它不是自动创建的。手动创建它是一个好习惯:
The Benefits of Indexing Foreign Keys
Unlike primary key constraints, when a foreign key constraint is defined for a table, an index is not created by default by SQL Server.
However, it's not uncommon for developers and database administrators to add them manually
CREATE TABLE MyTable(
Id int PRIMARY KEY IDENTITY(1, 1),
FooId int NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data nvarchar(10) NOT NULL,
);
exec sp_helpIndex 'MyTable'
index_name index_description index_keys
PK__MyTable__3214EC0742A69968 clustered, unique, primary key located on PRIMARY Id
显式索引创建:
CREATE TABLE MyTable (
Id int PRIMARY KEY IDENTITY(1, 1),
FooId int NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data nvarchar(10) NOT NULL,
INDEX FK_FooId nonclustered(FooId) -- inline syntax
);
exec sp_helpIndex 'MyTable'
index_name index_description index_keys
FK_FooId nonclustered located on PRIMARY FooId
PK__MyTable__3214EC0779B032FB clustered, unique, primary key located on PRIMARY Id
如果我有一个像这样的 table:
CREATE TABLE MyTable
(
Id INT PRIMARY KEY IDENTITY(1, 1),
FooId INT NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data NVARCHAR(10) NOT NULL
);
可能会做出以下观察:
将在 table
MyTable
的主键列Id
上创建聚簇索引。 =27=]另外,可以推断,会在table
Foo
上为其主键创建一个聚簇索引Id
.
问题:
在 table MyTable 上还会为外键 Foo.Id
创建索引吗?
换句话说,是为依赖 table.
上的每个外键 隐式创建的非聚集索引换句话说,在这个架构中创建的索引总数是否为2,如下所示:
- table
MyTable
上主键Id
的聚簇索引。 -
Foo
table 上的Id
主键上的聚簇索引。
或者会不会有以下3个指标:
- table
MyTable
上主键Id
的聚簇索引。 -
Foo
table 上的Id
主键上的聚簇索引。 - 外键
Foo(Id)
上的非聚集索引table上MyTable
.
我的问题与 Microsoft SQL Server 2014 有关。
不,它不是自动创建的。手动创建它是一个好习惯:
The Benefits of Indexing Foreign Keys
Unlike primary key constraints, when a foreign key constraint is defined for a table, an index is not created by default by SQL Server.
However, it's not uncommon for developers and database administrators to add them manually
CREATE TABLE MyTable(
Id int PRIMARY KEY IDENTITY(1, 1),
FooId int NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data nvarchar(10) NOT NULL,
);
exec sp_helpIndex 'MyTable'
index_name index_description index_keys
PK__MyTable__3214EC0742A69968 clustered, unique, primary key located on PRIMARY Id
显式索引创建:
CREATE TABLE MyTable (
Id int PRIMARY KEY IDENTITY(1, 1),
FooId int NOT NULL FOREIGN KEY REFERENCES Foo(Id),
Data nvarchar(10) NOT NULL,
INDEX FK_FooId nonclustered(FooId) -- inline syntax
);
exec sp_helpIndex 'MyTable'
index_name index_description index_keys
FK_FooId nonclustered located on PRIMARY FooId
PK__MyTable__3214EC0779B032FB clustered, unique, primary key located on PRIMARY Id