A brief discussion on the invalidation or implicit conversion of MySQL integer and string indexes

A brief discussion on the invalidation or implicit conversion of MySQL integer and string indexes

Problem Overview

Today at work, the DBA suddenly found a piece of SQL, indicating that the SQL had implicit conversion and did not use the index. After checking, we found that it was a varchar type field. We used conditions to pass in a numeric value. Due to concerns about violating the confidentiality agreement, I will not post the picture here. Let me reproduce a similar situation for everyone to see.

Problem Reproduction

First, we create a user table test_user, in which USER_ID is set to varchar type and a unique index is added for the sake of effect.

CREATE TABLE test_user (
  ID int(11) NOT NULL AUTO_INCREMENT,
  USER_ID varchar(11) DEFAULT NULL COMMENT 'User account',
  USER_NAME varchar(255) DEFAULT NULL COMMENT 'User name',
  AGE int(5) DEFAULT NULL COMMENT 'Age',
  COMMENT varchar(255) DEFAULT NULL COMMENT 'Introduction',
  PRIMARY KEY (ID)
  UNIQUE KEY UNIQUE_USER_ID (USER_ID) USING BTREE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

The table data is as follows (the data still uses the same data as the last MySQL article MySQL uses UNION to connect two queries and sorting fails, but please note that the table structure is different.)

ID USER_ID USER_NAME AGE COMMENT
1 111 Happy Rookie 18 I am very happy today
2 222 Sad Rookie twenty one Today is sad
3 333 Serious rookie 30 Very serious today
4 444 Happy Rookie 18 I am very happy today
5 555 Serious rookie twenty one Today is serious

Next we execute the following sql

EXPLAIN SELECT * FROM test_user WHERE USER_ID = 111;

The explanations given are found to be as follows:

id select_type table partitions type possible_keys key key_len ref rows Filter by Extra
1 SIMPLE test_user ALL 5 Using where

We put the condition in quotation marks and explain it as follows:

EXPLAIN SELECT * FROM test_user WHERE USER_ID = '111';

At this time, we found that the varchar type field uses the index when it is queried as a string, but does not use the index when it is queried as a numeric type.

Problem extension

So the question is, if the field is an integer and has an index, will the index not be used when querying with a string? Practice makes perfect, let’s continue testing it.

-- Change the type of USER_ID to integer CREATE TABLE test_user (
  ID int(11) NOT NULL AUTO_INCREMENT,
  USER_ID int(11) DEFAULT NULL COMMENT 'User account',
  USER_NAME varchar(255) DEFAULT NULL COMMENT 'User name',
  AGE int(5) DEFAULT NULL COMMENT 'Age',
  COMMENT varchar(255) DEFAULT NULL COMMENT 'Introduction',
  PRIMARY KEY (ID),
  UNIQUE KEY UNIQUE_USER_ID (USER_ID) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
EXPLAIN SELECT * FROM test_user WHERE USER_ID = 111;
EXPLAIN SELECT * FROM test_user WHERE USER_ID = '111';

After executing the above two statements, we found that int type fields will be indexed regardless of whether they are queried as strings or numerical values.

in conclusion

  1. When the field we use is a numeric type, adding quotes or not adding quotes (single quotes and double quotes in SQL have the same effect) does not affect the use of the index
  2. When our field is of string type, the index cannot be used for queries without quotes, and the index can be used normally for queries with quotes

To sum up, I think it is best to add quotation marks when writing SQL in the future to avoid the situation where string types are not indexed. The deeper principles need to be explored further. If you have any opinions, please discuss them.

This is the end of this article about the invalidation or implicit conversion of MySql integer index and string index. For more information about the invalidation of MySql integer index and string index, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL's surprising implicit conversion
  • Mysql 5.6 "implicit conversion" causes index failure and inaccurate data
  • Talk about implicit conversion in MySQL
  • Resolving MySQL implicit conversion issues
  • MySQL index invalidation implicit conversion problem

<<:  How to write HTML head in mobile device web development

>>:  Independent implementation of nginx container configuration file

Recommend

An example of how Tomcat manages Session

Learned ConcurrentHashMap but don’t know how to a...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...

Specific operations of MYSQL scheduled clearing of backup data

1|0 Background Due to project requirements, each ...

JavaScript implements displaying a drop-down box when the mouse passes over it

This article shares the specific code of JavaScri...

Bootstrap 3.0 learning notes button style

This article mainly explains the style of buttons...

Solve the problem that await does not work in forEach

1. Introduction A few days ago, I encountered a p...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...

Detailed explanation of the processing of the three Docker Nginx Logs

Because colleagues in the company need Nginx log ...

MySQL joint table query basic operation left-join common pitfalls

Overview For small and medium-sized projects, joi...

Detailed explanation of MySQL multi-table join query

Table of contents Multi-table join query Inner Jo...

Summary of 3 minor errors encountered during MySQL 8.0 installation

Preface In the past, the company used the 5.7 ser...

Detailed explanation of several examples of insert and batch statements in MySQL

Table of contents Preface 1.insert ignore into 2....

MySQL 8.0.21 free installation version configuration method graphic tutorial

Six steps to install MySQL (only the installation...

Detailed example of sorting function field() in MySQL

Preface In our daily development process, sorting...

Summary of commonly used tool functions in Vue projects

Table of contents Preface 1. Custom focus command...