当前位置:网站首页>[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
边栏推荐
- 区间问题 AcWing 906. 区间分组
- MySQL advanced part 2: SQL optimization
- 927. Trisection simulation
- Leetcode-6111: spiral matrix IV
- [rust notes] 14 set (Part 1)
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- MySQL advanced part 1: stored procedures and functions
- SPI 详解
- 【LeetCode】Easy | 20. Valid parentheses
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
猜你喜欢

Overview of variable resistors - structure, operation and different applications

liunx启动redis

Sqlmap tutorial (II) practical skills I

QQ computer version cancels escape character input expression

MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!

博弈论 AcWing 891. Nim游戏

容斥原理 AcWing 890. 能被整除的数

What is socket? Basic introduction to socket

阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长

LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
随机推荐
Open source storage is so popular, why do we insist on self-development?
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
LeetCode 1200. Minimum absolute difference
4. 对象映射 - Mapping.Mapster
[rust notes] 14 set (Part 2)
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
New title of module a of "PanYun Cup" secondary vocational network security skills competition
Leetcode-9: palindromes
Basic explanation of typescript
Leetcode dynamic programming
Introduction to LVS [unfinished (semi-finished products)]
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
liunx启动redis
区间问题 AcWing 906. 区间分组
The difference between CPU core and logical processor
Leetcode heap correlation
[rust notes] 13 iterator (Part 2)
求组合数 AcWing 888. 求组合数 IV
Leetcode-6111: spiral matrix IV