Client/Server Communication in MySQL - The Common 4-Byte Header
(Page 2 of 4 )
Table 4-1. Common 4-byte header for uncompressed packets
| Offset | Length | Description |
|---|
| 0 | 3 | Packet body length stored with the low byte first. |
| 3 | 1 | Packet sequence number. The sequence numbers are reset with each new command. While the correct packet sequencing is ensured by the underlying transmission protocol, this field is used for the sanity checks of the application logic. |
A compressed packet will have an additional 3-byte field, low byte first, containing the length of the compressed packet body part that follows. An uncompressed packet will have the body immediately after the header.
The compression is done with the use of ZLIB (see http://www.zlib.net). The body of the compressed packet is exactly what a call to compress() with the uncompressed body as argument would return. It is, however, possible for the body to be stored without compression when the compressed body would turn out no smaller than the uncompressed one, or when compress() fails for some reason—e.g., due to the lack of available memory. If this happens, the uncompressed length field will contain 0.
It is important to remember, though, that even in that case, the compressed format is still used, which unfortunately results in the waste of 3 bytes per packet. Therefore, a session that predominately uses small or poorly compressible packets goes faster if the compression is turned off.
As you may have noticed, the 3-byte field would limit the body length to 16 MB. What if you need to send a bigger packet? In version 3.23 and earlier, it is not possible. Version 4.0 added a compatible improvement to the protocol that overcame this limitation. If the length of the packet is greater than the value of MAX_PACKET_LENGTH , which is defined to be 224–1 in sql/net_serv.cc, the packet gets split into smaller packets with bodies of MAX_PACKET_LENGTH plus the last packet with a body that is shorter than MAX_PACKET_LENGTH . The last short packet will always be present even if it must have a zero-length body. It serves as an indicator that there are no more packet parts left in the stream for this large packet.
Next: Relationship Between MySQL Protocol and OS Layer >>
More Database Articles Articles
More By O'Reilly Media
|
This article is excerpted from chapter 4 of the book Understanding MySQL Internals, written by Sasha Pachev (O'Reilly, 2007; ISBN: 0596009577). Check it out today at your favorite bookstore. Buy this book now.
|
|