简体   繁体   中英

Including column from clustered index into nonclustered?

I am building a database to compile data from 40 different servers. These servers all have the same design but use a platform that was meant for a single location (so no identifying IDs per location). 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?

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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