Learn more about MySQL indexes

Learn more about MySQL indexes

1. Indexing principle

Indexes are used to quickly find rows with a specific value in a column. Without an index, MySQL would have to start with the first record and then read the entire table until it found the relevant rows. The larger the table, the more time it takes. For an ordered field, a binary search can be applied, which is why the performance can be substantially improved. Both MYISAM and INNODB use B+Tree as the index structure

(Primary key and unique key will add index by default)

2. Create an index

If we do not use the index, we will perform a full table scan when we query for salaries greater than 1500.

When do you need to add an index to a field?

-The amount of data in this field in the table is huge

-Fields that are frequently searched and often appear in where clauses

- It is not recommended to add indexes to fields that are frequently operated by DML

An index is like a table of contents in a book.

The primary key will automatically add an index, so it is more efficient to query based on the primary key.

If you frequently query based on sal and encounter performance bottlenecks, first check whether there is an algorithm problem in the program, and then consider indexing sal. The index is created as follows:

1. create unique index index name on table name (column name);

create unique index u_ename on emp(ename);

2. alter table table name add unique index index name (column name);

2. alter table table name add unique index index name (column name); 

3. View the index

show index from emp; 

4. Use indexes

Note that you must not use select * ... You can see that type!=all, indicating that the index is used

explain select sal from emp where sal > 1500;

The sal in the condition uses an index

As shown above: If we want to find all rows where sal is greater than 1500, we can scan the index. The index is sorted and the result is 7 rows. We know that there will be no more matching records and we can exit.

If you are looking for a value that does not appear before some intermediate point in the index table, there are also location algorithms that find its first matching index entry without doing a sequential scan of the table (such as binary search).

In this way, the first matching value can be quickly located to save a lot of search time. Databases use a variety of techniques to quickly locate index values, which are usually the work of the DBA.

5. Delete index

DROP INDEX index_name ON talbe_name

ALTER TABLE table_name DROP INDEX index_name

ALTER TABLE table_name DROP PRIMARY KEY

The first two statements are equivalent and delete the index index_name in table_name.

The third statement is only used when deleting the PRIMARY KEY index, because a table can only have one PRIMARY KEY index.

mysql> ALTER TABLE EMP DROP INDEX test_index;

After the deletion, the index is no longer used and the query will perform a full table scan.

The above is the detailed content of in-depth understanding of MySQL index. For more information about MySQL index, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL Create Index method, syntax structure and examples
  • Detailed explanation of MySQL combined index method
  • MySQL index knowledge summary
  • Summary of MySql storage engine and index related knowledge
  • Mysql index types and basic usage examples
  • Detailed explanation of the underlying implementation of descending index, a new feature of MySQL 8
  • How to use MySQL covering index and table return
  • Common scenarios and avoidance methods for index failure in MySQL
  • Mysql database advanced usage of views, transactions, indexes, self-connections, user management example analysis
  • Basic syntax of MySQL index

<<:  Win10+Ubuntu 20.04 LTS dual system installation (UEFI + GPT) (pictures and text, multiple pictures warning)

>>:  Vue3 Vue Event Handling Guide

Recommend

Vue Basics Introduction: Vuex Installation and Use

Table of contents 1. What is vuex 2. Installation...

Summary of data interaction between Docker container and host

Preface When using Docker in a production environ...

MySQL 5.7 installation-free configuration graphic tutorial

Mysql is a popular and easy-to-use database softw...

Analysis of the principles and usage of Linux hard links and soft links

In the Linux system, there is a kind of file call...

Method of using MySQL system database for performance load diagnosis

A master once said that you should know the datab...

The difference between JS pre-parsing and variable promotion in web interview

Table of contents What is pre-analysis? The diffe...

Pure CSS header fixed implementation code

There are two main reasons why it is difficult to...

Detailed steps for deploying Microsoft Sql Server with Docker

Table of contents 1 Background 2 Create a contain...

Detailed steps to build an NFS file sharing server in Linux

Linux builds NFS server In order to achieve data ...