An index in a MySQL table works like an index in a book.
Let's say you have a book about databases and you want to find some information about, say, storage. Without an index (assuming no other aid, such as a table of contents) you'd have to go through the pages one by one, until you found the topic (that's a "full table scan"). On the other hand, an index has a list of keywords, so you'd consult the index and see that storage is mentioned on pages 113-120, 231, and 354. Then you could flip to those pages directly, without searching (that's a search with an index, somewhat faster).
Of course, the usefulness of the index depends on many things - a few examples, using the simile above:
-- Create an index for column 'name' in table 'my_table'
CREATE INDEX idx_name ON my_table(name);
A unique index prevents the insertion of duplicated data in a table. NULL
values can be inserted in the columns that form part of the unique index (since, by definition, a NULL
value is different from any other value, including another NULL
value)
-- Creates a unique index for column 'name' in table 'my_table'
CREATE UNIQUE INDEX idx_name ON my_table(name);
-- Drop an index for column 'name' in table 'my_table'
DROP INDEX idx_name ON my_table;
This will create a composite index of both keys, mystring
and mydatetime
and speed up queries with both columns in the WHERE
clause.
CREATE INDEX idx_mycol_myothercol ON my_table(mycol, myothercol)
Note: The order is important! If the search query does not include both columns in the WHERE
clause, it can only use the leftmost index. In this case, a query with mycol
in the WHERE
will use the index, a query searching for myothercol
without also searching for mycol
will not. For more information check out this blog post.
Note: Due to the way BTREE's work, columns that are usually queried in ranges should go in the rightmost value. For example, DATETIME
columns are usualy queried like WHERE datecol > '2016-01-01 00:00:00'
. BTREE indexes handle ranges very efficiently but only if the column being queried as a range is the last one in the composite index.
CREATE TABLE (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY(id),
... );
Main notes:
INSERT
, or specify it as NULL
.Subtle notes:
MAX(id)+1
.auto_increment_offset
and auto_increment_increment
.PRIMARY KEY
and simply do INDEX(id)
. (This is an optimization in some situations.)AUTO_INCREMENT
as the "PARTITION
key" is rarely beneficial; do something different.INSERT IGNORE
(with dup key), REPLACE
(which is DELETE
plus INSERT
) and others. ROLLBACK
is another cause for gaps in ids.COMMIT
order.-- Create simple index
CREATE INDEX index_name ON table_name(column_name1 [, column_name2, ...])
-- Create unique index
CREATE UNIQUE INDEX index_name ON table_name(column_name1 [, column_name2, ...]
-- Drop index
DROP INDEX index_name ON tbl_name [algorithm_option | lock_option] ...
algorithm_option: ALGORITHM [=] {DEFAULT|INPLACE|COPY}
lock_option: LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}