Updated on 2024-09-24 GMT+08:00

Using Hints for Read/Write Splitting

In addition to the weight assignment rules of read/write splitting, hints serve as a complementary SQL syntax to specify whether an SQL statement is executed on a primary node or read replica.

This section describes how to use hints to route read and write requests to a primary node or read replica.

Precautions

  • Hints are only used as routing suggestions. In non-read-only SQL and non-transaction scenarios, SQL statements cannot be routed to read replicas.
  • If you want to connect to a DB instance using the MySQL CLI and Hints, add the -c option.

Usage

Hints supported by read/write splitting are as follows:

/*FORCE_MASTER*/: An SQL statement is executed on the primary node.

/*FORCE_SLAVE*/: An SQL statement is executed on read replicas.

For example, if you run select * from table1, the SQL statement will be executed on a read replica by default. If you change it to /*FORCE_MASTER*/ select * from table1, the SQL statement will be executed on the primary node.

/*FORCE_MASTER*/ only works for read/write addresses. Even if you use it for a read-only address, the SQL statement will not be executed on the primary node.