`active' flag or not?

后端 未结 18 1772
醉梦人生
醉梦人生 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 15:06

    Yes, we would. We currently have the "active='T/F'" column in many of our tables, mainly to show the 'latest' row. When a new row is inserted, the previous T row is marked F to keep it for audit purposes.

    Now, we're moving to a 2-table approach, when a new row is inserted, the previous row is moved to an history table. This give us better performance for the majority of cases - looking at the current data.

    The cost is slightly more than the old method, previously you had to update and insert, now you have to insert and update (ie instead of inserting a new T row, you modify the existing row with all the new data), so the cost is just that of passing in a whole row of data instead of passing in just the changes. That's hardly going to make any effect.

    The performance benefit is that your main table's index is significantly smaller, and you can optimise your tablespaces better (they won't grow quite so much!)

    0 讨论(0)
  • 2020-12-25 15:07

    Well, to ensure that you only draw active records in most situations, you could create views that only contain the active records. That way it's much easier to not leave out the active part.

    0 讨论(0)
  • 2020-12-25 15:08

    No - this is a pretty common thing - couple of variations depending on specific requirements (but you already covered them):

    1) If you expect to have a whole BUNCH of data - like multiple terabytes or more - not a bad idea to archive deleted records immediately - though you might use a combination approach of marking as deleted then copying to archive tables.

    2) Of course the option to hard delete a record still exists - though us developers tend to be data pack-rats - I suggest that you should look at the business process and decide if there is now any need to even keep the data - if there is - do so... if there isn't - you should probably feel free just to throw the stuff away.....again, according to the specific business scenario.

    0 讨论(0)
  • 2020-12-25 15:12

    Moving off to a separate table and bringing them back up takes time. Depending on how many records go offline and how often you need to bring them back, it might or might not be a good idea.

    If the mostly dont come back once they are buried, and are only used for summaries/reports/whatever, then it will make your main table smaller, queries simpler and probably faster.

    0 讨论(0)
  • 2020-12-25 15:13

    From a 'purist perspective' the realtional model doesn't differentiate between a view and a table - both are relations. So that use of a view that uses the discriminator is perfectly meaningful and valid provided the entities are correctly named e.g. Person/ActivePerson.

    Also, from a 'purist perspective' the table should be named person, not people as the name of the relation reflects a tuple, not the entire set.

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

    Binary flags like this in your schema are a BAD idea. Consider the query

    SELECT count(*) FROM users WHERE active=1

    Looks simple enough. But what happens when you have a large number of users, so many that adding an index to this table would be required. Again, it looks straight forward

    ALTER TABLE users ADD INDEX index_users_on_active (active)

    EXCEPT!! This index is useless because the cardinality on this column is exactly two! Any database query optimiser will ignore this index because of it's low cardinality and do a table scan.

    Before filling up your schema with helpful flags consider how you are going to access that data.

    https://stackoverflow.com/questions/108503/mysql-advisable-number-of-rows

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