這篇文章主要介紹了中參數sql_safe_updates在生產(chǎn)環(huán)境怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
前言
在應用 BUG或者 DBA誤操作的情況下,會(huì )發(fā)生對全表進(jìn)行更新:update delete 的情況。MySQL提供 sql_safe_updates 來(lái)限制次操作。
set sql_safe_updates = 1;
設置之后,會(huì )限制update delete 中不帶 where 條件的SQL 執行,較嚴格。會(huì )對已有線(xiàn)上環(huán)境帶來(lái)不利影響。對新系統、應用做嚴格審核,可以確保不會(huì )發(fā)生全表更新的問(wèn)題。
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()); # 過(guò)濾字段上沒(méi)有索引 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
由此,update 時(shí),在沒(méi)有 where 條件或者where 后不是索引字段時(shí),必須使用 limit ;在有 where 條件時(shí),為索引字段
最近在工作中又發(fā)現了一個(gè)問(wèn)題,mysql sql_safe_updates 不支持子查詢(xún)的更新。
考慮到開(kāi)發(fā)人員有時(shí)候不小心誤更新數據,要求線(xiàn)上庫的 MySQL 實(shí)例都設置 sql_safe_updates=1 來(lái)避免沒(méi)有索引的 update、delete。
結果有一天開(kāi)發(fā)發(fā)現下面的一個(gè)SQL 沒(méi)法正確執行:
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
也就是說(shuō)沒(méi)法對沒(méi)有走到索引的where條件進(jìn)行更新。搜索了下發(fā)現,的確不行。及時(shí) key1 和key2 分別是 t1、t2 的索引[我換成主鍵都不行] 。說(shuō)明是不支持子查詢(xún)的update。
google 了一下發(fā)現人家也問(wèn)過(guò)這個(gè)問(wèn)題。。
http://stackoverflow.com/questions/24314830/query-not-getting-executed-if-supplied-a-nested-sub-query
最后解決方法:
1)修改 session 級別的參數: set sql_safe_updates=0;
執行 update 操作。退出終端。
2)程序處理:先 select col2 from t2 where key2='ABcD'
獲取數據,然后循環(huán)處理結果,并用 update t1 set col2=1 where key1=?
來(lái)批量更新過(guò)。建議還是用程序處理,臨時(shí)修改變量不是長(cháng)久之計。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站