当前位置:网站首页>What does mysql---where 1=1 mean
What does mysql---where 1=1 mean
2022-06-28 05:19:00 【Zhangshao】
When the new colleague came, he asked me where 1=1 What's interesting , It doesn't make sense , I smiled. . Today, let's explain .
where 1=1
Let's start with a piece of code
<select id="queryBookInfo" parameterType="com.ths.platform.entity.BookInfo" resultType="java.lang.Integer">
select count(id) from t_book t where 1=1
<if test="title !=null and title !='' ">
AND title = #{title}
</if>
<if test="author !=null and author !='' ">
AND author = #{author}
</if>
</select>
The above code is familiar , This is the total number of qualified queries . stay mybatis Used in if Tag judgment where Condition after clause , In order to prevent the first field from being empty, resulting in sql Report errors . you 're right , When multiple query conditions are encountered , Use where 1=1 It can easily solve the problem that our condition is empty , So that's how it's written Is there anything wrong with that? ?
Many people on the Internet say , This can cause performance problems , May invalidate the index , So let's measure it today , Will you not go
Actually measured
title The field has been indexed , We go through EXPLAIN look down
EXPLAIN SELECT * FROM t_book WHERE title = ' And on earth ';

EXPLAIN SELECT * FROM t_book WHERE 1=1 AND title = ' And on earth ';

Comparing the above two, we will find You can see possible_keys( Possible indexes ) and key( Actual index used ) All use the index for Retrieval .
Conclusion
where 1=1 I'll go with the index , Does not affect query efficiency , We wrote sql Orders will be mysql Analyze and optimize into your own processing instructions , In the process 1 = 1 Such meaningless conditions will be optimized . Use explain EXTENDED sql Proofread , Find out where1=1 Such conditions will be mysql Optimized by the optimizer .
So we're in mybatis You can change the writing , Because after all mysql The optimizer also takes time , Although it's gone , But when the amount of data is large , It will still have an impact , So we suggest that the code be written like this :
<select id="queryBookInfo" parameterType="com.ths.platform.entity.BookInfo" resultType="java.lang.Integer">
select count(*) from t_book t
<where>
<if test="title !=null and title !='' ">
title = #{title}
</if>
<if test="author !=null and author !='' ">
AND author = #{author}
</if>
</where>
</select>
We use it where Label substitution .
author : Su Shi
link :juejin.cn/post/7030076565673213989
边栏推荐
- Share a powerful tool for factor Mining: genetic programming
- Realizing color detection with OpenCV
- Keil C51的Data Overlaying机制导致的函数重入问题
- 店铺进销存管理系统源码
- Docker安装Mysql5.7并开启binlog
- 深度强化学习笔记
- What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
- If a programmer goes to prison, will he be assigned to write code?
- Deeplearning ai-week1-quiz
- 乔布斯在斯坦福大学的演讲稿——Follow your heart
猜你喜欢

Dart学习——函数、类

Don't roll! How to reproduce a paper with high quality?

mysql导出数据库字典成excel文件

JS text box loses focus to modify width text and symbols

sqlmap工具使用手册

开关电源电压型与电流型控制

短视频本地生活版块成为热门,如何把握新的风口机遇?

The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022

改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP

2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码
随机推荐
氨基染料研究:Lumiprobe FAM 胺,6-异构体
无线传感器网络学习笔记(一)
禁用右击、键盘打开控制台事件
Organize the online cake mall project
公司为什么选择云数据库?它的魅力到底是什么!
MySQL export database dictionary to excel file
Share a powerful tool for factor Mining: genetic programming
Rxswift -- (1) create a project
MySQL 45讲 | 05 深入浅出索引(下)
Question bank and answers of 2022 materialman general basic (materialman) operation certificate examination
如何从零设计一款牛逼的高并发架构(建议收藏)
如何学习可编程逻辑控制器(PLC)?
If a programmer goes to prison, will he be assigned to write code?
Concurrent wait/notify description
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
通过例子学习Rust
2022 Western pastry (Advanced) test question simulation test platform operation
Voltage mode and current mode control of switching power supply
Prove that there are infinite primes / primes
Interview: what are the similarities and differences between abstract classes and interfaces?