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?
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?
假设你在谈论我通常做的多对多关系表
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
或反之亦然.
创建的两个复合索引将涵盖这些查询。