問題描述
我想從數據庫中刪除大量項目(1M+),我分叉了一個后臺作業來處理這個問題,這樣用戶就不必等待它完成繼續無論他/她在做什么,問題是,當項目被刪除時,應用程序變得沒有響應,所以我想我會一個塊一個塊地處理這些項目,然后休眠幾秒鐘,然后繼續.
I have a large number of items (1M+) that i want to delete from a database, i fork a background job to take care of that, so that the user won't have to wait for it to finish to carry on whatever he/she was doing, the problem is, the app becomes unresponsive while the items are being deleted, so i thought that i would process the items chunk by chunk and sleep for a couple of seconds then carry on.
這是處理刪除的代碼:
// laravel job class
// ...
public function handle()
{
$posts_archive = PostArchive::find(1); // just for the purpose of testing ;)
Post::where('arch_id', $posts_archive->id)->chunk(1000, function ($posts) {
//go through the collection and delete every post.
foreach($posts as $post) {
$post->delete();
}
// throttle
sleep(2);
});
}
預期結果:帖子被分塊并處理每個塊,然后空閑 2 秒,重復此操作,直到所有項目都被刪除.
Expected result: the posts are chunked and each chunk is processed, then idle for 2 seconds, repeat that until all the items are deleted.
實際結果:隨機刪除一次,過程結束.沒有錯誤沒有指標,沒有線索?
Actual result: a random number of items is deleted once, then the process ends. no errors no indicators, no clue ?
有沒有更好的方法來實現這一點?
is there a better way to implement this?
推薦答案
Laravel 并沒有針對您處理此問題的方式進行具體說明.如果作業中的刪除查詢凍結了 UI 的其余部分,聽起來您的數據庫服務器需要審查或優化.
There is nothing Laravel specific about the way you'd handle this. It sounds like your database server needs review or optimization if a delete query in a job is freezing the rest of the UI.
檢索每個模型并單獨運行刪除查詢絕對不是優化此操作的好方法,因為您將執行數百萬個查詢.如果您希望嘗試限制應用程序中的每秒負載而不是優化數據庫服務器來處理此查詢,則可以使用帶有刪除限制的 while 循環:
Retrieving each model and running a delete query individually definitely isn't a good way to optimize this as you'd be executing millions of queries. You could use a while loop with a delete limit if you wish to try to limit the load per second in your application instead of optimizing your database server to handle this query:
do {
$deleted = Post::where('arch_id', $posts_archive->id)->limit(1000)->delete();
sleep(2);
} while ($deleted > 0);
這篇關于Laravel 塊和刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!