当前位置:网站首页>新来的同事问我 where 1=1 是什么意思???
新来的同事问我 where 1=1 是什么意思???
2022-06-11 02:07:00 【Java知音_】
点击关注公众号,实用技术文章及时了解
写在前面
新的同事来之后问我where 1=1 是什么有意思,这样没意义啊,我笑了。今天来说明下。
where 1=1
先来看一段代码
<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标签代替。
弦外之音
感谢你的阅读,如果你感觉学到了东西,您可以点赞,关注。也欢迎有问题我们下面评论交流
来源:juejin.cn/post/7030076565673213989
推荐

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!
边栏推荐
- A collection of common ADB commands for app testing
- Common vocabulary of software testing English
- App test_ Summary of test points
- Prophet
- Tests logiciels vocabulaire commun anglais
- 10007. ISBN number
- Modify release opening animation
- [MySQL 45 lecture -12] lecture 12 the reason why MySQL has a wind attack from time to time
- 那些笑着离开“北上广”的人,为何最后都哭了?
- Jetpack compose scaffold and bottomappbar (bottom navigation)
猜你喜欢

mysql重装时写my.ini配置文件出错

银行选择电子招标采购的必要性

Epoll principle and Application & ET mode and lt mode

What can the enterprise exhibition hall design bring to the enterprise?

Jetpack Compose Scaffold和BottomAppBar(底部导航)

CPT 102_LEC 16

The most complete format description of clang format

弄懂了采矿业与碳中和的逻辑,就读懂了矿区无人驾驶的千亿市场

Istio installation and use

Baidu submits sitemap to prompt the solution of "index type is not handled"
随机推荐
Google Gmail mailbox marks all unread messages as read at once
企业展厅设计能为企业带来什么?
Mentality cannot collapse
P4338 [ZJOI2018]历史(树剖)(暴力)
[Fibonacci series]
STC8A8K64D4 EEPROM读写失败
GCC C inline assembly
AOSP ~ WIFI默认开启 + GPS默认关闭 + 蓝牙默认关闭 + 旋转屏幕关闭
ADVANCE.AI首席执行官寿栋将在2022新兴市场品牌出海线上峰会分享跨境电商运用AI技术合规
Byte beating | the first batch of written examination for game R & D post (question solution)
Baidu submits sitemap to prompt the solution of "index type is not handled"
app 测试 常用 adb 命令集合
你的公司会选择开发数据中台吗?
A collection of common ADB commands for app testing
【冒泡排序的实现】
年金保险理财产品可以复利吗?利率是多少?
软件测试英语常见词汇
Li Kou brushing questions - hash table
弄懂了采矿业与碳中和的逻辑,就读懂了矿区无人驾驶的千亿市场
[resolved] how to fix another update in progress WordPress upgrade error