当前位置:网站首页>[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
2022-07-05 06:20:00 【Daidou family】
Catalog
Preface
- We all know mysql When where When there is a function query in the condition , Can cause indexes to fail
- for example :
select * from test where DATE(swap_time) = DATE( date_sub( CURRENT_DATE, INTERVAL 1 DAY ) );
- use Explain Commands can be viewed sql Implementation plan of , Whether the index is used , Can pass possible_keys and rows It's easy to see
- In this case mysql 5.7 There are different solution suggestions before and after the version
- First, query the current mysql The version used
select VERSION();
mysql 5.7 Previous version optimization suggestions
select * from test where swap_time >= CURRENT_DATE and swap_time < date_add(CURRENT_DATE,INTERVAL 1 day);
mysql 5.7 Optimization suggestions for future versions
- stay 5.7 Later versions of the database support the establishment of functional indexes
- Create virtual columns
alter table test add column swap_time_index datetime GENERATED ALWAYS AS (DATE(swap_time));
- Re execution sql
select * from test where DATE(swap_time) = DATE( date_sub( CURRENT_DATE, INTERVAL 1 DAY ) );
- I found that the query speed has become faster
- utilize Explain View execution plan
Explain select * from test where DATE(swap_time) = DATE( date_sub( CURRENT_DATE, INTERVAL 1 DAY ) );
- It is found that the functional index is used
Reference address :
mysql Build functional index
MySQL Explain Detailed explanation
MySQL Functional index and optimization
mysql How to view sql Statement execution time and efficiency
边栏推荐
- Usage scenarios of golang context
- Doing SQL performance optimization is really eye-catching
- LeetCode 1200. Minimum absolute difference
- [rust notes] 13 iterator (Part 2)
- Leetcode dynamic programming
- Is it impossible for lamda to wake up?
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- LVS简介【暂未完成(半成品)】
- Winter messenger 2
- Sqlmap tutorial (II) practical skills I
猜你喜欢

实时时钟 (RTC)

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库

MySQL advanced part 2: storage engine

Leetcode-6110: number of incremental paths in the grid graph

Groupbykey() and reducebykey() and combinebykey() in spark

MySQL advanced part 1: index

SQLMAP使用教程(一)

SPI 详解

【LeetCode】Easy | 20. Valid parentheses

Leetcode array operation
随机推荐
1040 Longest Symmetric String
SPI 详解
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
[rust notes] 14 set (Part 1)
[rust notes] 16 input and output (Part 1)
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
Appium基础 — 使用Appium的第一个Demo
WordPress switches the page, and the domain name changes back to the IP address
Leetcode backtracking method
redis发布订阅命令行实现
求组合数 AcWing 889. 满足条件的01序列
博弈论 AcWing 893. 集合-Nim游戏
Open source storage is so popular, why do we insist on self-development?
Leetcode heap correlation
Winter vacation water test 1 Summary
[rust notes] 13 iterator (Part 2)
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
高斯消元 AcWing 884. 高斯消元解异或线性方程组