当前位置:网站首页>How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
2022-07-30 05:26:00 【m0_67391518】
I. Introduction/Overview
This week my colleague asked me a question, how to use placeholders for preprocessing in SQL, and why he used prepare but did not take effect;
1. Introduction to prepare
When executing an SQL statement multiple times, if the SQL statement is processed each time to generate an execution plan, it will inevitably waste a certain amount of time.
SQL preprocessing (Prepare) is a special SQL processing method; preprocessing does not directly execute SQL statements, but first compiles SQL statements to generate an execution plan, and then executes SQL statements with SQL parameters through the Execute command.
- Prepare is widely used, and most ORM frameworks have API support;
- Prepare can not only improve SQL execution performance, but also prevent security problems caused by SQL injection;
- Although the syntax of Prepare is very different in each database, in general, we do not write SQL by hand, but use the ORM framework to do it;
2. The origin of prepare?
From the perspective of the mysql server executing sql, the SQL execution process includes the following stages: lexical analysis -> syntax analysis -> semantic analysis -> execution plan optimization -> execution.
The two stages of lexical analysis->grammatical analysis are called hard parsing.
- Lexical analysis identifies each word in sql;
- Syntax analysis parses whether the SQL statement conforms to the SQL syntax, and obtains a syntax tree (Lex).
For sql with different parameters but the same others, their execution time is different but the hard parsing time is the same.
As the query data changes for the same SQL, the execution time of multiple queries may be different, but the time for hard parsing is unchanged.Therefore, for the shorter SQL execution time, the higher the ratio of SQL hard parsing time to the total execution time is.
Prepare appears to optimize the problem of hard parsing.
Although Prepare can save hard parsing time in the execute phase.But if sql is only executed once and executed in prepare mode, then sql execution requires two interactions with the server (prepare and execute), while in normal (non-prepare) mode, only one interaction is required.Using prepare in this way will bring additional network overhead, which outweighs the gain.
If the same SQL needs to be executed multiple times, for example, 10 times in the prepare mode, only one hard parsing is required.At this time, the additional network overhead is negligible; therefore prepare is more suitable for frequently executed SQL.
Second, prepare for IN query
MySQL preprocessing is a set of SQL operations. It does not have a fixed syntax format, but in most cases it is used in the following 3 steps:
- Use the PREPARE directive to predefine the SQL statement template;
- Use the SET command to define SQL parameters;
- Use the EXECUTE command to execute the SQL template with parameters.
1. The preprocessing IN query is invalid!
SQL:
prepare myFun from 'select * from user where id IN (?)';set @str='1,2';execute myFun using @str;The above SQL will do three things:
- Create preprocessing SQL (
myFun), where variables are to be used are indicated with; - set variable
@str; - Execute preprocessing SQL (
myFun),USINGfollowed by parameters, if there are multiple parameters (ie multiple placeholders?) are separated by commas,;
SQL execution result:
Judging from the SQL execution results, it is found that the preprocessing SQL is not fully effective, and only one row of records is queried; normally, two rows of records should be found;
2. Solve the problem of invalid preprocessing IN query
Replace with the following SQL:
prepare myFun from 'select * from user where FIND_IN_SET(id,?)';set @str='1,2';execute myFun using @str;SQL execution result (as expected):
Dynamic IN queries in 3, MyBatis and JDBC
mybatis supports the dynamic sql query of in, the input parameter can be a Collection, and the bottom layer builds the content of the Collection into a string separated by commas , ; it can also be an English comma , delimited strings.
In the native jdbc, sql does not support IN to directly pass in a comma-spliced string for a single placeholder, such as in ('001', '002'), which requires the developer to specify the number of parameters passed in.The number of placeholders to construct;
For example, pass in an array or List, {'001', '002'}; the in that needs to be constructed is: in(, );
Then dynamically construct the SQL statement through the PreparedStatement#setString() method.
Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- RadonDB PostgreSQL on K8s 2.1.0 发布!
- go语言学习笔记四
- go语言学习笔记二
- MySQL安装配置教程(超级详细)
- Unity踩坑记录 —— GetComponent的使用
- gnss rtcm rtklib Ntrip...
- C language implements highly secure game archives and reads files
- GO language study notes one
- [3D Detection Series-PointRCNN] Reproduces the PointRCNN code, and realizes the visualization of PointRCNN3D target detection, including the download link of pre-training weights (starting from 0 and
- C语言中的基本库函数(qsort)
猜你喜欢

Seven, custom configuration

uni-app realizes cross-end development of mobile phone Bluetooth to receive and send data

力扣344-反转字符串——双指针法

程序员大保健指南,给自己的身心偶尔放松的机会
![[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset](/img/f2/429e3dd157647bb614595a83843140.png)
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset

Small program npm package--API Promise

微信支付及支付回调

Some understanding of YOLOv7

是时候不得不学英语了,技多不压身,给自己多条路

ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
随机推荐
Small programs use npm packages to customize global styles
参与开源,让程序员找回热血和激情
mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
(RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
翻译 | Kubernetes 将改变数据库的管理方式
容器化 | 在 KubeSphere 中部署 MySQL 集群
Hexagon_V65_Programmers_Reference_Manual (11)
BindingExpression path error: 'selectMenusList' property not found on 'object' ''ViewModel'
给小白的 PostgreSQL 容器化部署教程(上)
Hexagon_V65_Programmers_Reference_Manual(14)
leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
Unity stepping on the pit record - the use of GetComponent
Let's talk about what SaaS is, and the problems encountered...
Acwing完全数
Summary of skills in using ms project2010 project management software
Seven, custom configuration
无代码开发平台子管理员入门教程
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
聊一聊什么是SaaS,以及遇到的问题......
涂鸦Wi-Fi&BLE SoC开发幻彩灯带