I realize that the answer here is probably some form of "it depends"; but I'm still wondering what (if any) the general rule of thumb is here.
If I want to enable "soft-deleting" records of a certain type, I might add a deleted
column so I can do something like this (providing a Rails example, but I don't mean for the question to be Rails-specific):
class SomeModel < ActiveRecord::Base
default_scope { where(:deleted => false) }
end
In this case I would imagine that indexing the deleted
column would be relatively cheap as there are only two possible values: true
or false
.
Of course it might be useful to know when a record was deleted; so I could go with deleted_at
instead:
class SomeModel < ActiveRecord::Base
default_scope { where(:deleted_at => nil) }
end
I've definitely seen this sort of thing quite a bit in the wild. My question is whether the indexing is actually much more expensive in this case, since basically every record will have a different value for deleted_at
*. I know almost zero about database internals (something I definitely would like to read up on some day); so I recognize that this is probably a naive question.
And I know it probably depends on the database as well; if it matters, I'm mostly concerned with PostgreSQL. But I would imagine there are others who would also like to know about MySQL, Oracle, etc.
So? Little help?
* As I typed that sentence I think I realized the answer, since obviously every record has a different value for id
as well but you don't think twice about indexing that. So probably date/time values work the same way? Anyway, I'd prefer an answer from someone with real knowledge on the subject rather than my hunch :)