SQL Server 2005 外键和索引
我有一个关于数据库建模中外键表索引的一般性问题。如果我有一个表 TABLE_A,创建为:
CREATE TABLE [dbo].[TABLE_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RelatedTableBID] [int] NOT NULL,
CONSTRAINT [PK__TABLE_A] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[TABLE_A] WITH CHECK
ADD CONSTRAINT [TABLE_A__RelatedTableB]
FOREIGN KEY([RelatedTableBID])
REFERENCES [dbo].[TABLE_B] ([ID])
和 Table_B 创建为:
CREATE TABLE [dbo].[TABLE_B](
[ID] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__TABLE_B] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
如果我在引用表(表 A)引用列(RelatedTableBID)上创建索引,实际上会更高效吗?比如:
CREATE INDEX TABLE_A_FK_INDEX1 on TABLE_A(RelatedTableBID)
或者我是在倒着思考这个问题?看起来,由于被引用的列本身就是一个聚集索引,因此在连接期间不应该出现任何问题 - 如果有的话,只有从 TABLE_B 中删除似乎容易受到性能不佳的影响。
感谢您帮助我纠正错误。
-穆斯塔法
编辑
因此,一般来说,如果我在查询时一致地加入或使用 where 子句中的列,我是否应该考虑在其上添加索引? 一些最佳实践和创建数据库索引的“经验法则”? 听起来这通常是一个合理的决定。
I have a general question regarding table indices on foreign keys in database modeling. If I have a table, TABLE_A, created as:
CREATE TABLE [dbo].[TABLE_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RelatedTableBID] [int] NOT NULL,
CONSTRAINT [PK__TABLE_A] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[TABLE_A] WITH CHECK
ADD CONSTRAINT [TABLE_A__RelatedTableB]
FOREIGN KEY([RelatedTableBID])
REFERENCES [dbo].[TABLE_B] ([ID])
and Table_B as:
CREATE TABLE [dbo].[TABLE_B](
[ID] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__TABLE_B] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
Will it in practice be more efficient if I create an index on the referencing table (Table A) referencing column (RelatedTableBID)? As in:
CREATE INDEX TABLE_A_FK_INDEX1 on TABLE_A(RelatedTableBID)
Or am I thinking about this backwards? It seems that since the column being referenced is itself a clustered index, there shouldn't be any issues during joins - if anything, only deletes from TABLE_B seem susceptible to poor performance.
Thanks for any help in setting me straight.
-Mustafa
EDIT
So, in general, if I'm ever joining on or using a column in a where clause consistently when querying, should I consider adding an index on it? What are some best practices and "rules of thumb" for creating database indexes? Sounds like that's a generally sound decision.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说得对。每当您需要连接 TABLE_A 和 TABLE_B 时,外键上的索引应该会有所帮助。
You've got it right. That index on the foreign key should help whenever you need to join TABLE_A and TABLE_B.
不,您正在寻找的是第三张桌子。如果您要构建多对多关系,那么如果没有第三个表,那么就不可能没有低效的数据库设计。
因此:
表“用户”:uid、第一个、中间、最后一个
表“地址”:援助、街道、城市、州、国家等。
表'users-addresses':id,uid,aid
然后您可以在第三个表中进行多对多关联。这通常称为数据库规范化。
因此,您的查询关联查询将类似于:
SELECT * FROM users,addresses JOIN users-addresses ON users.uid=users-addresses.uid AND 地址.aid = users-addresses.aid WHERE users.uid='1' 或类似的东西
..将为您提供用户“1”的所有用户信息和地址
No. What you're looking for is a third table. If it's a many-to-many relationship you're looking to build, it's not possible to do without a disgustingly ineffecient DB design without a third table.
Thus:
Table 'users': uid, first, middle, last
Table 'addresses': aid, street, city, state, country, etc.
Table 'users-addresses': id, uid, aid
Then you can make many to many associations in the third table. This is typically called database normalization.
So then your query associative query would look something like:
SELECT * FROM users,addresses JOIN users-addresses ON users.uid=users-addresses.uid AND addresses.aid = users-addresses.aid WHERE users.uid='1' or something like that
..would give you all user information and addresses for user '1'