Overview Indexing is a skill that must be mastered in MySQL, and it is also a means to improve MySQL query efficiency. Can you understand it through the following experiment? MySQL index rules can also continuously optimize SQL statements Purpose This experiment is to verify the leftmost principle of the combined index illustrate This experiment is only to verify the results of actual use of the index, please ignore the rationality of the design Preparation
Create table statement DROP TABLE IF EXISTS `qz_users`; CREATE TABLE `qz_users` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'User's UID', `user_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'User name', `real_name` varchar(128) CHARACTER SET utf8 DEFAULT NULL COMMENT 'User name', `email` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT 'EMAIL', `mobile` varchar(16) CHARACTER SET utf8 DEFAULT NULL COMMENT 'User's mobile phone', `password` varchar(32) CHARACTER SET utf8 DEFAULT NULL COMMENT 'User password', `salt` varchar(16) CHARACTER SET utf8 DEFAULT NULL COMMENT 'User-added obfuscation code', `avatar_file` varchar(128) CHARACTER SET utf8 DEFAULT NULL COMMENT 'Avatar file', `sex` tinyint(1) DEFAULT NULL COMMENT 'Gender', `birthday` int(10) DEFAULT NULL COMMENT 'Birthday', PRIMARY KEY (`uid`), KEY `user_name` (`user_name`(250)), KEY `complex_index` (`email`,`mobile`,`sex`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; Prepared queries explain select * from qz_users where user_name = "ryanhe"; explain select * from qz_users where email = "x"; explain select * from qz_users where email = "x" and mobile = "x" and sex=1; explain select * from qz_users where email = "x" and mobile = "x"; explain select * from qz_users where email = "x" and sex = "x"; explain select * from qz_users where sex = "x" and mobile = "x"; explain select * from qz_users where mobile = "x" and sex = "0"; Results Analysis Using the user_name condition explain select * from qz_users where user_name= "x"; result analyze
Use email conditions explain select * from qz_users where email = "x"; result analyze
Use email + mobile + sex conditions explain select * from qz_users where email = "x" and mobile = "x" and sex=1; result analyze
Use email + mobile conditions explain select * from qz_users where email = "x" and mobile = "x"; result analyze
Use email + sex condition explain select * from qz_users where email = "x" and sex = "x"; result analyze
Use sex + mobile conditions explain select * from qz_users where sex = "x" and mobile = "x"; result analyze
Use mobile+ sex conditions explain select * from qz_users where mobile = "18602199680" and sex = "0"; result analyze
in conclusion From the above results, we can see that after setting the combined index, the reasonable use of the query condition order can avoid slow query of SQL statements. You may also be interested in:
|
<<: JavaScript implements long image scrolling effect
>>: Detailed tutorial on installing Docker on CentOS 7.5
tomcat server configuration When everyone is lear...
I was working on a pop-up ad recently. Since the d...
Execute the create table statement in the databas...
One sentence to introduce HOC What is a higher-or...
In this blog, we will discuss ten performance set...
1. Single row overflow 1. If a single line overfl...
To deploy multiple sites on a server, you need to...
1. Pull the Mysql image docker pull mysql:5.7 2. ...
Copy code The code is as follows: <span style=...
<br />Original text: http://jorux.com/archiv...
The project test environment database data is los...
MySQL itself does not support recursive syntax, b...
Table of contents MySQL multi-version concurrency...
In the past two days, I have been very troubled t...
This technique comes from this article - How to a...