当前位置:网站首页>[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
边栏推荐
- Golang uses context gracefully
- 【Rust 笔记】15-字符串与文本(上)
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- C job interview - casting and comparing - C job interview - casting and comparing
- Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
- MySQL advanced part 2: the use of indexes
- 【LeetCode】Day94-重塑矩阵
- Data visualization chart summary (II)
- What's wrong with this paragraph that doesn't work? (unresolved)
- Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
猜你喜欢
SPI details
Quickly use Amazon memorydb and build your own redis memory database
RGB LED infinite mirror controlled by Arduino
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
MySQL advanced part 2: storage engine
Redis publish subscribe command line implementation
什么是套接字?Socket基本介绍
1.14 - assembly line
博弈论 AcWing 894. 拆分-Nim游戏
Data visualization chart summary (II)
随机推荐
[rust notes] 17 concurrent (Part 1)
RGB LED infinite mirror controlled by Arduino
Leetcode-3: Longest substring without repeated characters
【Rust 笔记】13-迭代器(下)
背包问题 AcWing 9. 分组背包问题
MySQL advanced part 1: stored procedures and functions
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
Appium automation test foundation - Summary of appium test environment construction
[rust notes] 14 set (Part 1)
【Rust 笔记】13-迭代器(中)
Traversal of leetcode tree
927. Trisection simulation
求组合数 AcWing 888. 求组合数 IV
JS quickly converts JSON data into URL parameters
Is it impossible for lamda to wake up?
C job interview - casting and comparing - C job interview - casting and comparing
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
Liunx starts redis