Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MySQL index columns?

When do you use each MySQL index type?

  • PRIMARY - Primary key columns?
  • UNIQUE - Foreign keys?
  • INDEX - ??

For really large tables, do indexed columns improve performance?

like image 538
Robin Rodricks Avatar asked Oct 04 '09 20:10

Robin Rodricks


People also ask

How do I index a column in MySQL?

You can create a simple index on a table. Just omit the UNIQUE keyword from the query to create a simple index. A Simple index allows duplicate values in a table. If you want to index the values in a column in a descending order, you can add the reserved word DESC after the column name.

Why do we index columns 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.

How indexing is used in MySQL with example?

Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update).

Which column should be indexed in MySQL?

column the first in the composite index (in my case that would be the Showtime column). The only problem with that is that the index can only be used by the database if the first column is included in the search query, which it currently isn't in either of my queries.


1 Answers

Primary

The primary key is - as the name suggests - the main key of a table and should be a column which is commonly used to select the rows of this table. The primary key is always a unique key (unique identifier). The primary key is not limited to one column, for example in reference tables (many-to-many) it often makes sense to have a primary key including two or more columns.

Unique

A unique index makes sure your DBMS doesn't accept duplicate entries for this column. You ask 'Foreign keys?' NO! That would not be useful since foreign keys are per definition prone to be duplicates, (one-to-many, many-to-many).

Index

Additional indexes can be placed on columns which are often used for SELECTS (and JOINS) which is often the case for foreign keys. In many cases SELECT (and JOIN) queries will be faster, if the foreign keys are indexed.

Note however that - as SquareCog has clarified - Indexes get updated on any modifications to the data, so yes, adding more indexes can lead to degradation in INSERT/UPDATE performance. If indexes didn't get updated, you would get different information depending on whether the optimizer decided to run your query on an index or the raw table -- a highly undesirable situation.

This means, you should carefully assess the usage of indices. One thing is sure on the basis of that: Unused indices have to be avoided, resp. removed!

like image 189
markus Avatar answered Sep 29 '22 10:09

markus