当前位置:网站首页>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标签 代替。
边栏推荐
- 机器学习流程与方法
- Detailed introduction to the usage of Nacos configuration center
- require.context
- 【教程】chrome关闭跨域策略cors、samesite,跨域带上cookie
- require. context
- Leetcode 183 Customers who never order (2022.07.02)
- 4. Classes and objects
- Job object of collaboration in kotlin
- Su Shimin: 25 principles of work and life
- Exception handling in kotlin process
猜你喜欢

Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol

通达OA v12流程中心

线程安全的单例模式

y54.第三章 Kubernetes从入门到精通 -- ingress(二七)

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

RestCloud ETL 跨库数据聚合运算

Detailed introduction to the deployment and usage of the Nacos registry

Create + register sub apps_ Define routes, global routes and sub routes

awk从入门到入土(0)awk概述

MySQL learning 03
随机推荐
String replace space
CFdiv2-Fixed Point Guessing-(區間答案二分)
Packing and unpacking of JS
Use go language to realize try{}catch{}finally
【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
Stm32f407 ------- IIC communication protocol
4. 类和对象
Kotlin middle process understanding and Practice (I)
GBase 8c系统表-pg_amproc
GBase 8c系统表-pg_aggregate
Gbase 8C system table PG_ collation
stm32F407-------DMA
Cancellation of collaboration in kotlin, side effects of cancellation and overtime tasks
Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
微服务组件Sentinel (Hystrix)详细分析
UDP receive queue and multiple initialization test
Current situation and future of Web3 in various countries
Word word word
苏世民:25条工作和生活原则