我正在将 Jet 数据库升级到 SQL Server Express 2008 R2,在此之前,我正在重新评估架构(它是在 1997-98 年设计的,设计它的人(即我)是一个白痴!)。
我的问题是关于带有两列复合键的 N:N 连接表。在 Jet 中,两列复合键的第一列上的联接将使用复合索引,但第二列上的联接不会使用复合索引,因此一般来说,在具有大量记录的大型 N:N 联接表的 Jet 数据库中,除了复合索引之外,我还在第二列上添加了第二个非唯一索引。
这在 SQL Server 中是个好主意吗?
(也许这在 Jet 中不是一个好主意?)
I'm upsizing a Jet database to SQL Server Express 2008 R2 and before doing so, I'm re-evaluating the schema (it was designed in 1997-98, and the guy who designed it (i.e., me) was something of a moron!).
My question is about N:N join tables with a two-column composite key. In Jet, joins on the first column of a two-column composite key will use the composite index, but joins on the second column will not, so in general, in Jet databases with large N:N join tables with reasonably large numbers of records, in addition to the composite index I add a second, non-unique index on the second column.
Is this a good idea in SQL Server?
(Maybe it's not a good idea in Jet?)
发布评论
评论(3)
同样的规则也适用于 SQL Server。如果您在 (ColumnA, ColumnB) 上有索引,则仅对 ColumnA 或 ColumnA 和 ColumnB 一起进行查询可以使用该索引,但仅对 ColumnB 进行查询则不能。如果只需要连接 ColumnB,那么您绝对应该创建索引。
The same rules apply in SQL Server. If you have an index on (ColumnA, ColumnB) a query on only ColumnA or ColumnA and ColumnB together can use the index, but a query on only ColumnB cannot. If there is a need to join on just ColumnB, then you should definitely create the index.
如果列
(A,B)
上有复合索引,则任何查找、范围扫描、排序或聚合操作都不能将其用于仅包含B<的表达式/代码>。对于 SQL Server 来说是这样,就像对于 Jet(红色)驱动程序来说也是如此(我认为对于跳过扫描
操作中使用它。所以答案是您需要单独对
(B)
建立单独的索引。If you have a composite index on columns
(A,B)
no seek, range scan, sort or aggregate operation can use it for expressions that contain onlyB
. This is true for SQL Server, just as it was true for Jet (Red) drivers (and I think for Jet Blue also). Some other engines might use it in a so calledskip scan
operation.So the answer is that you need separate indexes on
(B)
alone.为了帮助您更多,只是一个提示,在 SQL Server 中使用管理工作室,您可以通过“显示估计执行计划”来评估性能。它展示了索引和连接是如何工作的。
您还可以使用 DTA(数据库引擎优化顾问)获取更多信息和优化。
To help you more, just a tip, in SQL server using the Managment studio you can evaluate the performance by "Display Estimated execution plan". It shown how the indexs and join works.
Also you can use the DTA (Database Engine Tuning Advisor) for more info and optimization.