MySQL系統變量sql_safe_updates總結mysql
在MySQL中,系統變量sql_safe_updates是個很是有意思的系統變量,在Oracle和SQL Server中都沒有見過這樣的參數或功能。若是這個系統變量設置爲1的話,意味着update與delete將會受到限制。我的臆測,之因此提供這個功能,一方面是要避免出現更新或刪除數據時,忘記添加WHERE條件,致使數據被誤更新或誤刪的狀況。相信很多人都因爲疏忽或大意,遇到過這種狀況; 另一方面也是爲了提升SQL性能考慮,避免DELETE與UPDATE語句的WHERE條件不走索引的狀況。默認狀況下,系統變量sql_safe_updates是禁用的。咱們下面來簡單測試、瞭解一下這個系統變量的細節問題sql
系統變量sql_safe_updates分會話級別和全局級別。能夠動態修改。默認是關閉(值爲0)。具體參考官方文檔。session
系統變量的查看app
注意事項1:show variables優先顯示會話級變量的系統變量值,若是這個系統變量沒有會話級別的值,則顯示全局級系統變量的值,固然最清晰的方式就是加上GLOBAL或SESSION關鍵字來區別。ide
注意事項2:@@GLOBAL.var_name、@@SESSION.var_name 、@@LOCAL.var_name 。若是在變量名前沒有級別限定符,將優先顯示會話級的值。性能
1:查看會話級別系統變量sql_safe_updates的值測試
mysql> show variables like 'sql_safe_updates';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| sql_safe_updates | ON |
+------------------+-------+
1 row in set (0.01 sec)
mysql> show session variables like 'sql_safe_updates';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| sql_safe_updates | ON |
+------------------+-------+
1 row in set (0.00 sec)
mysql>
mysql> select @@session.sql_safe_updates;
+----------------------------+
| @@session.sql_safe_updates |
+----------------------------+
| 1 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select @@sql_safe_updates;
+--------------------+
| @@sql_safe_updates |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)
2:查看全局級別系統變量sql_safe_updates的值spa
mysql> show global variables like 'sql_safe_updates';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| sql_safe_updates | OFF |
+------------------+-------+
1 row in set (0.00 sec)
mysql> select @@global.sql_safe_updates;
+---------------------------+
| @@global.sql_safe_updates |
+---------------------------+
| 0 |
+---------------------------+
1 row in set (0.00 sec)
系統變量sql_safe_updates的設置code
#設置會話級別的系統變量orm
mysql> set sql_safe_updates=1;
Query OK, 0 rows affected (0.00 sec)
#設置全局級別的系統變量
mysql> set global sql_safe_updates=1;
Query OK, 0 rows affected (0.00 sec)
若是要設置全局級別的系統變量sql_safe_updates,最好在配置文件my.cnf中設置,這樣就能避免MySQL重啓後,SQL命令設置的sql_safe_updates系統全局變量失效。
實驗測試案例
UPDATE語句測試
1: 沒有WHERE條件。
mysql> update shop set price=price*1.5;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
2: 沒有WHERE條件,可是有LIMTI限制條件
以下測試所示,沒有WHERE條件,可是有LIMIT限制的話,SQL也是能夠執行的。
mysql> update shop set price=price*1.5 limit 5;
Query OK, 5 rows affected (0.05 sec)
Rows matched: 5 Changed: 5 Warnings: 0
3: 有WHERE條件,可是WHERE條件的列沒有索引可用
mysql> show index from shop\G;
*************************** 1. row ***************************
Table: shop
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: article
Collation: A
Cardinality: 931203
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: shop
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 2
Column_name: dealer
Collation: A
Cardinality: 931203
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: shop
Non_unique: 1
Key_name: shop__price
Seq_in_index: 1
Column_name: price
Collation: A
Cardinality: 1877
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.00 sec)
ERROR:
No query specified
mysql> update shop set price = price * 1.5 where dealer='K';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
4: 有WHERE條件,可是WHERE條件列沒有索引 + LIMIT限制
mysql> update shop set price = price * 1.5 where dealer='K' limit 1000;
Query OK, 1000 rows affected (0.13 sec)
Rows matched: 1000 Changed: 1000 Warnings: 0
5:有WHERE條件的UPDATE,並且字段有索引
mysql> update shop set price = price * 1.5 where article=6156;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
6:有WHERE條件,可是WHERE條件爲常量
mysql> update shop set price = price * 1.5 where 1=1;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
7:有WHERE條件,可是WHERE條件爲常量+LIMIT
mysql> update shop set price = price * 1.5 where 1=1 limit 10;
Query OK, 10 rows affected (0.00 sec)
Rows matched: 10 Changed: 10 Warnings: 0
DELETE語句測試
1: DELETE語句沒有WHERE條件
mysql> delete from shop;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
2: DELETE語句沒有WHERE條件+ LIMIT
mysql> delete from shop limit 10;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
3:DELETE語句有WHERE條件,並且查詢條件的字段有索引
mysql> delete from shop where article =6156;
Query OK, 1 row affected (0.01 sec)
mysql>
4:DELETE語句有WHERE條件,並且查詢條件的字段有索引 +LIMIT
mysql> delete from shop where article< 2000 limit 1000;
Query OK, 1000 rows affected (0.02 sec)
mysql>
5:DELETE語句有WHERE條件,可是查詢條件的字段沒有索引
mysql> delete from shop where dealer='K';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
6:DELETE語句有WHERE條件,可是查詢字段沒有索引 +LIMIT語句
mysql> delete from shop where dealer='K' limit 20;
Query OK, 20 rows affected (0.05 sec)
mysql>
7:DELETE語句有WHERE條件,可是WHERE條件爲常量
mysql> delete from shop where 1=1;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
8:DELETE語句有WHERE條件,可是WHERE條件爲常量 +LIMIT
mysql> delete from shop where 1=1 limit 10;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
mysql>
如上測試的實驗結果,總結出來以下所示.至因而否要開啓這個系統變量,須要結合實際狀況,而且和開發人員、項目管理人員一塊兒協商才行。
操做 |
Delete |
Update |
NO WHERE |
No |
No |
NO WHERE + LIMIT |
No |
Yes |
WHERE KEY |
Yes |
Yes |
WHERE KEY + LIMIT |
Yes |
Yes |
WHERE NOKEY |
No |
No |
WHERE NOKEY+ LIMIT |
Yes |
Yes |
WHERE CONSTANT |
No |
No |
WHERE CONSTANT + LIMIT |
No |
Yes |