Hi! I'm working on some Worker code which needs to fetch about a 1000 of small rows from a MySQL dat

Hi! I'm working on some Worker code which needs to fetch about a 1000 of small rows from a MySQL database (hosted by PlanetScale). When I connect using Hyperdrive, the CPU usage per request is way higher than when connecting directly. For example, 5ms with a direct connection, compared to 40ms with Hyperdrive.
The root cause - to my understanding - is the fact that when receiving data from Hyperdrive, each resulting row (mysql protocol packet) is returned in what appears to be a separate TCP packet. In my tests, each call to ReadableStreamBYOBReader.read returns only 22 bytes when receiving query results (this matches the size of the mysql packet), despite having lots of free space in the buffer.
I find this surprising, because conventional TCP mechanisms such as Nagle's algorithm should merge these small packets. If my understanding is correct, then this behaviour also introduces inefficiencies at other OS/network layers.
For comparison, when I connect directly to planetscale, the data is returned in much larger chunks (multiple kilobytes), allowing for more efficient processing.
A dirty workaround is to simply add a delay before receiving the query result. This allows one read() call to process a larger chunk of data.
Was this page helpful?