从Laravel7中的Mysql表中删除重复记录
我有一个表名temp从Laravel7中的Mysql表中删除重复记录,mysql,laravel-7,Mysql,Laravel 7,我有一个表名temp id,brand,message,created_atupdated_at 随着时间的推移,它开始变得巨大,拥有10万张唱片,但其中大部分都是field brand的重复唱片 我写了一个bellow语句,它适用于少量的记录,但对于大型记录,它会被卡住 $sql=" delete from temp using temp, temp e1 where temp.id > e1.id and temp.brand = e1.brand and
id,brand,message,created_atupdated_at
随着时间的推移,它开始变得巨大,拥有10万张唱片,但其中大部分都是field brand的重复唱片
我写了一个bellow语句,它适用于少量的记录,但对于大型记录,它会被卡住
$sql="
delete
from temp using temp,
temp e1
where temp.id > e1.id
and temp.brand = e1.brand
and temp.message IS NULL
";
DB::unprepared($sql);
我想做的是
1.删除具有相同品牌名称的重复行,但仅当消息为空时才删除。我这样做了,速度快,工作正常
$result = DB::table('temp')
->select('id','brand')
->where('message', NULL)
->get();
$ids=array();
foreach($result->unique('brand') as $brand){
$ids[]=$brand->id;
}
if(count($ids)>0){
foreach(array_chunk($ids, 500) as $chunk) {
$sql="
delete
from temp where id NOT IN ('".implode("','",$chunk)."')";
DB::unprepared($sql);
}
}
您的查询将删除所有没有消息的行,因此不需要自连接。我还构建了自己的查询,以查看我是否正确理解您,而J保留了一个没有消息的行。请进行任何更新?我将给出我的答案,但当您删除所有没有消息的行时,不需要comlex删除,请参阅链接