当前位置:网站首页>The new colleague asked me what "where 1=1" means???
The new colleague asked me what "where 1=1" means???
2022-06-12 19:51:00 【Java collection】
When the new colleague came, he asked me where 1=1 What's interesting , It doesn't make sense , I smiled. . Today, let's explain .
where 1=1
Let's start with a piece of code
<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>The above code is familiar , This is the total number of qualified queries . stay mybatis Used in if Tag judgment where Condition after clause , In order to prevent the first field from being empty, resulting in sql Report errors . you 're right , When multiple query conditions are encountered , Use where 1=1 It can easily solve the problem that our condition is empty , So that's how it's written Is there anything wrong with that? ?
Many people on the Internet say , This can cause performance problems , May invalidate the index , So let's measure it today , Will you not go . in addition , More similar interview questions , official account Java selected , reply java interview , Access to interview information , Support online question brushing anytime, anywhere .
Actually measured
title The field has been indexed , We go through EXPLAIN look down :
EXPLAIN SELECT * FROM t_book WHERE title = ' And on earth ';EXPLAIN SELECT * FROM t_book WHERE 1=1 AND title = ' And on earth ';Comparing the above two, we will find You can see possible_keys( Possible indexes ) and key( Actual index used ) All use the index for Retrieval .
Conclusion
where 1=1 I'll go with the index , Does not affect query efficiency , We wrote sql Orders will be mysql Analyze and optimize into your own processing instructions , In the process 1 = 1 Such meaningless conditions will be optimized . Use explain EXTENDED sql Proofread , Find out where1=1 Such conditions will be mysql Optimized by the optimizer .
So we're in mybatis You can change the writing , Because after all mysql The optimizer also takes time , Although it's gone , But when the amount of data is large , It will still have an impact , So we suggest that the code be written like this :
<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>We use it where Label substitution .
author : Su Shi _
https://juejin.cn/post/7030076565673213989
official account “Java selected ” The published content indicates the source of , All rights reserved ( Those whose copyright cannot be verified or whose source is not indicated all come from the Internet , Reprinted , The purpose of reprinting is to convey more information , The copyright belongs to the original author . If there is any infringement , Please contact the , The author will delete the first time !
Many people have asked recently , Is there any readers Communication group ! The way to join is simple , official account Java selected , reply “ Add group ”, You can join the group !
Java Interview questions ( Wechat applet ):3000+ The road test questions , contain Java Basics 、 Concurrent 、JVM、 Threads 、MQ series 、Redis、Spring series 、Elasticsearch、Docker、K8s、Flink、Spark、 Architecture design, etc , Brush questions online at any time !
------ Special recommendation ------
Special recommendation : Focus on the most cutting-edge information and technology sharing , Official account for preparing for overtaking on curves and various open source projects and efficient software ,「 Big coffee notes 」, Focus on finding good things , It's worth your attention . Click the official account card below to follow .
If the article helps , Click to see , Forward! !
边栏推荐
- Ctfshow-web265 (deserialization)
- Module 8 operation
- 设备管理-借还模块界面代码
- What is a hash index?
- Demand and business model innovation - demand 4- overview of demand acquisition
- unity websockt一些知识:
- The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
- 【刷题笔记】线段树
- Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN
- Equipment management - borrowing and returning module 1
猜你喜欢

Reading small program graduation design based on wechat e-book (5) assignment

3D object detection

Low code enables rural construction to open "smart mode"

torch 网络模型转换onnx格式,并可视化

Unsupported class file major version 60

Demand and business model innovation - demand 1 - Introduction to demand engineering

PostgreSQL数据库复制——后台一等公民进程WalReceiver pg_stat_wal_receiver视图

IO流基础知识详解--文件及IO流原理

Pyinstaller packaging tutorial packaging resource files
![[digital ic/fpga] data accumulation output](/img/58/8d10e41a7bc837feba677f1e0b1ceb.png)
[digital ic/fpga] data accumulation output
随机推荐
What are meta-inf and WEB-INF respectively?
Reading small program graduation design based on wechat e-book (5) assignment
Reading applet based on wechat e-book graduation design (2) applet function
运算器的基本结构
SPI one master and many slaves
Test prerequisites: recommend a special cross platform app performance test tool!
Wechat e-book reading applet graduation design completion works (8) graduation design thesis template
基于微信电子书阅读小程序毕业设计毕设作品(8)毕业设计论文模板
[games101] class note 8 - shading (shading frequency, graphics pipeline, texture mapping)
2022年,中国大学生最多的20个城市
Demand and business model innovation-5-process
Demand and business model innovation - demand 2- demand basis
PostgreSQL数据库复制——后台一等公民进程WalReceiver pg_stat_wal_receiver视图
5g R17 standard is frozen. What does it say?
system()
Efcore tuning
Demand and business model analysis-3-design
PostgreSQL database replication - background first-class citizen process walreceiver PG_ stat_ wal_ Receiver view
模塊八作業
Overview of object detection