`active' flag or not?

后端 未结 18 1771
醉梦人生
醉梦人生 2020-12-25 14:21

OK, so practically every database based application has to deal with \"non-active\" records. Either, soft-deletions or marking something as \"to be ignored\". I\'m curious a

相关标签:
18条回答
  • 2020-12-25 14:48

    I think looking at it strictly as a piece of data then the way that is shown in the original post is proper. The active flag piece of data is directly dependent upon the primary key and should be in the table.

    That table holds data on people, irrespective of the current status of their data.

    0 讨论(0)
  • 2020-12-25 14:50

    We use an enum('ACTIVE','INACTIVE','DELETED') in most tables so we actually have a 3-way flag. I find it works well for us in different situations. Your mileage may vary.

    0 讨论(0)
  • 2020-12-25 14:50

    Moving inactive stuff is usually a stupid idea. It's a lot of overhead with lots of potential for bugs, everything becomes more complicated, like unarchiving the stuff etc. What do you do with related data? If you move all that, too, you have to modify every single query. If you don't move it, what advantage were you hoping to get?

    That leads to the next point: WHY would you move it? A properly indexed table requires one additional lookup when the size doubles. Any performance improvement is bound to be negligible. And why would you even think about it until the distant future time when you actually have performance problems?

    0 讨论(0)
  • 2020-12-25 14:50

    This is an old question but for those search for low cardinality/selectivity indexes, I'd like to propose the following approach that avoids partitioning, secondary tables, etc.:

    The trick is to use "dateInactivated" column that stores the timestamp of when the record is inactivated/deleted. As the name implies, the value is NULL while the record is active, but once inactivated, write in the system datetime. Thus, an index on that column ends up having high selectivity as the number of "deleted" records grows since each record will have a unique (not strictly speaking) value.

    Then your query becomes:

    SELECT * FROM people WHERE dateInactivated is NULL;
    

    The index will pull in just the right set of rows that you care about.

    0 讨论(0)
  • 2020-12-25 14:50

    Filtering data on a bit flag for big tables is not really good in terms of performance. In case when 'active' determinate virtual deletion you can create 'TableName_delted' table with the same structure and move deleted data there using delete trigger.

    That solution will help with performance and simplifies data queries.

    0 讨论(0)
  • 2020-12-25 14:55

    You partition the table on the active flag, so that active records are in one partition, and inactive records are in the other partition. Then you create an active view for each table which automatically has the active filter on it. The database query engine automatically restricts the query to the partition that has the active records in it, which is much faster than even using an index on that flag.

    Here is an example of how to create a partitioned table in Oracle. Oracle doesn't have boolean column types, so I've modified your table structure for Oracle purposes.

    CREATE TABLE people
    (
       id       NUMBER(10),
       name     VARCHAR2(100),
       active   NUMBER(1)
    )
    PARTITION BY LIST(active)
    (
       PARTITION active_records VALUES (0)
       PARTITION inactive_records VALUES (1)
    );
    

    If you wanted to you could put each partition in different tablespaces. You can also partition your indexes as well.

    Incidentally, this seems a repeat of this question, as a newbie I need to ask, what's the procedure on dealing with unintended duplicates?

    Edit: As requested in comments, provided an example for creating a partitioned table in Oracle

    0 讨论(0)
提交回复
热议问题