当前位置:网站首页>[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
边栏推荐
- SQLMAP使用教程(一)
- 1040 Longest Symmetric String
- Simple selection sort of selection sort
- MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
- 容斥原理 AcWing 890. 能被整除的数
- SQL三种连接:内连接、外连接、交叉连接
- 1.15 - input and output system
- How to generate an image from text on fly at runtime
- Traversal of leetcode tree
- LeetCode 0107. Sequence traversal of binary tree II - another method
猜你喜欢
随机推荐
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
Appium基础 — 使用Appium的第一个Demo
LeetCode-54
TypeScript 基础讲解
2021apmcm post game Summary - edge detection
redis发布订阅命令行实现
中国剩余定理 AcWing 204. 表达整数的奇怪方式
Leetcode heap correlation
MySQL advanced part 2: SQL optimization
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
[leetcode] day94 reshape matrix
传统数据库逐渐“难适应”,云原生数据库脱颖而出
【LeetCode】Easy | 20. Valid parentheses
求组合数 AcWing 889. 满足条件的01序列
Shutter web hardware keyboard monitoring
MySQL advanced part 1: View
New title of module a of "PanYun Cup" secondary vocational network security skills competition
Groupbykey() and reducebykey() and combinebykey() in spark
A reason that is easy to be ignored when the printer is offline
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!