When an employer asks you whether an index will be used in a MySQL query condition, how should you answer?
Let’s test it out 1. Create a table and index the field port CREATE TABLE `pre_request_logs_20180524` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip` char(16) NOT NULL COMMENT 'Proxy IP', `port` int(8) NOT NULL COMMENT 'Port number', `status` enum('success','failure') NOT NULL COMMENT 'status', `create_time` datetime NOT NULL COMMENT 'Creation time', `update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `idx_port` (`port`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COMMENT='Proxy IP request log'; Insert test data INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (1, '192.168.1.199', 53149, 'Failed', '2018-05-24 14:55:34', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (2, '192.168.1.100', 10653, 'Success', '2018-05-24 14:55:54', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (3, '192.168.1.112', 50359, 'Failed', '2018-05-24 14:56:00', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (4, '192.168.1.67', 30426, 'Failed', '2018-05-24 14:56:09', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (5, '192.168.1.209', 49323, 'Failed', '2018-05-24 14:56:12', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (6, '192.168.1.209', 51161, 'Success', '2018-05-24 14:56:13', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (7, '192.168.1.12', 54167, 'Success', '2018-05-24 14:56:16', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (8, '192.168.1.64', 20462, 'Success', '2018-05-24 14:56:19', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (9, '192.168.1.53', 22823, 'Failed', '2018-05-24 14:56:31', '2018-11-16 10:58:13'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (10, '192.168.1.85', 48229, 'Success', '2018-05-24 14:56:32', '2018-11-16 11:01:11'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (11, '192.168.1.85', 48229, 'Success', '2018-05-24 14:56:32', '2018-11-16 11:01:15'); INSERT INTO ``(`id`, `ip`, `port`, `status`, `create_time`, `update_time`) VALUES (12, '192.168.1.85', 48229, 'Success', '2018-05-24 14:56:32', '2018-11-16 13:34:37'); 2. Test SQL explain select * from pre_request_logs_20180524 where port in (51161,20462,48229); Execution Results From the results, it seems that the index is not used, but don't jump to conclusions. Let's look at the two SQL statements. select * from pre_request_logs_20180524 where port in (51161,48229); select * from pre_request_logs_20180524 where port in (51161,20462); The execution results are as follows You can see that the second SQL statement uses an index. The difference between the two SQL statements is that the port value is different. One contains 48229 and the other contains 20462. In fact, the MySQL optimizer will automatically determine whether in uses the secondary index, that is, the index of the port field. 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:
|
Docker is becoming more and more mature and its f...
Basic Environment Pagoda installation service [Py...
Make a note so you can come back and check it lat...
1. Command Introduction The tac (reverse order of...
Table of contents Horizontal bar chart Dynamicall...
Operating system: Win7 64-bit Ultimate Edition My...
When coding, you will find that many things have ...
Introduction to Debian Debian in a broad sense re...
Introduction Memcached is a distributed caching s...
As a super rookie, I just started learning MySQL ...
Preface Vue provides a wealth of built-in directi...
1. Goal: Change the value of character_set_server...
MySQL sets up independent writing separation. If ...
React is an open-source JavaScript library used b...
This article uses examples to describe how to cre...