简体   繁体   中英

How can I improve performance of soft-deletion in django?

I have a moderately large set of models (about 350,000) backed by a MySQL InnoDB table. I'm currently using a soft delete method similar to this one that stores a deleted attribute in the database.

I have a view that shows the count of these items, and I have found that filtering the soft-deleted items is very slow.

Item.objects.filter(deleted=False).count() -> ~400ms

If I don't exclude the deleted objects, the count is relatively fast.

Item.objects.all().count() -> ~140ms

Strangely, if I add an index on the deleted column in the db, the time increases.

Item.objects.filter(deleted=False).count() -> ~450ms # indexed on 'deleted'

What alternatives to this method of soft-deletion might I consider that doesn't make my app 3x slower?

将删除的项目存储在单独的表中?

Copied from another discussion.

"Queries become very complex" - Use views instead of tables when selecting. "Queries become slower over time" - Use filtered indexes, instead of the regular indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM