Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you index tables differently on Master and Slave (MySQL)

Is it possible to set up different indexing on a read only slave, from on the master? Basically, this seems like it makes sense given the different requirements of the two systems, but I want to make sure it will work and not cause any problems.

like image 827
Scott Miller Avatar asked Dec 10 '10 19:12

Scott Miller


People also ask

What is the difference between master and slave table?

Master vs Slave Simply, a master is a device or a process that controls other devices or processes and a slave is a device or a process that is controlled by another device or a process.

How does MySQL decide which index to use?

If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index). If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.

How master-slave works in MySQL?

The master has a thread, called the dump thread, that continuously reads the master's binlog and sends it to the slave. The slave has a thread, called the IO thread, that receives the binlog that the master's dump thread sent, and writes it to a file: the relay log.

When would you use a master-slave database?

The master-slave database architecture can be used for scaling out your application by distributing your data load across multiple databases.


2 Answers

I believe so. After replication is working, you can drop the indexes on the slave and create the indexes you want and that should do it. Since MySQL replicates statements and not data (at least by default), as long as the SQL necessary to insert or update or select from the table doesn't need to change, it shouldn't notice.

Now there are obviously downsides to this. If you make a unique key that isn't on the master, you could get data inserted on the master that can't be inserted on the slave. If an update is done that uses an index it may run fast on the master but cause a table scan on the slave (since you don't have whatever index was handy).

And if any DDL changes ever happen on the master (such as to alter an index) that will be passed to the slave and the new index will be created there as well, even though you don't want it to.

like image 152
MBCook Avatar answered Oct 26 '22 23:10

MBCook


For sure. I do it all the time. Issues I've run into:

  • Referencing indexes via FORCE/USE/IGNORE INDEX in SELECTS will error out
  • Referencing indexes in ALTER statments on the master can break replication
  • Adds another step to promoting a slave to be the master in case of emergency
  • If you're using statement based replication (the norm), and you're playing around with UNIQUE indexes, any INSERT... ON DUPLICATE KEY, INSERT IGNORE or REPLACE statments will cause extreme data drifting / divergence
  • Performance differences (both good and bad)
like image 37
Riedsio Avatar answered Oct 27 '22 01:10

Riedsio