[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Create and manage indexes on read replicas\n\n\u003cbr /\u003e\n\nMySQL \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis page describes how to add and drop indexes on Cloud SQL read\nreplicas. While a replica is normally read-only, there might be times you want\nto create [secondary indexes](https://dev.mysql.com/doc/refman/8.0/en/innodb-index-types.html)\non tables for reporting purposes. Cloud SQL offers a set of [stored\nprocedures](/sql/docs/mysql/stored-procedures) for managing these indexes.\n\nTerminology\n-----------\n\n- **Clustered index.** The main index on a MySQL table that physically orders the rows on disk. When you define a primary key on a table, MySQL uses it as the clustered index. There can only be one clustered index on a table.\n- **Secondary index.** An additional index on a MySQL table that optimizes query performance.\n\nStored procedures for indexes\n-----------------------------\n\nCloud SQL includes two stored procedures in the `mysql` schema that you\ncan use to add and drop secondary indexes on a MySQL read replica. Note that\nwhile these procedures can run on a primary source instance, they are designed\nfor read replicas.\n\nmysql.addSecondaryIdxOnReplica\n: Adds a secondary index on the database. This stored procedure is a\n wrapper for the\n [CREATE INDEX](https://dev.mysql.com/doc/refman/8.0/en/create-index.html)\n DDL statement.\n\n #### Parameters:\n\n\n - **`idxType`** - Type of index to create. For example, pass UNIQUE to create a unique index.\n - **`idxName`** - Name of the index.\n - **`tableName`** - Name of the table in the format of schema.name.\n - **`idxDefinition`** - Definition of the index. Do not include outer parentheses.\n - **`idxOption`** - Any additional options to pass on index creation. For example, in MySQL 8.0, an option could pass INVISIBLE for an invisible index.\n\n\n #### Syntax:\n\n\n ```\n mysql.addSecondaryIdxOnReplica(idxType, idxName, tableName, idxDefinition, idxOption)\n \n ```\n\nmysql.dropSecondaryIdxOnReplica\n: Drops a secondary index on the database. This stored procedure is a\n wrapper for the\n [DROP INDEX](https://dev.mysql.com/doc/refman/8.0/en/drop-index.html)\n DDL statement.\n\n #### Parameters:\n\n\n - **`idxName`** - Name of the index.\n - **`tableName`** - Name of the table in the format of schema.name.\n - **`idxOption`** - Any additional options to pass when dropping an index. For example, an algorithm option like INPLACE.\n\n\n #### Syntax:\n\n\n ```\n mysql.dropSecondaryIdxOnReplica(idxName, tableName, idxOption)\n \n ```\n\nFor the `idxType` and `idxOption` parameters, consult the documentation for the\nmajor version of MySQL running on the Cloud SQL instance.\n\nExamples\n--------\n\n### Correct Usage\n\nHere are some example invocations of the procedures. Suppose we have a table\nwith the following definition. \n\n CREATE TABLE sampletest.t1(\n id int(10) unsigned NOT NULL AUTO_INCREMENT,\n first_name varchar(64) NOT NULL,\n last_name varchar(64) NOT NULL,\n license_id int NOT NULL,\n PRIMARY KEY (id),\n KEY idx_fname (first_name)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8\n\nIf you wanted to create a regular index named `t1_fname_lname` on the\n`first_name` and `last_name` columns, you would execute the following: \n\n call mysql.addSecondaryIdxOnReplica('', 't1_fname_lname', 'sampletest.t1', 'first_name, last_name', '')\n\nIf you also wanted to create a unique index named `t1_license_id` on the\n`license_id` column with the comment \"unique license id,\" you would execute the\nfollowing: \n\n call mysql.addSecondaryIdxOnReplica('unique', 't1_license_id', 'sampletest.t1', 'license_id', 'comment \\\"unique license id\\\"')\n\nIf you then wanted to drop the t1_fname_lname index, you would execute the following: \n\n call mysql.dropSecondaryIdxOnReplica('t1_fname_lname', 'sampletest.t1', '')\n\n### Incorrect Usage\n\nThe following attempt to create an index on the `first_name` and `last_name`\ncolumns fails due to the outer parentheses in the idxDefinition parameter. \n\n call mysql.addSecondaryIdxOnReplica('', 't1_extra_parenthesis', 'sampletest.t1', '(first_name, last_name)', '')\n\nYou can only add indexes on customer created tables. The following attempt to\ncreate an index on the host column of the mysql.servers table fails. \n\n call mysql.addSecondaryIdxOnReplica('', 'idx_invalid', 'mysql.servers', 'host', '')\n\nYou can only use the `dropSecondaryIdxOnReplica` procedure to drop indexes\npreviously created using the `addSecondaryIdxOnReplica` procedure. For example,\nthe following call to drop the existing `idx_fname` index fails. \n\n call mysql.dropSecondaryIdxOnReplica('idx_fname', 'sampletest.t1', '')\n\nSQL injection in these procedure calls will fail. For example, the following SQL\ninjection with a comment sequence will fail. \n\n call mysql.addSecondaryIdxOnReplica(\\\"user 'a'@'%' --\\\", 'idx_fname', 'sampletest.t1', 'first_name', '')\n\nSimilarly, this SQL injection attempt with a delimiter fails. \n\n call mysql.addSecondaryIdxOnReplica('', 'idx_fname', 'sampletest.t1', 'first_name', ';flush status')\n\nRecreation of read replicas\n---------------------------\n\nOccasionally, when there's an issue, Cloud SQL recreates a read replica\nfrom the primary source in order to quickly recover the instance. Indexes\ncreated on the read replica prior to a recreate operation are not persisted. It\nis the responsibility of the customer to recreate these indexes using the stored\nprocedures on the read replica.\n\nWhat's next\n-----------\n\n- [Connecting using a database client from a local machine or Compute Engine](/sql/docs/mysql/connect-admin-ip)"]]