不過在今天測試的過程中發現,當listen特定的model的pre_delete事件時,刪除整個QuerySet會造成多一次的SELECT query。翻閱Django的文件後發現有下列的提醒在:
The delete() method does a bulk delete and does not call any delete() methods on your models. It does, however, emit the pre_delete and post_delete signals for all deleted objects (including cascaded deletions).看來是應該要考慮一下是否當用戶刪除資料時也要做對映的log,系統中目前做刪除log的model中,有一個欄位還必需去取被刪除model的外鍵,若是刪除10筆就多10個Query,目前尚無好的解決方式。
Django needs to fetch objects into memory to send signals and handle cascades. However, if there are no cascades and no signals, then Django may take a fast-path and delete objects without fetching into memory. For large deletes this can result in significantly reduced memory usage. The amount of executed queries can be reduced, too.[1]
參看:
沒有留言:
張貼留言