当前位置:网站首页>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
边栏推荐
- 【JVM】——JVM中内存划分
- wordpress zibll子比主题6.4.1开心版 免授权
- BioVendor sRAGE Elisa试剂盒化学性质和技术研究
- How to design an awesome high concurrency architecture from scratch (recommended Collection)
- 开关电源电压型与电流型控制
- mysql 导出查询结果成 excel 文件
- Hundreds of lines of code to implement a script interpreter
- sqlmap工具使用手册
- Amino dye research: lumiprobe fam amine, 6-isomer
- msa. h: There is no such file or directory
猜你喜欢

Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid

!‘ Cat 'is not an internal or external command, nor is it a runnable program or batch file.

Share a powerful tool for factor Mining: genetic programming

Latest Windows version 5.0.14 of redis

Docker安装Mysql5.7并开启binlog

BioVendor sRAGE蛋白解决方案

IP datagram sending and forwarding process

CPG 固体支持物研究:Lumiprobe通用 CPG II 型

cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结

Voltage mode and current mode control of switching power supply
随机推荐
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
Store inventory management system source code
开关电源电压型与电流型控制
If a programmer goes to prison, will he be assigned to write code?
【JVM系列】JVM调优
The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
Biovendor sRAGE protein solution
通过例子学习Rust
mysql导出数据库字典成excel文件
QCOM LCD调试
Wireless sensor network learning notes (I)
Binary sort tree: BST
Simple usage of GSAP
JS text box loses focus to modify width text and symbols
Study on modified triphosphate: lumiprobe amino-11-ddutp
创新之源 理解通透 二
Unity delegate
Wedding studio portal applet based on wechat applet
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
How long will the PMP test results come out? You must know this!