1

Should I always index all entries of a join table?

I imagine that there is not much benefit to indexing both columns together, and each column should have its own index?

4

1 回答 1

3

假设你在谈论我通常做的多对多关系表

CREATE TABLE FooBar
(
FooId int NOT NULL REFERENCES Foo(FooId),
BarId int NOT NULL REFERENCES Bar(BarId),
PRIMARY KEY (FooId, BarId ),
UNIQUE (BarId, FooId )
)

因为这既可以确保没有添加重复的行,并且会(在 SQL Server 和可能所有的 RDBMS 中)隐式地在两者上创建复合索引,FooId, BarId并且BarId,FooId通常您会希望在任一方向上寻找以获取所有Bar的 aFoo或反之亦然.

创建的两个复合索引将涵盖这些查询。

于 2013-07-28T11:00:47.940 回答