designing new table for daily uploads - use unique constraint

*爱你&永不变心* 提交于 2020-01-06 20:38:07

问题


I am using SQL Server 2012 & am creating a table that will have 8 columns, types below

datetime
varchar(12)
varchar(6)
varchar(100)
float
float
int
datetime

Once a day (normally) there will be an upload of approx 10,000 rows of data. Going forward its possible it could be 100,000.

The rows will be unique if I group on the first three columns listed above. I have read I can use the unique constraint on multiple columns which will guarantee the rows are unique.

I think I'm correct in saying that the unique constraint by default sets up non-clustered index. Would a clustered index be better & assuming when the table starts to contain millions of rows this won't cause any issues?

My last question. By applying the unique constraint on my table I am right to say querying the data will be quicker than if the unique constraint wasn't applied (because of the non-clustering or clustering) & uploading the data will be slower (which is fine) with the constraint on the table?


回答1:


  1. Unique index can be non-clustered.
  2. Primary key is unique and can be clustered
  3. Clustered index is not unique by default
  4. Unique clustered index is unique :)

Mor information you can get from this guide.

So, we should separate uniqueness and index keys. If you need to kepp data unique by some column - create uniqe contraint (unique index). You'll protect your data. Also, you can create primary key (PK) on your columns - they will be unique also. But, there is a difference: all other indexies will use PK for referencing, so PK must be as short as possible. So, my advice - create Identity column (int or bigint) and create PK on it. And, create unique index on your unique columns. Querying data may become faster, if you do queries on your unique columns, if you do query on other columns - you need to create other, specific indexies.

So, unique keys - for data consistency, indexies - for queries.




回答2:


I think I'm correct in saying that the unique constraint by default sets up non-clustered index

TRUE

Would a clustered index be better & assuming when the table starts to contain millions of rows this won't cause any issues?

(1)if u need to make (datetime ,varchar(12), varchar(6)) Unique

(2)if you application or you will access rows using datetime or datetime ,varchar(12) or datetime ,varchar(12), varchar(6) in where condition ALL the time then have primary key on (datetime ,varchar(12), varchar(6))

by default it will put Uniqness and clustered index on all above three column.

but as you commented above:

the queries will vary to be honest. I imagine most queries will make use of the first datetime column

and you will deal with huge data and might join this table with other tables

then its better have a surrogate key( ever-increasing unique identifier ) in the table and to satisfy your Selects have Non-Clustered INDEXES

Surrogate Key vs Business Key

NON-CLUSTERED INDEX



来源:https://stackoverflow.com/questions/32137416/designing-new-table-for-daily-uploads-use-unique-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!