sql primary key and index

后端 未结 11 1412
误落风尘
误落风尘 2020-12-04 11:36

Say I have an ID row (int) in a database set as the primary key. If I query off the ID often do I also need to index it? Or does it being a primary key mean it\'s already in

11条回答
  •  暖寄归人
    2020-12-04 12:29

    Well in SQL Server, generally, primary key is automatically indexed. This is true, but it not guaranteed of faster query. The primary key will give you excellent performance when there is only 1 field as primary key. But, when there are multiple field as primary key, then the index is based on those fields.

    For example: Field A, B, C are the primary key, thus when you do query based on those 3 fields in your WHERE CLAUSE, the performance is good, BUT when you want to query with Only C field in the WHERE CLAUSE, you wont get good performance. Thus, to get your performance up and running, you will need to index C field manually.

    Most of the time, you wont see the issue till you hits more than 1 million records.

提交回复
热议问题