A few days ago, a colleague asked me a question about MySQL index. Although I know it roughly, I still want to practice it. Is it possible to use indexes for queries such as is null and is not null? Maybe some articles on the Internet said that indexes cannot be used, but in fact, it is not. Let's take a look at a small experiment CREATE TABLE `null_index_t` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `null_key` varchar(255) DEFAULT NULL, `null_key1` varchar(255) DEFAULT NULL, `null_key2` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_1` (`null_key`) USING BTREE, KEY `idx_2` (`null_key1`) USING BTREE, KEY `idx_3` (`null_key2`) USING BTREE )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Use a stored procedure to insert data delimiter $ #Use delimiter to mark the end of the stored procedure. $ indicates the end of the stored procedure. create procedure nullIndex1() begin declare i int; declare j int; set i=1; set j=1; while(i<=100) do while(j<=100) do IF (i % 3 = 0) THEN INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (null , LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8)); ELSEIF (i % 3 = 1) THEN INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), NULL, LEFT(MD5(RAND()), 8)); ELSE INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8), NULL); END IF; set j=j+1; end while; set i=i+1; set j=1; end while; end $ call nullIndex1(); Then look at our is null query EXPLAIN select * from null_index_t WHERE null_key is null; Let's look at another EXPLAIN select * from null_index_t WHERE null_key is not null; What can we see from here? Think about it. From the above, we can see that is null should be indexed, so at least it is not a blanket rule. However, is not null does not seem to work. Let's make a small change and change the data in this table to 9100 null and the remaining 900 have values, and then execute the following command: Then let's look at the execution results EXPLAIN select * from null_index_t WHERE null_key is null; EXPLAIN select * from null_index_t WHERE null_key is not null; Is it different? Here I would like to add that the MySQL I used in the experiment is 5.7, and the consistency of other versions is not guaranteed. The above is the detailed content of sharing some details about MySQL index. For more information about MySQL index, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Q&A: Differences between XML and HTML
>>: Detailed explanation of vue3 cache page keep-alive and unified routing processing
Preface This article summarizes some implementati...
Table of contents 1. exists 1.1 Description 1.2 E...
Table of contents Preface Prototypal inheritance ...
Table of contents 1. Install axios 2. Use of axio...
You may often see the following effect: That’s ri...
This article example shares the specific code of ...
Select the category selection. After testing, IE ...
origin Recently, I am working on requirement A, i...
1. Built-in functions 1. Mathematical functions r...
There was a problem when installing the compresse...
Parent File import React, { useState } from '...
Table of contents Preface Why How much is it? Num...
<body> <div id="root"> <...
Table of contents 1. Introduction 2. Basic Concep...
Turn off ping scanning, although it doesn't h...