当前位置:网站首页>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
边栏推荐
- Rxswift -- (1) create a project
- gorm事务体验
- 【Linux】——使用xshell在Linux上安装MySQL及实现Webapp的部署
- Have you finished the examination of level II cost engineer? There are also qualification regulations!
- 2022 special operation certificate examination question bank and simulation examination for safety management personnel of fireworks and firecrackers business units
- Extjs library management system source code intelligent library management system source code
- cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
- The short video local life section has become popular. How to grasp the new opportunities?
- How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
- 2022 high altitude installation, maintenance and removal examination questions and answers
猜你喜欢

【JVM】——JVM中内存划分

Latest Windows version 5.0.14 of redis

Leetcode 88: merge two ordered arrays
![[leetcode] 12. Integer to Roman numeral](/img/3e/815f24a85a3333ce924acee1856f62.png)
[leetcode] 12. Integer to Roman numeral

How to design an awesome high concurrency architecture from scratch (recommended Collection)

How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years

学习太极创客 — MQTT 第二章(五)心跳机制

The heading angle of sliceplane is the same as that of math Corresponding transformation relation of atan2 (y, x)

Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR

交流电和直流电的区别是什么?
随机推荐
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
mysql导出数据库字典成excel文件
2022西式面点师(高级)考试试题模拟考试平台操作
Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
公司为什么选择云数据库?它的魅力到底是什么!
深度强化学习笔记
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
DH parameters of robotics and derivation using MATLAB symbolic operation
【JVM系列】JVM调优
[microservices openfeign] openfeign quick start service invocation based on feign
gorm事务体验
Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
基于订单流工具,我们能看到什么?
Hundreds of lines of code to implement a script interpreter
CPG 固体支持物研究:Lumiprobe通用 CPG II 型
【Linux】——使用xshell在Linux上安装MySQL及实现Webapp的部署
Redis 的 最新windows 版本 5.0.14
wordpress zibll子比主题6.4.1开心版 免授权
基于微信小程序的婚纱影楼门户小程序
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结