当前位置:网站首页>[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
边栏推荐
- Records of some tools 2022
- SPI 详解
- Groupbykey() and reducebykey() and combinebykey() in spark
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- Leetcode-3: Longest substring without repeated characters
- Chart. JS - Format Y axis - chart js - Formatting Y axis
- [2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
- 背包问题 AcWing 9. 分组背包问题
- 11-gorm-v2-02-create data
- Leetcode backtracking method
猜你喜欢
随机推荐
Introduction to LVS [unfinished (semi-finished products)]
Single chip computer engineering experience - layered idea
1.13 - RISC/CISC
JS quickly converts JSON data into URL parameters
MIT-6874-Deep Learning in the Life Sciences Week 7
Leetcode stack related
C job interview - casting and comparing - C job interview - casting and comparing
Sqlmap tutorial (1)
Leetcode-1200: minimum absolute difference
C - XOR to all (binary topic)
Leetcode dynamic programming
The difference between CPU core and logical processor
11-gorm-v2-02-create data
[leetcode] day95 effective Sudoku & matrix zeroing
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Appium自动化测试基础 — Appium测试环境搭建总结
Open source storage is so popular, why do we insist on self-development?
Sword finger offer II 058: schedule
Golang uses context gracefully
【LeetCode】Easy | 20. Valid parentheses