Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create index for a table with combined primary key?

Tags:

indexing

mysql

I use MYSQL to create a table with combined primary key. For example, col_a and col_b are used together as the key. I will use col_a and col_b to get a row often, and also will use col_a or col_b separately to query some rows.

My questions is, how will I create the index for it? Will I need only one composite index, or three indexes with two indexes for the separated columns?

I think MYSQL will create the index for primary key. But will it create a composite index for (col_a, col_b), or create an index for each column?

like image 320
flypen Avatar asked Feb 22 '12 16:02

flypen


2 Answers

Multi-Column Indexes

For a multi-column index, the index works from left to right.

Given your primary key:

(col_a, col_b)

Here are the cases where it can be used:

  • col_a: YES. The index can be utilized when searching on col_a, since starting from left to right, col_a is the first column.
  • col_a & col_b: YES. When searching on col_a, and col_b, the index can also be utilized because starting from left to right, it's col_a, then col_b.
  • col_b:: NO. When searching only for col_b, the index can't be used because the first column in the index is col_a, and we can't skip over columns.

Given a larger index like this:

(col_a, col_b, col_c)
  • col_a: YES
  • col_a & col_b: YES
  • col_a & col_b & col_c: YES
  • col_a & col_c: NO. Can't skip over col_b in the index.
  • col_b:: NO. Can't skip col_a.
  • col_c:: NO. Can't skip col_a.
  • col_b & col_c: NO. Can't skip over col_a in the index.

Covering Indexes

To utilize an index as a "covering" index, the left-to-right pattern still applies.

Given the index (col_a, col_b), you can select both col_a and col_b, but search only on col_a (or col_a and col_b), and MySQL can use the index both for the search and for retrieving the data because col_a and col_b are both in the index. This saves a trip to the actual row data, since the values are stored in the index.

Given the index (col_a, col_b, col_c), you could not SELECT col_a and col_c if you were searching only on col_a and be able to use the covering index to return col_c, since you can't skip over col_b in the index. Instead, MySQL would visit the actual table data. However, you could SELECT col_a and col_c and use it as a covering index if you were searching on both col_a and col_b.

Primary Keys in "Covering" Indexes

Every secondary index has the primary key included in the index.

A single column index, as it relates to a covering index, can be thought of as a multi-column index in the following pattern. Given the primary key (col_a, col_b), a single column index on col_b, really looks like this (col_b)(col_a, col_b).

Given the above single column index, you could still select col_a and col_b, and search on col_b, and the index could be used as a covering index. Since the primary key includes col_a, MySQL could still use the index to return both columns without having to hit the actual table data.

What if I want to search on col_b?

Given the primary key (col_a, col_b), if you want to search on just col_b, you should add a single column index on col_b. As described above, it can still be used as a covering index if you are returning col_b and col_a.

like image 86
Marcus Adams Avatar answered Sep 18 '22 00:09

Marcus Adams


The composite primary key index on (col_a, col_b) will take care of situations where you are querying on both columns or querying on only col_a. For queries on only col_b, you'll need a separate index.

like image 20
Joe Stefanelli Avatar answered Sep 20 '22 00:09

Joe Stefanelli