Re: Unsafe statement written to the binary log
Posted by:
Rick James
Date: March 27, 2012 08:43PM
> 1. INSERT... SELECT... ON DUPLICATE KEY UPDATE is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are updated. This order cannot be predicted and may differ on master and the slave.
> 2. The statement is unsafe because it uses a LIMIT clause. This is unsafe because the set of rows included cannot be predicted.
Both of those may be solved by adding an ORDER BY. I say "may be" for two reasons:
* If the ORDER BY does not completely determine the order, it is inadequate.
* There is a case for which ORDER BY is not sufficient. (Sorry, I can't seem to find the item in bugs.mysql.com at the moment.)
One workaround is (roughly, and in pseudo code):
1. $last_id = SELECT id ... ORDER BY id LIMIT 100,1;
2. UPDATE ... WHERE id BETWEEN $first_id AND $last_id;
The idea is to avoid any ambiguity in the replication stream by having a precise range over which do do the operation.
The reason for the error (which is relatively new, in spite of the potential error existing since day 1), is just what it says -- The Slave could pick a different set of rows, thereby making the Master and Slave out of sync.
Suggest you present a small, fully deterministic, test case to bugs.mysql.com.
Subject
Views
Written By
Posted
9161
March 23, 2012 02:47AM
3720
March 23, 2012 07:41AM
3569
March 23, 2012 07:43AM
3365
March 25, 2012 03:15PM
4223
March 26, 2012 07:32AM
Re: Unsafe statement written to the binary log
4411
March 27, 2012 08:43PM
2951
April 02, 2012 04:15AM
2820
April 02, 2012 11:43PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.