交易出错对table的id索引有影响吗?
Is there some effects on table's id index if error occured in transaction?
这是我在 Yii 1.1 中的主要交易代码:
try{
$transaction=Yii::app()->projectdb->beginTransaction();
foreach($list as $order){
$orderInfo = OrderInfo::model()->findByPk($order['order_id']);
if(empty($orderInfo )){
throw new Exception('Empty order_info');
}
// ...
// save order data into mysql
}
$transaction->commit();
}catch (Exception $e){
$transaction->rollBack();
}
现在,我发现我的 table 订单中缺少 ID,
select id from order where id between 10231 and 10280
# id name
# 10231 name_10231
# 10280 name_10280
# missed 50 data
这与上面的交易代码有关吗?当交易中发生错误时,它会添加 id 的索引吗?
如果你能给我一个明确的答案,非常感谢..
是的,可能与回滚事务有关。如果您在交易期间插入新记录,MySQL 会为该记录保留 ID(增加 AUTO_INCREMENT
计数器,因此不同的进程可能会在该交易期间插入记录而没有 ID 冲突的风险)。如果你回滚事务,这个 ID 将不会被重用。所以如果你:
- 开启交易,
- 插入50条记录,
- 回滚事务,
您最终会在 ID 列中出现空白,因为这 50 个 ID 已保留,但从未提交。
这是我在 Yii 1.1 中的主要交易代码:
try{
$transaction=Yii::app()->projectdb->beginTransaction();
foreach($list as $order){
$orderInfo = OrderInfo::model()->findByPk($order['order_id']);
if(empty($orderInfo )){
throw new Exception('Empty order_info');
}
// ...
// save order data into mysql
}
$transaction->commit();
}catch (Exception $e){
$transaction->rollBack();
}
现在,我发现我的 table 订单中缺少 ID,
select id from order where id between 10231 and 10280
# id name
# 10231 name_10231
# 10280 name_10280
# missed 50 data
这与上面的交易代码有关吗?当交易中发生错误时,它会添加 id 的索引吗?
如果你能给我一个明确的答案,非常感谢..
是的,可能与回滚事务有关。如果您在交易期间插入新记录,MySQL 会为该记录保留 ID(增加 AUTO_INCREMENT
计数器,因此不同的进程可能会在该交易期间插入记录而没有 ID 冲突的风险)。如果你回滚事务,这个 ID 将不会被重用。所以如果你:
- 开启交易,
- 插入50条记录,
- 回滚事务,
您最终会在 ID 列中出现空白,因为这 50 个 ID 已保留,但从未提交。