Is it bad to have a non-clustered index that contains the primary key from the clustered index?

后端 未结 4 1676
长情又很酷
长情又很酷 2021-01-05 06:59

If you have a table with a clustered index on the Primary Key (int), is it redundant and bad to have one (ore more) non-clustered indexes that include that primary key colum

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 07:57

    Actually there could be valid reasons to create a non-clustered index identical with the clustered one. The reason is that clustered indexes carry the baggage of the row data and this can make very poor row density. Ie. you can have 2-3 rows per page due to wide fields that are not in the clustered key, but the clustered index key is only, say, 20 bytes. Having a non-clustered index on exactly the same key(s) and order as the clustered index would give a density of 2-3 hundreds of keys per page. A lot of aggregate queries typical for an OLAP/BI workload can be answered more efficiently by the non-clustered index, simply because it reduces the I/O by hundreds of times.

    As for non-clustered indexes that contain parts of the clustered key, or even the same keys but in different order, then all bets are off as they obviously could be used for a multitude of queries.

    So the answer to your question is: It Depends.

    For a more precise answer you'll have to share the exact schema of your table(s) and the exact queries involved.

提交回复
热议问题