MySQL中パラメータsql_safe_udatesの生産環境での使用詳細


前言
バグやDBAを使って誤操作した場合、全表を更新します。MySQLはsql_を提供しますsafe_udatesは次の操作を制限します。

set sql_safe_updates = 1;
設定後は、udate deleteにwhere条件のないSQLの実行を制限します。厳しいです。既存のオンライン環境に悪影響を及ぼす。新しいシステム、アプリケーションを厳しく審査して、全表更新の問題が発生しないように確保できます。

CREATE TABLE working.test01 (id INT NOT NULL AUTO_INCREMENT,NAME VARCHAR(20),age INT,gmt_created DATETIME,PRIMARY KEY(id));

 insert into test01(name,age,gmt_created) values('xiaowang',2,now());
 insert into test01(name,age,gmt_created) values('huahua',5,now()); 
 insert into test01(name,age,gmt_created) values('gougou',9,now()); 
 insert into test01(name,age,gmt_created) values('heihei',12,now()); 
 insert into test01(name,age,gmt_created) values('baibai',134,now()); 

#          
update
update test01 set name = 'xiaoxiao' where age = 2 ;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
#     
update test01 set name = 'xiaoxiao';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
#   limit   
update test01 set name = 'xia' limit 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

#     
create index idx_age on test01(age);

update test01 set name = 'xiaoxiao' where age = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

update test01 set name = 'hhh' where age = 9 limit 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

alter table test01 drop index idx_age;
create index idx_age_name on test01(age,name);


update test01 set age= 100 where name = 'hhh';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

update test01 set age= 100 where name = 'hhh' limit 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
これにより、udateの場合は、where条件またはwhereがない後にインデックスフィールドでない場合は、limitを使用しなければなりません。where条件がある場合は、索引フィールドとして
最近また仕事中に問題を発見しました。safe_udatesはサブクエリの更新をサポートしていません。
開発者が誤ってデータを更新することがあることを考慮して、オンラインライブラリのMySQL例にsql_を設定することを要求します。safe_udates=1はインデックスがないudate、deleteを避けます。
その結果、ある日の開発で次のSQLは正しく実行できないことが分かりました。

update t1 set col2=1 where key1 in (select col2 from t2 where key2='ABcD');
エラーは以下の通りです

ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
つまりインデックスに来ていないwhere条件を更新することはできません。検索してみたら、確かにだめです。適時key 1とkey 2はそれぞれt 1、t 2のインデックスです。説明は、サブクエリに対応していないudateです。
googleは人がこの問題を聞いたことがあることを発見しました。
http://stackoverflow.com/questions/24314830/query-not-getting-executed-if-supplied-a-nested-sub-query
最終解決方法:
1)sessionレベルのパラメータを変更する:set sql_safe_updates=0; がudate動作を実行する。端末を終了します。
2)プログラム処理:まずselect col2 from t2 where key2='ABcD' でデータを取得し、その後、ループ処理結果をupdate t1 set col2=1 where key1=?で一括更新した。プログラムで処理するか、一時的に変数を修正するのは長い間の計ではないと提案します。
締め括りをつける
以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に対して一定の参考となる学習価値を持っています。質問があれば、メッセージを書いて交流してください。ありがとうございます。