当前位置:网站首页>where 1=1 是什么意思
where 1=1 是什么意思
2022-07-03 02:22:00 【梦远星帆】
where 1=1
起拼接作用,主要是为了预防参数为空sql出错
但是,这种写法在druid数据源中可能被识别为sql非法注入,对于mysql引擎来说。是会自动转换的,影响不大
而且,如果不是select,而是delete,那么就危险了,呕吼 全部删除,这就是灾难了
先来看一段代码
<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>
上面的代码很熟悉,就是查询符合条件的总条数。在mybatis中常用到if标签判断where子句后的条件,为防止首字段为空导致sql报错。没错 ,当遇到多个查询条件,使用where 1=1 可以很方便的解决我们条件为空的问题,那么这么写 有什么问题吗 ?
网上有很多人说,这样会引发性能问题,可能会让索引失效,那么我们今天来实测一下,会不会不走索引?
实 测
title字段 已经加上索引,我们通过EXPLAIN看下
EXPLAIN SELECT * FROM t_book WHERE title = '且在人间';
EXPLAIN SELECT * FROM t_book WHERE 1=1 AND title = '且在人间';
对比上面两种我们会发现 可以看到 possible_keys(可能使用的索引) 和 key(实际使用的索引)都使用到了索引进行检索。
结 论
where 1=1 也会走索引,不影响查询效率,我们写的sql指令会被mysql 进行解析优化成自己的处理指令,在这个过程中 1 = 1 这类无意义的条件将会被优化。
使用explain EXTENDED sql 进行校对,发现确实where1=1这类条件会被mysql的优化器所优化掉。
那么我们在mybatis当中可以改变一下写法,因为毕竟mysql优化器也是需要时间的,虽然是走了索引,但是当数据量很大时,还是会有影响的,所以我们建议代码这样写:
<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>
我们用 where标签 代替。
边栏推荐
- GBase 8c 触发器(一)
- How to deal with cache hot key in redis
- Recommendation letter of "listing situation" -- courage is the most valuable
- [Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
- Monitoring and management of JVM
- Distributed transaction solution
- Iptables layer 4 forwarding
- GBase 8c系统表-pg_amproc
- require. context
- stm32F407-------DMA
猜你喜欢
awk从入门到入土(0)awk概述
Memory pool (understand the process of new developing space from the perspective of kernel)
Awk from introduction to earth (0) overview of awk
Tongda OA V12 process center
线程安全的单例模式
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Tongda OA homepage portal workbench
Restcloud ETL cross database data aggregation operation
詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
What are MySQL locks and classifications
随机推荐
Socket programming
The Sandbox阐释对元宇宙平台的愿景
面试八股文整理版
Awk from getting started to being buried (2) understand the built-in variables and the use of variables in awk
How to deal with cache hot key in redis
Restcloud ETL cross database data aggregation operation
SPI机制
GBase 8c系统表-pg_amproc
Create + register sub apps_ Define routes, global routes and sub routes
Explore the conversion between PX pixels and Pt pounds, mm and MM
What are MySQL locks and classifications
awk从入门到入土(0)awk概述
力扣(LeetCode)183. 从不订购的客户(2022.07.02)
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
苏世民:25条工作和生活原则
elastic stack
Socket编程
Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
使用Go语言实现try{}catch{}finally
GBase 8c系统表pg_cast