当前位置:网站首页>[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
边栏推荐
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- Presentation of attribute value of an item
- MySQL advanced part 1: index
- Filter the numbers and pick out even numbers from several numbers
- Groupbykey() and reducebykey() and combinebykey() in spark
- Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
- Basic explanation of typescript
- WordPress switches the page, and the domain name changes back to the IP address
- C job interview - casting and comparing - C job interview - casting and comparing
- Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
猜你喜欢
博弈论 AcWing 894. 拆分-Nim游戏
Is it impossible for lamda to wake up?
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
QQ电脑版取消转义符输入表情
QQ computer version cancels escape character input expression
Doing SQL performance optimization is really eye-catching
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
MIT-6874-Deep Learning in the Life Sciences Week 7
实时时钟 (RTC)
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
随机推荐
QQ电脑版取消转义符输入表情
Open source storage is so popular, why do we insist on self-development?
Golang uses context gracefully
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Quickly use Amazon memorydb and build your own redis memory database
栈 AcWing 3302. 表达式求值
MySQL advanced part 2: the use of indexes
11-gorm-v2-03-basic query
可变电阻器概述——结构、工作和不同应用
4. 对象映射 - Mapping.Mapster
求组合数 AcWing 889. 满足条件的01序列
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Appium automation test foundation - Summary of appium test environment construction
How to understand the definition of sequence limit?
Liunx starts redis
Data visualization chart summary (II)
MySQL advanced part 1: stored procedures and functions
【Rust 笔记】13-迭代器(中)
[leetcode] day94 reshape matrix
Overview of variable resistors - structure, operation and different applications