当前位置:网站首页>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 !
边栏推荐
- Forest v1.5.22 发布!支持Kotlin
- Stc8a8k64d4 EEPROM read / write failure
- When a logical deletion encounters a unique index, what are the problems and solutions?
- 【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析
- How to state clearly and concisely the product requirements?
- app 测试 常用 adb 命令集合
- APP测试_测试点总结
- Google Gmail mailbox marks all unread messages as read at once
- A digit DP
- HOSATAPD_ CLI debug using WiFi whitelist
猜你喜欢
![【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析](/img/77/30215c363ae8a1324db1cbcaa5324b.png)
【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析

MySQL备份与恢复

Metal organic framework materials (fe-mil-53, mg-mof-74, ti-kumof-1, fe-mil-100, fe-mil-101) supported on isoflurane / methotrexate / doxorubicin (DOX) / paclitaxel / ibuprofen / camptothecin

How to state clearly and concisely the product requirements?

Write my Ini configuration file error

APP测试_测试点总结

A collection of common ADB commands for app testing

6 best WordPress Image optimizer plug-ins to improve WordPress website performance

Jetpack compose scaffold and topappbar (top navigation)

Use of CIN and cout
随机推荐
扁平数据转tree与tree数据扁平化
When a logical deletion encounters a unique index, what are the problems and solutions?
helm 部署traefik ingress
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
Executeineditmode property details
Unity determines whether the object is in the camera field of view
Forest v1.5.22 发布!支持Kotlin
Jetpack compose box control
弄懂了采矿业与碳中和的逻辑,就读懂了矿区无人驾驶的千亿市场
Istio安装与使用
ADVANCE. AI CEO Shoudong will share the compliance of cross-border e-commerce using AI technology at the 2022 emerging market brands online Summit
Win10 安装Office 2016出现错误代码30204-44怎么处理?
Fundamentals of deep learning [4] build easyocr and carry out simple character recognition from 0
How to fix syntax errors in WordPress websites
Metal organic framework MOF Al (Diba), MOF Zr (Diba), MOF Fe (Diba) loaded with curcumin / carboxybenzylpenicillin /mtx methotrexate / paclitaxel ptx/ DOX / cisplatin cddp/cpt camptothecin and other d
92. actual combat of completable future
Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
[MySQL 45 -10] Lesson 10 how MySQL selects indexes
Kotlin let method
Link list of high frequency written interview question brushing summary (distribution explanation & code annotation)