当前位置:网站首页>The new colleague asked me what "where 1=1" means???

The new colleague asked me what "where 1=1" means???

2022-06-11 02:49:00 Java confidant_

Click on the official account , Practical technical articles Know in time 2e4d5842958699447a1a1b595801295e.png

Write it at the front

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

Actually measured

title The field has been indexed , We go through EXPLAIN look down

EXPLAIN SELECT * FROM t_book WHERE title = ' And on earth ';
78d5bb8ebd2727a0091f53efde79fbca.png
EXPLAIN SELECT * FROM t_book WHERE 1=1 AND title = ' And on earth ';
ec871c724b6408185c1b109a72f5ebd9.png

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 .

overtones

Thanks for reading , If you feel like you've learned something , You can like it , Focus on . You are also welcome to comment and exchange with us if you have any questions

source :juejin.cn/post/7030076565673213989

recommend

Java Interview questions

Technical involution group , Learn together !!

e0d53c64d444943eef288bd98b728b53.png

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !

原网站

版权声明
本文为[Java confidant_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110207228089.html

随机推荐