MySQL 更新失效

2022-05-01 11:51:08 字數 2280 閱讀 1575

create table t(id int not null primary key,c int default null) engine=innodb;

insert into t(id,c)values(1,1),(2,2),(3,3),(4,4);

session1session2

([email protected]:3306) [test]> show variables like '%iso%';

+---------------+-----------------+

| variable_name | value |

+---------------+-----------------+

| tx_isolation |repeatable-read|

+---------------+-----------------+

([email protected]:3306) [test]> show variables like 'auto%';

+--------------------------+-------+

| variable_name | value |

+--------------------------+-------+

| auto_increment_increment | 1 |

| auto_increment_offset | 1 |

|autocommit|on|

| automatic_sp_privileges | on |

+--------------------------+-------+

([email protected]:3306) [test]> begin;

query ok, 0 rows affected (0.00 sec)

([email protected]:3306) [test]> select * from t;

+----+------+

| id | c |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

+----+------+

4 rows in set (0.00 sec)

session2: ([email protected]:3306) [test]> update t set c=id+1;

([email protected]:3306) [test]> update t set c=0 where id=c;

query ok, 0 rows affected (4.39 sec)

rows matched: 0 changed: 0 warnings: 0

//session1更新失效,update是當前讀,此時已經找不到id=c的值,所以更新失敗

//rc情況,update還是會更新失效,跟隔離級別無關,都是當前讀,只不過rc情況,下面的select就能讀到session2的正確的值

([email protected]:3306) [test]> select * from t;

+----+------+

| id | c |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

+----+------+

4 rows in set (0.00 sec)

([email protected]:3306) [test]> commit;

query ok, 0 rows affected (0.00 sec)

([email protected]:3306) [test]> select * from t;

+----+------+

| id | c |

+----+------+

| 1 | 2 |

| 2 | 3 |

| 3 | 4 |

| 4 | 5 |

+----+------+

4 rows in set (0.00 sec)

mysql索引失效 常見mysql索引失效條件

使用索引的一般語句 1 where條件中有or,除非or的所有欄位都有索引,只要有乙個沒有索引,就不走索引 explain select from jf user ju where ju.user id or ju.superior1 yyy user id是主鍵,superior1是普通索引,結果...

mysql 主鍵失效 MySQL索引(索引失效)

索引 索引也是一張表,該錶儲存了主鍵與索引字段,並指向實體表的記錄。myisam儲存引擎,資料檔案 索引檔案 表結構檔案分開儲存 innodb儲存引擎,資料和索引儲存在乙個檔案中 b tree索引 hash索引 hash索引 只有memory儲存引擎支援 查詢一條記錄的速度非常快 b tree索引 ...

mysql 索引失效場景 Mysql 索引失效場景

例如 一張user表 有欄位屬性 name,age 其中name為索引 下面列舉幾個索引失效的情況 1.select from user where name xzz or age 16 例如這種情況 當語句中帶有or的時候 即使有索引也會失效。2.select from user where na...