简体   繁体   English

包括从聚簇索引到非聚簇的列?

[英]Including column from clustered index into nonclustered?

I am building a database to compile data from 40 different servers. 我正在构建一个数据库来编译来自40个不同服务器的数据。 These servers all have the same design but use a platform that was meant for a single location (so no identifying IDs per location). 这些服务器都具有相同的设计,但使用的是专用于单个位置的平台(因此每个位置不需要识别ID)。 I have created keys for each location for the data that gets compiled, but due to this almost all my tables have composite primary keys with designs such as: 我为每个位置创建了已编译数据的密钥,但由于这几乎所有我的表都有复合主键,其设计如下:

LocationID, TransactionID, CustomerID, ServiceID, TransDate, Amount

and

LocationID, CustomerID, FirstName, LastName, Balance

for columns. 对于列。

My question is, in regards to my nonclustered indexes. 我的问题是,关于我的非聚集索引。 Is it best to include the LocationID column if I planned to, for example, create an index on the CustomerID on my Transaction table even though the LocationID is in the clustering key of the table already? 如果我计划在例如在Transaction表上的CustomerID上创建索引,那么最好包括LocationID列,即使LocationID已经在表的集群键中了吗?

Hope that made sense, thanks in advance! 希望有意义,提前谢谢!

Clustering is really about how you're going to retrieve data, when all's said and done. 在完成所有操作时,群集实际上是关于如何检索数据。 If you're going to be retrieving data based upon the CustomerID and LocationID then sure, you'd want to include those in an index of some sort. 如果您要根据CustomerIDLocationID检索数据,那么您肯定要将它们包含在某种索引中。 The problem, though, is that you've got to 1) load your data in, which may end up re-balancing your clustered index (ie, if you loaded in LocationID 2 prior to loading in LocationID 1), and then 2) define your queries to include that LocationID in everything. 但问题是,你得1)加载数据,这最终可能会重新平衡你的聚集索引(也就是说,如果你在加载LocationID 2装载之前在LocationID 1),然后2)定义您的查询以在所有内容中包含该LocationID

My gut feeling would be that it'd be cleaner to re-key everything whilst keeping the same data structures you already have, rather than trying to incorporate LocationID into every table and into every query join. 我的直觉是,在保留您已有的相同数据结构的同时重新键入所有内容更加清晰,而不是尝试将LocationID合并到每个表和每个查询连接中。 Whether it's easier or not ... well, I think it may be easier, as well, to simply re-key the data in transit, rather than altering the entire schema. 是否更容易......好吧,我认为简单地重新键入传输中的数据可能更容易,而不是改变整个架构。 It may seem simple to simply add LocationID , but I suspect it's going to bite you, particularly on outer join queries & things that are a bit more nuanced than simple equijoins. 简单地添加LocationID 似乎很简单,但我怀疑它会咬你,特别是外连接查询以及比简单的equijoins更微妙的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM