当前位置:网站首页>The new colleague asked me what "where 1=1" means???
The new colleague asked me what "where 1=1" means???
2022-06-11 02:49:00 【Java confidant_】
Click on the official account , Practical technical articles Know in time 
Write it at the front
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 .
overtones
Thanks for reading , If you feel like you've learned something , You can like it , Focus on . You are also welcome to comment and exchange with us if you have any questions
source :juejin.cn/post/7030076565673213989
recommend
Technical involution group , Learn together !!

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !
边栏推荐
- WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
- App test_ Summary of test points
- P4338 [ZJOI2018]历史(树剖)(暴力)
- 新来的同事问我 where 1=1 是什么意思???
- How to state clearly and concisely the product requirements?
- CPT 102_LEC 18
- How to fix syntax errors in WordPress websites
- cmake常用命令
- GCC C inline assembly
- Problèmes de classe d'outils JDBC
猜你喜欢

CPT 102_LEC 15

Will your company choose to develop data center?

One line of code solves the problem that the time to fetch datetime from MySQL database is less than eight hours

Link list of high frequency written interview question brushing summary (distribution explanation & code annotation)

js 内存泄漏

蓝桥杯_小蓝吃糖果_鸽巢原理 / 抽屉原理

If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas

Technology sharing | quick intercom, global intercom
![[C language classic]: inverted string](/img/f3/e6f37b852d22d395314628b73c9be3.jpg)
[C language classic]: inverted string

码农的进阶之路 | 每日趣闻
随机推荐
jdbc工具类的问题
Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid / emodin / quercetin / sucralose / diflunisal / omeprazole (OME)
【冒泡排序的实现】
银行选择电子招标采购的必要性
Stc8a8k64d4 EEPROM read / write failure
P4338 [zjoi2018] history (tree section) (violence)
Istio安装与使用
Explanation of spark common parameters
Common vocabulary of software testing English
MySQL backup and recovery
Modify release opening animation
怎样简洁明了地说清楚产品需求?
OpenJudge NOI 1.13 18:Tomorrow never knows?
基于互联网架构演进, 构建秒杀系统
Byte beating | the first batch of written examination for game R & D post (question solution)
mysql重装时写my.ini配置文件出错
CPT 102_LEC 13-14
CPT 102_LEC 18
Flat data to tree and tree data flattening
20220610 星期五