What you need to know about creating MySQL indexes

What you need to know about creating MySQL indexes

Preface:

In MySQL, basically every table has an index, and sometimes you need to add different indexes based on different business scenarios. The establishment of indexes is very important for the efficient operation of the database. This article will introduce the knowledge and precautions related to creating indexes.

1. Create index method

You can create an index when you create a table, or you can use the alter table or create index statement to create an index after the table is created. The following are some common index creation scenarios.

# Specify the index when creating the table CREATE TABLE `t_index` (
  `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Auto-increment primary key',
  `col1` int(11) NOT NULL,
  `col2` varchar(20) NOT NULL,
  `col3` varchar(50) NOT NULL,
  `col4` int(11) NOT NULL,
 `col5` varchar(50) NOT NULL,
  PRIMARY KEY (`increment_id`),
  UNIQUE KEY `uk_col1` (`col1`),
  KEY `idx_col2` (`col2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Test index';

# Create index (two methods)
# Normal index alter table `t_index` add index idx_col3 (col3);
 create index idx_col3 on t_index(col3);
# Unique index alter table `t_index` add unique index uk_col4 (col4);
create unique index uk_col4 on t_index(col4);
# Joint index alter table `t_index` add index idx_col3_col4 (col3,col4);
create index idx_col3_col4 on t_index(col3,col4);
# Prefix index alter table `t_index` add index idx_col5 (col5(20));
 create index idx_col5 on t_index(col5(20));

# View the table index mysql> show index from t_index;
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+------------+
| t_index | 0 | PRIMARY | 1 | increment_id | A | 0 | NULL | NULL | | BTREE | | |
| t_index | 0 | uk_col1 | 1 | col1 | A | 0 | NULL | NULL | | BTREE | | |
| t_index | 1 | idx_col2 | 1 | col2 | A | 0 | NULL | NULL | | BTREE | | |
| t_index | 1 | idx_col3 | 1 | col3 | A | 0 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+------------+

2. Permissions required to create an index

If you are not using the root account, you need to consider the permission issue when creating an index. Do you only need create and alter permissions? Let’s take a closer look.

# Test user permissions mysql> show grants;
+-----------------------------------------------------------------------------------------------------+
| Grants for testuser@% |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER ON `testdb`.* TO 'testuser'@'%' |
+-----------------------------------------------------------------------------------------------------+

# Create index using alter table mysql> alter table `t_index` add index idx_col2 (col2);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

# create index mysql> create index idx_col3 on t_index(col3);
ERROR 1142 (42000): INDEX command denied to user 'testuser'@'localhost' for table 't_index'

# create index To create an index, you also need the index permission. Grant the index permission and then execute mysql> create index idx_col3 on t_index(col3);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

From the above test, we can see that the alter permission is required to create an index using the alter table method, and the index permission is required to create an index using the create index method.

In addition, you can also delete indexes using alter table `tb_name` drop index xxx and drop index xxx on tb_name, which require alter and index permissions respectively.

The obvious advantage of indexes is that they can speed up queries, but creating indexes also comes at a cost. First, each time an index is created, a B+ tree must be created for it, which will take up additional storage space; secondly, when the data in the table is added, deleted, or modified, the index also needs to be dynamically maintained, which reduces the speed of data maintenance. Therefore, we still need to consider the business when creating indexes. It is recommended not to add too many indexes in a table.

The above is the detailed information you need to know about creating MySQL indexes. For more information about creating MySQL indexes, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to quickly create a test data table with 8 million entries in MySQL
  • Understand MySQL index creation principles in one article
  • MySQL creates a scheduled task
  • The first step in getting started with MySQL database is to create a table
  • How to create a table in mysql and add field comments
  • Detailed explanation of creating, calling and managing MySQL stored procedures
  • MySQL creates many-to-many and one-to-one relationships

<<:  The most basic code for web pages

>>:  Calculation of percentage value when the css position property is absolute

Recommend

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

Introduction and tips for using the interactive visualization JS library gojs

Table of contents 1. Introduction to gojs 2. Gojs...

HTML meta explained

Introduction The meta tag is an auxiliary tag in ...

Ideas for creating wave effects with CSS

Previously, I introduced several ways to achieve ...

Randomly generate an eight-digit discount code and save it to the MySQL database

Currently, many businesses are conducting promoti...

Detailed installation history of Ubuntu 20.04 LTS

This article records the creation of a USB boot d...

Vue Router loads different components according to background data

Table of contents Requirements encountered in act...

Example code for implementing stacked carousel effect with HTML+CSS+JS

Effect: When the slideshow moves in one direction...

Detailed steps to install Mysql5.7.19 using yum on Centos7

There is no mysql by default in the yum source of...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

Detailed explanation of Linux text editor Vim

Vim is a powerful full-screen text editor and the...