TheRobotLearns Platform · ProxySQL: from first install to production
Two-Tier IDs and Global Dedup
Learning objectives
- Explain how client, ProxySQL, and backend prepared-statement IDs differ during execute routing.
- Route execute traffic across pooled connections using client-side, global, and backend-side mappings.
- Predict when ProxySQL answers prepare requests from global content-hash state without a backend.
- Determine how username and schema separate or reuse global prepared-statement entries.
- Determine when a global prepared-statement entry can be safely evicted.
Why statement IDs differ
You prepare a query through ProxySQL, then the next execute arrives on a pooled backend connection that never saw the prepare. The backend numeric identifier from the first connection is not useful there. ProxySQL cannot forward a client identifier straight to the backend.
Every party assigns its own numeric statement identifier. The client identifier belongs to one client session. ProxySQL assigns it when the client prepares a statement. Two clients may hold the same numeric client identifier for different statements.
A global identifier is process-wide and represents one unique statement. It is the pivot point. Client-side identifiers map to global identifiers, and global identifiers map to backend-side identifiers. The global registry holds the statement metadata needed to prepare the statement again.
A backend-side identifier belongs to one backend connection. MySQL assigns it when ProxySQL sends COM_STMT_PREPARE on that connection. The same query prepared on two backends can receive two different backend identifiers.
Execute routing uses these levels. ProxySQL reads the client-side ID, finds the global ID, then finds the backend-side ID for the backend connection that will run the next message.
Takeaway: Client, global, and backend identifiers are separate namespaces, and ProxySQL translates between them.
How pooled execute finds backend
A client prepares on one backend, then sends execute after ProxySQL multiplexed that client onto another pooled connection. If ProxySQL forwarded the client identifier directly, execution would fail or produce incorrect results. The global ID is the pivot that lets ProxySQL translate the client identifier to a backend-side ID on the next connection.
ProxySQL stores the client-side ID for each client and maps it to the global ID. The client sees only its own numeric identifier. ProxySQL then needs a backend-side ID for the connection handling the next execute.
If that connection has prepared the statement, ProxySQL uses the recorded mapping from the global ID to that connection's backend ID. If the connection has not seen the prepare, ProxySQL sends COM_STMT_PREPARE on demand. The statement already exists in the global registry, so the backend can be prepared lazily when execute arrives.
The same global ID can serve many client IDs and many backend IDs. This mapping makes pooling, lazy preparation, and transparent proxying possible.
Takeaway: Execute traffic flows from client-side ID to global ID to the backend-side ID chosen for that pooled connection.
Which prepare reaches backend
Many clients prepare the same query against the same database. Without deduplication, each client connection would independently prepare the same statement on each backend. ProxySQL computes a content hash before doing anything else. The hash comes from the SQL query text, the connected username, and the current schema.
If the hash matches an entry already in the global registry, ProxySQL reuses that entry. It returns cached metadata to the client. It does not send COM_STMT_PREPARE to a backend for that request.
Only the client that triggered the first prepare for that hash sends the statement to a backend. Every later client with the same hash receives the already-prepared statement's metadata.
The username matters because clients can use identical SQL under different security contexts. The schema matters because identical text can resolve table references differently in another database. Different user or schema produces a different global entry.
Takeaway: ProxySQL skips the backend prepare when the SQL text, username, and schema hash already match a global entry.
When entries can be evicted
A client closes a prepared statement, or a backend connection is destroyed. Those events change reference counts, but ProxySQL drops the global entry only after both counts reach zero.
The client reference count tracks how many client-side IDs map to this global entry. When a client sends COM_STMT_CLOSE, ProxySQL removes that client-side mapping and decrements the count.
The backend reference count tracks how many backend connections have this statement prepared. When a backend connection is destroyed, ProxySQL removes its mappings. It decrements each referenced global entry.
A global entry is eligible for eviction only when both counts have reached zero. Live clients expect to execute it, and live backends hold resources for it.
Routing timing changed in v2.1.0. Query rules are re-evaluated at COM_STMT_EXECUTE time, so routing reflects the session context at execution.
Before v2.1.0, routing metadata was cached at prepare time. A statement prepared against a reader hostgroup could keep executing there even inside a write transaction. In v2.2.0, ProxySQL includes the first_comment when computing routing for COM_STMT_EXECUTE, so statements carrying an explicit hostgroup annotation are routed to the requested hostgroup during execution.
Takeaway: Evict only when client and backend reference counts are zero; from v2.1.0, execute re-evaluates query rules.
Protocol steps behind the mappings
-- COM_STMT_PREPARE: ProxySQL computes the content hash.
-- COM_STMT_EXECUTE: ProxySQL maps the client-side ID through the global ID.
-- COM_STMT_CLOSE: ProxySQL decrements the client reference count.This comment-only script names the protocol messages ProxySQL handles. The first line marks COM_STMT_PREPARE, when ProxySQL computes the content hash and may reuse a global entry. The second line marks COM_STMT_EXECUTE, when ProxySQL translates a client-side ID through the global ID to a backend-side ID. The third line marks COM_STMT_CLOSE, when ProxySQL removes the client-side mapping and decrements the client reference count.
Sources
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
- MySQL Prepared Statements Architecture
Do more with an account
- Graded quizzes is available on your account.
- Saving your progress is available on your account.
- Hands-on labs is available on your account.
- AI tutoring is available on your account.