Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL: Index keyword in create table and when to use it

Tags:

mysql

What does index keyword mean and what function it serves? I understand that it is meant to speed up querying, but I am not very sure how this can be done.

When how to choose the column to be indexed?

A sample of index keyword usage is shown below in create table query:

CREATE TABLE `blog_comment` (     `id` INTEGER  NOT NULL AUTO_INCREMENT,     `blog_post_id` INTEGER,     `author` VARCHAR(255),     `email` VARCHAR(255),     `body` TEXT,     `created_at` DATETIME,     PRIMARY KEY (`id`),     INDEX `blog_comment_FI_1` (`blog_post_id`),     CONSTRAINT `blog_comment_FK_1`         FOREIGN KEY (`blog_post_id`)         REFERENCES `blog_post` (`id`) )Type=MyISAM 

;

like image 375
Graviton Avatar asked Feb 18 '09 15:02

Graviton


People also ask

When should you create an index on a table?

Index the Correct Tables and Columns Create an index if you frequently want to retrieve less than about 15% of the rows in a large table. This threshold percentage varies greatly, however, according to the relative speed of a table scan and how clustered the row data is about the index key.

What is the use of CREATE INDEX in MySQL?

In MySQL, an index can be created on a table when the table is created with CREATE TABLE command. Otherwise, CREATE INDEX enables to add indexes to existing tables. A multiple-column index can be created using multiple columns. The indexes are formed by concatenating the values of the given columns.

Why and when should we use indexes in MySQL?

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more this costs.

What is the use of index in a table?

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.


2 Answers

I'd recommend reading How MySQL Uses Indexes from the MySQL Reference Manual. It states that indexes are used...

  • To find the rows matching a WHERE clause quickly.
  • To eliminate rows from consideration.
  • To retrieve rows from other tables when performing joins.
  • To find the MIN() or MAX() value for a specific indexed column.
  • To sort or group a table (under certain conditions).
  • To optimize queries using only indexes without consulting the data rows.

Indexes in a database work like an index in a book. You can find what you're looking for in an book quicker, because the index is listed alphabetically. Instead of an alphabetical list, MySQL uses B-trees to organize its indexes, which is quicker for its purposes (but would take a lot longer for a human).

Using more indexes means using up more space (as well as the overhead of maintaining the index), so it's only really worth using indexes on columns that fulfil the above usage criteria.

In your example, the id and blog_post_id columns both uses indexes (PRIMARY KEY is an index too) so that the application can find them quicker. In the case of id, it is likely that this allows users to modify or delete a comment quickly, and in the case of blog_post_id, so the application can quickly find all comments for a given post.

You'll notice that there is no index for the email column. This means that searching for all blog posts by a particular e-mail address would probably take quite a long time. If searching for all comments by a particular e-mail address is something you'd want to add, it might make sense to add an index to that too.

like image 86
David Grant Avatar answered Sep 21 '22 00:09

David Grant


This keyword means that you are creating an index on column blog_post_id along with the table.

Queries like that:

SELECT * FROM blog_comment WHERE blog_post_id = @id 

will use this index to search on this field and run faster.

Also, there is a foreign key on this column.

When you decide to delete a blog post, the database will need check against this table to see there are no orphan comments. The index will also speed up this check, so queries like

DELETE FROM blog_post WHERE ... 

will also run faster.

like image 20
Quassnoi Avatar answered Sep 23 '22 00:09

Quassnoi