Primary key, self-increment primary key, primary key index, unique index concept difference and performance difference

Source: Internet
Author: User

Conceptual differences:

Primary key: Refers to a column where the field is unique and is not a null value.

Primary key index: refers to the primary key, the primary key does not have a clear concept definition, the primary key is both a constraint, an index, a primary key is an index, is a unique index of the special type. when you create a primary key, the database creates a unique index by default for the primary key.

Self-increment primary key: The field type is numeric, self-increment, and is a primary key.

Unique index: The value of the indexed column must be unique, but a null value is allowed. The primary key is a unique index, so that's true. But the unique index is also the primary key for the anti-fire error, because the unique index allows null values, the primary key does not allow null values, so it cannot be said that the unique index is also the primary key.

Performance differences:

Through testing, the discovery of primary key, self-increment primary key, unique index query efficiency is not the same, of course, this gap is very small, summed up as:

Query: Unique index > Auto-increment primary key > Primary key (primary key index)

Insert: primary key > Auto-increment primary key > unique index

Test scenario:

Create 3 tables with the same name field: Id,name,money,traddate
Insert the same data for each 100W bar

Table A:id primary key, table B:id self-increment and primary key, Tablec:id create unique index

Inquire:

Performance: Unique index > Auto-increment primary key > Primary key (primary key index)

Table A (primary key): 0.06s

b Table (self-increasing primary key): 0.01s,

C table (unique index): 0.00s

Insert:

10W data, Bulk Insert, performance: primary key > Auto-increment primary key > unique index

Table A (primary key):38.408s

b Table (self-increasing primary key): 38.708s,

C table (unique index): 39.018s


Write so much, suddenly feel to do this test does not make any sense, have wood?


Primary key, self-increment primary key, primary key index, unique index concept difference and performance difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.