MySQL 5.0 Indexes - Unique and Not Unique

What is the difference between a unique and non-unique MySQL index in terms of performance?

Suppose I want to create an index for a combination of 2 columns, and the combination is unique, but I create a non-unique index. Will it affect the performance or memory usage of MySQL?

The same question, is there a difference between a primary key and a unique index?

+57
mysql indexing
Dec 23 '08 at 14:27
source share
2 answers

UNIQUE and PRIMARY KEY are constraints, not indexes. Although most databases implement these restrictions using an index. The additional overhead of restrictions in addition to the index is not significant, especially when you are calculating the cost of tracking and correcting inadvertent duplicates when (and not if) they arise.

Indexes are usually more effective if you have high selectivity. This is the ratio of the number of different values ​​to the total number of rows.

For example, a column for a social security number might have 1 million rows with 1 million different values. Thus, selectivity of 1,000,000 / 1,000,000 = 1.0 (although there are rare historical exceptions, SSNs must be unique).

But the other column in this gender table can have only two different values ​​of more than 1 million rows. 2/1000000 = very low selectivity.

An index with a UNIQUE or PRIMARY KEY constraint guarantees selectivity of 1.0, so it will always be as effective as the index.

You asked about the difference between a primary key and a unique constraint. This basically means that you can have only one primary key constraint per table (even if this constraint definition includes several columns), while you can have several unique constraints. A column with a unique constraint can be NULL, while columns in the primary key constraints must not be NULL. Otherwise, the primary key and uniqueness are very similar in their implementation and use.

You asked in a comment about whether to use MyISAM or InnoDB. In MySQL, they use the term "storage engine." There are many subtle differences between the two storage engines, but the main ones are:

  • InnoDB supports transactions, so you can roll back or commit changes. MyISAM effectively always auto-commutes.
  • InnoDB provides foreign key constraints. MyISAM does not apply or even retain foreign key restrictions.

If these features are necessary for your application, you should use InnoDB.




To answer your comment, it is not so simple. InnoDB is actually faster than MyISAM in quite a few cases, so it depends on your application combining selections, updates, concurrent queries, indexes, buffer configuration, etc.

See http://www.mysqlperformanceblog.com/2007/01/08/innodb-vs-myisam-vs-falcon-benchmarks-part-1/ for a very thorough comparison of storage performance. InnoDB often wins over MyISAM, which is clearly impossible to say that one is faster than the other.

As with most performance questions, the only way to answer it for your application is to check both configurations using the application and a representative sample of the data and measure the results.

+124
Dec 23 '08 at 19:25
source share

In a non-unique index, which is simply a unique and unique index? I'm not sure, but I think not so much. The optimizer should study the power of the index and use it (it will always be the number of rows for a unique index).

As for the primary key, maybe quite a lot, but it depends on which engine you use.

The InnoDB engine (which is used by many people) always clusters strings on the primary key. This means that the PC is essentially matched with the actual row data. If you do a lot of PK searches (or, indeed, range scanning, etc.), this is a good thing because it means you won’t have to extract as many blocks from disk.

A unique PK-free index will never be clustered in InnoDB.

On the other hand, some other mechanisms (in particular, MyISAM) do not cluster PK, so the primary key is like a regular unique index.

+2
Dec 23 '08 at 14:33
source share



All Articles