当前位置:网站首页>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系统表-pg_am
- Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
- Awk from getting started to getting into the ground (3) the built-in functions printf and print of awk realize formatted printing
- Swift development learning
- Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt
- Awk from getting started to being buried (2) understand the built-in variables and the use of variables in awk
- Apple releases MacOS 11.6.4 update: mainly security fixes
- 基于线程池的生产者消费者模型(含阻塞队列)
- Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
- 【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
猜你喜欢

内存池(内核角度理解new开辟空间的过程)

微服务组件Sentinel (Hystrix)详细分析

《上市风云》荐书——唯勇气最可贵

UDP receive queue and multiple initialization test

通达OA 首页门户工作台

elastic stack

线程安全的单例模式

Coroutinecontext in kotlin

Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???

通达OA v12流程中心
随机推荐
Gbase 8C system table PG_ aggregate
Iptables layer 4 forwarding
Socket编程
Gbase 8C system table PG_ database
MySQL learning 03
Awk from introduction to earth (0) overview of awk
Stm32f407 ------- IIC communication protocol
Trial setup and use of idea GoLand development tool
4. 类和对象
GBase 8c触发器(三)
QT qcombobox add qccheckbox (drop-down list box insert check box, including source code + comments)
[Yu Yue education] reference materials of love psychology of China University of mining and technology
How to deal with cache hot key in redis
easyExcel
《上市风云》荐书——唯勇气最可贵
require. context
Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
Su Shimin: 25 principles of work and life
Javescript 0.1 + 0.2 = = 0.3 problem
String replace space