MySQL 8.0.39
Source Code Documentation
|
Topics in this section:
In the MySQL Client/Server Protocol, a PREPARE
/EXECUTE
cycle required two round-trips as the COM_STMT_EXECUTE
requires data from the COM_STMT_PREPARE-ok
packet.
The X Protocol is designed in a way that the EXECUTE
stage doesn't depend on the response of the PREPARE
stage.
That allows the client to send both PREPARE
and EXECUTE
after each other without waiting for the server's response.
When inserting a large set of data (data import), make a trade-off among:
For this example it is assumed that 1 million rows, each 1024 bytes in size have to be transferred to the server.
Optimizing for Network Round-Trips**
(Assuming the MySQL Client/Server Protocol in this case) Network round-trips can be minimized by creating a huge SQL statements of up to 1Gbyte in chunks of 16Mbyte (the protocol's maximum frame size) and sending it over the wire and letting the server execute it.
In this case:
The Execution Time for inserting all rows in one batch is:
Optimizing for Memory Usage and Error-Reporting**
The other extreme is using single row INSERT
statements:
Using Prepared Statements solves the last bullet point:
The Execution Time for inserting all rows using prepared statements and the MySQL Client/Server Protocol is:
Optimizing for Execution Time and Error-Reporting**
In the X Protocol, a pipeline can be used to stream messages to the server while the server is executing the previous message.
The Execution Time for inserting all rows using prepared statements and using pipelining is (assuming that the network is not saturated):
one
network latency to get the initial prepare
/execute
across the wireone
network bandwith to get the initial prepare
/execute
across the wire. All further commands arrive at the server before the executor needs them thanks to pipelining.one
row parse time to parse the prepare
num_rows
row execution time stays as beforeIn case error reporting isn't a major topic one can combine multi-row INSERT
with pipelining and reduce the per-row network overhead. This is important in case the network is saturated.
Inserting multiple documents into a collection col1
is a two-step process:.
By utilizing pipelining the execute
message can be batched without waiting for the corresponding executeOk
message to be returned.
Deleting documents from collection col2
based on data from collection col1
.
Instead of fetching all rows from col1
first to construct a big delete
message it can also be run in nested loop: