当前位置:网站首页>mysql----where 1=1是什么意思
mysql----where 1=1是什么意思
2022-06-28 04:13:00 【张 邵】
新的同事来之后问我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
边栏推荐
- inherit
- Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
- [CSP-J2020] 优秀的拆分
- Source code of live video system, countdown display, countdown of commodity spike
- The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
- Ppt production tips
- Is the securities account opened by qiniu safe? How to open an account
- Why are cloud vendors targeting this KPI?
- 短视频平台开发,点击链接、图片自动跳转到新的页面
- Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e
猜你喜欢

Design a stack with getmin function

UI automation test framework construction - write an app automation

控制器的功能和工作原理

2022年中國音頻市場年度綜合分析

Annual comprehensive analysis of China's audio market in 2022
![leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]](/img/e4/5ec39aced223512b162020d05eb313.png)
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]

Introduction to SQLSERVER database

Matlab exercises -- routine operation of matrix

Play with double pointer

How to traverse collections Ordereddict, taking it and forgetting items
随机推荐
2022年中國音頻市場年度綜合分析
Go language -select statement
Go语言学习教程(十四)
Aspnetcoreratelimit rate limit interface access limit current limit control
Secouer le son et se battre ~ prêter attention au blogueur
After launching the MES system, these changes have taken place in the enterprise
xml&nbsp; File read / write
Analyse complète annuelle du marché chinois de l'audio en 2022
快速下载JDK,除了官方Oracle下载,还有国内可以有最新版本的下载地址吗
If mysqlcdc sets multiple parallelism, will the incremental data repeat?
Project practice! Teach you JMeter performance test hand in hand
测试开发必备技能:安全测试漏洞靶场实战
27年,微软IE结束了!
Live online source code, JS dynamic effect, sidebar scrolling fixed effect
Detailed reading of the thesis: implementing volume models for handowriting text recognition
Mask's miserable and inspirational childhood, who is introverted by campus violence
Another option for ERP upgrade, MES system
The development of the Internet has promoted the emergence of a series of new models such as unbounded retail, digital retail and instant retail
Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e
如何遍历collections.OrderedDict,服了又忘记items