Flip it casually , You can see the use where 1=1 There are many people , Maybe this is already a coding habit .

Then use where 1=1 , What will happen .

first , Refute the rumor first .  

 
where 1=1 Will not invalidate the index !
where 1=1 Will not invalidate the index !
where 1=1 Will not invalidate the index !

Treat objectively :

Why did you write this where 1=1 ? 

WHERE 1=1 

Example :
<select id="queryList" resultMap="BaseResultMap"> select <include
refid="Base_Column_List"/> from user_info where 1=1 <if test="type !=null"> and
TYPE = #{type} </if> </select>
Maybe many people see this , Already know , actually mybatis have where Tags are specifically used for this scenario .

And most people know where label , Just used to it where 1=1 .

Actually, there are two points :

1. Copy paste , Any query is if label , Copy and change , In order not to report errors , add where 1=1 , Finish .
2. know mysql Will receive sql Optimize instructions , such where 1=1 It will be optimized , So it doesn't affect .

 

Does not affect index hits :

  Not used where 1=1 , Able to hit the index :

 

  use where 1=1 , Same hit index :

 

So say it again :  where 1=1 Will not invalidate the index !

Mybatis where label

Example :
<select id="queryList" resultMap="BaseResultMap"> select <include
refid="Base_Column_List"/> from user_info <where> <if test="type !=null"> and
TYPE = #{type} </if> </where> </select>
  You can see the use where Label words , The first feeling is , Make the code smoother , But there is one more where Knowledge of label use needs to be known .

Can see , My example , above where label   inside First of if Inside the label Yes and .

It doesn't matter ,mybatis It will also help us optimize , first and This kind of thing doesn't matter .

Because there are many if When labels exist at the same time , Just make sure that the triggered tag   Between two is used and perhaps or Just connect these ,
The first one will be optimized automatically . Of course, this is mybatis Help us do things .

How should I know? ? Look at the source code :
mybatis where Source code of label :

You can see in the code where label It's just a definition So called prefix list, Actually, it's still used super.

Inherited parent class , It's actually trim label .
So we use where Example of label writing , It can also be used instead trim label :
 
<select id="queryList" resultMap="BaseResultMap"> select <include
refid="Base_Column_List"/> from user_info <trim prefix="WHERE"
prefixOverrides="AND | OR" > <if test="type !=null"> and TYPE = #{type} </if>
</trim> </select>

trim label , You can also take a look at the source code :

prefix: to trim Inside the label sql Statement prefixed
suffix: to trim Inside the label sql Statement with suffix
prefixOverrides: Remove redundant prefix content , as :prefixOverrides=“AND”, remove trim Inside the label sql Statement redundant prefix "AND"
suffixOverrides: Remove redundant suffix content , as :suffixOverrides=",", remove trim Inside the label sql Statement redundant suffix ","

that mybatis How to deal with this where label ( Include any other labels ) Yeah ?
actually mybatis There are relevant test functions on the source code , We can also see how to interpret it where label , Finally, it is spliced into sql of .

Those who are interested can have a look by themselves ( If you have an idea , In fact, you can define your own set of rules , Anyway, it is finally analyzed and spliced sql sentence .):
 

  Problems worth thinking about

Far away , So back to the point , Treat use objectively where 1=1 .

Some are listed above where Source code of label , Why should I list these .

In fact, it is to lead to a problem worthy of consideration ?

use where 1=1 ,mysql The bottom layer will help us optimize , Does not affect the index .

use where label , Need Code level Help us analyze .

So in this way , Is it used where label , In fact, there will be performance problems , After all, we have to go through code parsing ? 

Then choose java Analysis or selection of code application layer mysql The bottom optimization ?

This question is for everyone to think about .

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?