Skip to content

数据表通用字段 delete_time 导致的性能问题 #129

@colorful3

Description

@colorful3

对于数据的软删除,Lin CMS是通过delete_time字段是否为null来判断的。因此所有的查询语句,都会拼接上一个 where 条件delete_time is null。这种做法会引发很诸多性能问题:
因为delete_time默认值为null,可以为 null ,会导致查询效率大打折扣,explain会走全表扫描:
image

推荐增加 is_deleted 字段来做删除标识,这种是比较常用的逻辑删除做法,查询性能上至少比现在的做法要好:

is_deleted int(1)  NOT NULL DEFAULT 0 COMMENT '0:正常 1:已删除'

参考链接:
性能优化案例分析之一:软删除是慢查询的罪魁祸首?
小心 MySQL Soft Delete
为什么 MySQL 不推荐默认值为 null ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions