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

Understanding and application of JavaScript ES6 destructuring operator

Table of contents Preface The role of deconstruct...

How to clear floating example code in css

Overview The framework diagram of this article is...

Summary of pitfalls in virtualbox centos7 nat+host-only networking

Table of contents 1. Problem Background 2. What a...

Linux installation MySQL5.6.24 usage instructions

Linux installation MySQL notes 1. Before installi...

Repair solution for inconsistent MySQL GTID master and slave

Table of contents Solution 1: Rebuild Replicas Pr...

IE6 distortion problem

question: <input type="hidden" name=...

How to upgrade MySQL 5.6 to 5.7 under Windows

Written in front There are two ways to upgrade My...

Detailed explanation of the interaction between React Native and IOS

Table of contents Prerequisites RN passes value t...

Implementing a simple carousel based on JavaScript

This article shares the specific code of JavaScri...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

The most common declaration merge in TS (interface merge)

Table of contents 1. Merge interface 1.1 Non-func...

Pay attention to the use of HTML tags in web page creation

This article introduces some issues about HTML ta...

Introduction to basic concepts and technologies used in Web development

Today, this article introduces some basic concept...

Example of using #include file in html

There are two files a.htm and b.htm. In the same d...