Summary of situations where MySQL indexes will not be used

Summary of situations where MySQL indexes will not be used

Types of Indexes in MySQL

Generally, they can be divided into four categories:

  • Normal index: the most common index
  • Unique index: The value of the index column must be unique, but null values ​​are allowed.
  • Primary key index: a special unique index that does not allow null values
  • Joint index: The index column has multiple fields, and the leftmost prefix principle must be met when using it

Normal index

This is the most basic index and it has no restrictions. It can be created in the following ways:

1. Create an index

The code is as follows:

CREATE INDEX indexName ON mytable(username(length));

If it is CHAR or VARCHAR type, length can be smaller than the actual length of the field; if it is BLOB or TEXT type, length must be specified, and the same applies below.

2. Modify the table structure

The code is as follows:

ALTER mytable ADD INDEX [indexName] ON (username(length))

Specify directly when creating a table

CREATE TABLE mytable(
 ID INT NOT NULL,
 username VARCHAR(16) NOT NULL,
 INDEX [indexName] (username(length))
);

The syntax for deleting an index is:

DROP INDEX [indexName] ON mytable;

Unique Index

It is similar to the previous ordinary index, except that the values ​​of the index column must be unique, but null values ​​are allowed. If it is a composite index, the combination of column values ​​must be unique. It can be created in the following ways:

CREATE UNIQUE INDEX indexName ON mytable(username(length))

Modify the table structure:

ALTER mytable ADD UNIQUE [indexName] ON (username(length))

When creating a table, specify directly:

CREATE TABLE mytable(  
	ID INT NOT NULL,  
	username VARCHAR(16) NOT NULL,  
	UNIQUE [indexName] (username(length))  
);

Primary key index

It is a special unique index that does not allow null values. Generally, the primary key index is created when the table is created:

The code is as follows:

CREATE TABLE mytable(  
	ID INT NOT NULL,  
	username VARCHAR(16) NOT NULL,  
	PRIMARY KEY(ID) 
);

Of course, you can also use the ALTER command. Remember: a table can only have one primary key.

Joint Index

To visually compare single-column indexes and composite indexes, add multiple fields to the table:

CREATE TABLE mytable(  
	ID INT NOT NULL,  
	username VARCHAR(16) NOT NULL,  
	city ​​VARCHAR(50) NOT NULL,  
	age INT NOT NULL 
);

In order to further squeeze the efficiency of MySQL, we should consider creating a composite index. That is, build name, city, and age into one index:

The code is as follows:

ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age);

Situations where indexes are not used

Indexes may not be effective every time. If we do not operate correctly, it is very likely that a full table scan will be performed instead of an index. The possible_key, key_len, and key parameters in Explain can analyze whether our SQL statements use indexes.

The following situations will cause the index to fail

  1. != is used in the query column, for example, select id,name,age from student where id != 2;
  2. If a function operation is used in the query column, such as pow(id,2) to square the id, the index will not be used.
  3. If there is an or in the condition, it will not be used even if some of the conditions have indexes (this is why you should try to use or as little as possible)
  4. Even if the leftmost prefix principle is met in the joint index, the index will not be used if the first condition includes a range query.
  5. If the data type of the index column is implicitly converted, the index will not be used. For example, if the column type is a string, the data must be quoted in the condition, otherwise the index will not be used.
  6. If MySQL estimates that a full table scan will be faster than using an index, it does not use the index.

You can use Explain to test whether the index is used.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • MySQL index usage monitoring skills (worth collecting!)
  • Detailed explanation of MySQL row locks when encountering composite primary keys and multi-column indexes
  • Will the index be used in the MySQL query condition?
  • Example analysis of the impact of MySQL index on sorting
  • How to modify the IP restriction conditions of MySQL account
  • Detailed explanation of the limitations and restrictions of MySQL partitioned tables
  • MySQL query statement uses limit to limit the number of rows queried
  • Solution to the problem of MySQL connection number exceeding the limit
  • Analysis of the Principle of MySQL Index Length Limit

<<:  WeChat applet implements jigsaw puzzle game

>>:  How to install Docker CE on Ubuntu 18.04 (Community Edition)

Recommend

How to use Portainer to build a visual interface for Docker

Portainer Introduction Portainer is a graphical m...

Let's talk about the problem of Vue integrating sweetalert2 prompt component

Table of contents 1. Project Integration 1. CDN i...

MAC+PyCharm+Flask+Vue.js build system

Table of contents Configure node.js+nvm+npm npm s...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Implementation of remote Linux development using vscode

Say goodbye to the past Before vscode had remote ...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...

Detailed explanation of Linx awk introductory tutorial

Awk is an application for processing text files, ...

6 interesting tips for setting CSS background images

Background-image is probably one of those CSS pro...

Do you know all 24 methods of JavaScript loop traversal?

Table of contents Preface 1. Array traversal meth...

Detailed explanation of MySQL DEFINER usage

Table of contents Preface: 1.Brief introduction t...

MySQL implements a solution similar to Oracle sequence

MySQL implements Oracle-like sequences Oracle gen...

How to use Vuex's auxiliary functions

Table of contents mapState mapGetters mapMutation...

Element uses scripts to automatically build new components

Table of contents background How does element-ui&...