Does using an auto increment of 1 on numeric PK hurts performance

auto-incrementperformanceprimary-key

A teacher of mine said that having an numeric PK with auto increment is a bad practice because it hurts performance.

He also said that Oracle database doesn't have an easy way to set up auto increment because of this.

He said that increment should be done on app instead of DB.

The performance penalty, if I remember correctly, would happen while inserting many rows, it had something to do with allocating a certain number of PKs IDs or something.

Is this true? If yes why?

Best Answer

I think the answer is that "it depends". In a lot of use cases, using an auto increment PK is good practice.

In InnoDB, the default storage engine in MySQL and MariaDB, the performance penalty of using the auto increment feature depends on the auto increment lock mode used. Two of the modes come at the cost of a table-level lock that is released once the statement has completed, which means only one statement can run at any one time. The third option (interleaved lock mode) doesn't lock the tables.

Oracle (12c) actually has auto incrementing keys (identity columns). In previous versions you had to use sequences, which can serve the same purpose, and more. They are a bit more cumbersome to use, though, than auto incrementing keys.

The suggestion that applications should be generating these ID numbers on their own without the aid of the RDBMS' sequence or auto increment features really isn't realistic in a lot of use cases.