[PYTHON] Accelerate a large number of simple queries with MySQL

No matter how light the query, for example, if there is an RTT between the web server and MySQL for 5ms and 20 queries are executed, the RTT alone will take 100ms.

The technique of using bulk insert (a query that writes multiple rows after VALUES) when inserting a lot of data is well known. However, this technique cannot be used in the following cases.

For example, there was the following scene in a certain case of our company.

I used multiple statements and multiple result sets to speed up these situations.

Multiple Statements

Multiple queries can be sent in one shot (one Cursor.execute () call at the Python level, one COM_QUERY packet at the protocol level) separated by;. By connecting multiple INSERT statements with;, you can speed up a large number of INSERT statements and UPDATE statements.

Not only can RTT be reduced, but the number of TCP packets can also be reduced, so it can be expected to reduce the load on the part that receives queries on the MySQL server side.

However, this mechanism of concatenating queries using; is also often abused in SQL injection. Therefore, the MySQL protocol allows you to disable multiple statements during the handshake. Depending on the MySQL client library, you may need an option to take advantage of multiple statements.

For example, Python's mysqlclient has multiple statements available by default. (For backwards compatibility with the original fork library ...) On the other hand, Go's github.com/go-sql-driver/mysql must specify multiStatements = true.

Another caveat is that placeholder is not available when using MySQL's prepared statement (protocol level rather than PREPARE statement). You have to escape it yourself, assemble the SQL string and then throw the query, or set it not to use prepared statements.

Multiple Result sets

If you want to combine multiple SELECT statements, you need to not only throw multiple queries but also receive the results. To do this, use mutltiple result sets.

When multiple Result Sets are returned, Python can use Cursor.nextset () to receive the next Result Set.

import MySQLdb

con = MySQLdb.connect(host="127.0.0.1", port=3306, user="test", password="test")
cur = con.cursor()

q = """\
select 1;
select 2;
select 3;
select 4;
select 5;
"""

cur.execute(q)

while True:
    print(cur.fetchall())
    if not cur.nextset():
        break

In case of Go, use Rows.NextResultSet () of database / sql to receive multiple Result Sets in the same way. I can.

Recommended Posts

Accelerate a large number of simple queries with MySQL
Connect a large number of videos together!
Upload a large number of images to Wordpress
Organize a large number of files into folders
[Python] Randomly generate a large number of English names
Consolidate a large number of CSV files in folders with python (data without header)
A tool to follow posters with a large number of likes on instagram [25 minutes to 1 second]
A simple sample of pivot_table.
Paste a large number of image files into PowerPoint [python-pptx]
Scrapy-Redis is recommended for crawling a large number of domains
Lambda + Python is good at restricting access with a large number of IP address lists
Executing a large number of Python3 Executor.submit may consume a lot of memory
Rubyist tried to make a simple API with Python + bottle + MySQL
Implementation of a simple particle filter
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
Creating a simple app with flask
Convert a large number of PDF files to text files using pdfminer
How to create a large amount of test data in MySQL? ??
I want to backtest a large number of exchange pairs and strategies at once with Python's backtesting.py
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
Use shutil to delete all folders with a small number of files
ETL processing for a large number of GTFS Realtime files (Python edition)
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Generate a list packed with the number of days in the current month.
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
Creating a simple PowerPoint file with Python
Maximum average number of daily visitors (large)
A simple RSS reader made with Django
Create a large text file with shellscript
Let's make a simple language with PLY 1
Create a simple web app with flask
Expand any number of arguments with yasnippet
Pandas: A very simple example of DataFrame.rolling ()
I made a simple blackjack with Python
Count the number of characters with echo
One-liner to create a large number of test files at once on Linux
I tried to make a simple mail sending application with tkinter of Python
Find out how to divide a file with a certain number of lines evenly
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Set up a simple HTTPS server with asyncio
A simple example of how to use ArgumentParser
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Make a simple pixel art generator with Flask
Calculate the total number of combinations with python
Start a simple Python web server with Docker
Find the number of days in a month
Solve A ~ D of yuki coder 247 with python
[Python] Make a simple maze game with Pyxel
I made a simple Bitcoin wallet with pycoin
Get a list of IAM users with Boto3
Automating simple tasks with Python Table of contents
Flow of creating a virtual environment with Anaconda
A simple to-do list created with Python + Django
I made a random number graph with Numpy
Create a table of contents with IPython notebook
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
A script that combines margins when pasting a number of graphs on tiles with gnuplot
How to identify the element with the smallest number of characters in a Python list?
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
Use API to mark a large number of unread emails in Gmail as read