SQL - column choice

Status
Not open for further replies.
Simple 'issue' here, but my OCD is causing me conflicts as to the the best choice...

I have a simple table that gets new records inserted when email replies come into a system. It simply records a timestamp and the database record to which the reply applies.

There is a requirement to add functionality to highlight that there is a new/unacknowledged email (which gets acknowledged when the record to which the email relates is view by a user).

Now, I'm thinking a simple, new bit value against the record to record its "new" status which gets cleared when the parent record is viewed.

But then the little voice kicks in and says that it's a waste of a new field (even though its only a bit) since once acknowledged I'm still storing 0's against every record for no purpose whatsoever - I could simply record the parent id as a negative value initially and update to positive when acknowledged, thereby saving the "expense" of adding a new field that provides no real benefit after the fact.

Another voice chimes in to say simply create a second table that purely holds the id's of records that are "new" and delete the record when its acknowledged, thereby no waste of data recorded.

What do I go with:
1. New field to hold the "new" status.
2. Use the existing recordid field but record new records temporarily as negative values.
3. New table to temporarily hold the new status of records.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Don't repurpose fields to mean other things. It's a long term nightmare.

Don't delete when you can add instead, have a separate FK backed table indicating 'viewed' this can include who viewed it and when. Doing a view of "replies without any views" is trivial. You can easily make this filtered to exclude the older ones.

Disk is cheap, stop caring about it. A decent db engine, with a reasonable index structure will work just fine for this
 

Jehos

Ars Legatus Legionis
55,560
A bit field takes all of one byte per record if you have no other bit fields in the table. If you do, its zero extra storage unless it's the ninth bit field, in which case it adds another byte.

You're literally trying to optimize storage to prevent "wasting" one megabyte per million rows in your table. Stop that.

The only important question is whether you already have a table linking users to e-mails where that bit flag can live, since "unread status" is per user per email.
 

metadaemon

Wise, Aged Ars Veteran
121
You would be surprised how often these little features can spawn more requirements.

If a record's handled status is a necessary attribute of that record, add columns to the table. Otherwise, create a queue table to record it's handling.

It sounds to me like a new table is the way to go in your case. A note on the column type. Bits are extremely stiff. Integers are less stiff but somewhat non-obvious when you use a number to mean something else. Anytime I have needed to describe a record's workflow state (new,answered), I have used text.
 

hestermofet

Ars Legatus Legionis
25,464
Bits are extremely stiff. Integers are less stiff but somewhat non-obvious when you use a number to mean something else. Anytime I have needed to describe a record's workflow state (new,answered), I have used text.

You could use a lookup table for your status codes so that it becomes self-documenting. I guess the "correct" way to deal with this is to explain the numbers in your data dictionary that's in the documentation that goes along with your project. Text is just a bit messy to deal with programmatically.
 

Jehos

Ars Legatus Legionis
55,560
You would be surprised how often these little features can spawn more requirements.

If a record's handled status is a necessary attribute of that record, add columns to the table. Otherwise, create a queue table to record it's handling.

It sounds to me like a new table is the way to go in your case. A note on the column type. Bits are extremely stiff. Integers are less stiff but somewhat non-obvious when you use a number to mean something else. Anytime I have needed to describe a record's workflow state (new,answered), I have used text.
That gets slow with millions of records. Bits join and filter incredibly quickly.
 
Bits are extremely stiff. Integers are less stiff but somewhat non-obvious when you use a number to mean something else. Anytime I have needed to describe a record's workflow state (new,answered), I have used text.

You could use a lookup table for your status codes so that it becomes self-documenting. I guess the "correct" way to deal with this is to explain the numbers in your data dictionary that's in the documentation that goes along with your project. Text is just a bit messy to deal with programmatically.
Text isn't just messy programmatically speaking - there's a high risk of inconsistency, undermining the value.

The former is the proper way - use a foreign key to associate to a code/lookup/xref table that only immediately needs a Status table, which has a numeric primary key column and a StatusDescription column to explain the value. Depending on the number of values and the database vendor, there's numeric options like TINYINT and SMALLINT (MySQL, SQL Server) before going to INT in order to save space. And changing the data type (unlikely in this scenario but...) from TINYINT to SMALLINT/etc doesn't require data conversion.

Indexing the foreign key column depends on cardinality - if there's currently only two values/options, an index won't provide much if any value. But several values, with disparate percentages...

Drive space is cheap - a couple hundred $ will get you 1+ TB.
 

Drizzt321

Ars Legatus Legionis
34,920
Subscriptor++
Don't repurpose fields to mean other things. It's a long term nightmare.

+++++++++

This is a constant challenge when maintaining anything of reasonable complexity and changing business needs/requirements. Generally we try and go with "add new stuff" rather than "repurpose/extend". We've gotten bit occasionally with remembering that we added this other meaning to this field when this other thing is in this condition, but it's the normal meaning every other case. *shudder*
 

Ardax

Ars Legatus Legionis
19,794
Subscriptor
Or you'd be unable to use the negative value because then the FK wouldn't match up.


Indexing the foreign key column depends on cardinality - if there's currently only two values/options, an index won't provide much if any value. But several values, with disparate percentages...
Slightly unrelated: Is there a point in creating the index if you don't do any querying by that value?
 

fitten

Ars Legatus Legionis
55,632
Subscriptor++
Or you'd be unable to use the negative value because then the FK wouldn't match up.


Indexing the foreign key column depends on cardinality - if there's currently only two values/options, an index won't provide much if any value. But several values, with disparate percentages...
Slightly unrelated: Is there a point in creating the index if you don't do any querying by that value?

Depends on the type of index. If you don't use it as filtering criteria but do return the column in queries, including that column as an "included" column on an index (that's indexing other columns that are used in filter criteria) can make the queries faster, even if you aren't actually indexing on that column itself. Of course, this has its limits and is mostly useful if your included columns 'cover' your query (all the columns returned in the select for specific, frequently used queries). That is, of course, if your database supports that feature.
 

Drizzt321

Ars Legatus Legionis
34,920
Subscriptor++
Or you'd be unable to use the negative value because then the FK wouldn't match up.


Indexing the foreign key column depends on cardinality - if there's currently only two values/options, an index won't provide much if any value. But several values, with disparate percentages...
Slightly unrelated: Is there a point in creating the index if you don't do any querying by that value?

Depends on the type of index. If you don't use it as filtering criteria but do return the column in queries, including that column as an "included" column on an index (that's indexing other columns that are used in filter criteria) can make the queries faster, even if you aren't actually indexing on that column itself. Of course, this has its limits and is mostly useful if your included columns 'cover' your query (all the columns returned in the select for specific, frequently used queries). That is, of course, if your database supports that feature.

Or if you want that column to only ever have unique values, but not as part of the PKey (or using an auto-inc ID as the PKey), make it a Unique Key to enforce that DB side* rather than just application side.


*As has been mentioned (complained, discussed, etc) elsewhere in this sub-fora, NULL in a Unique Key can have multiple rows with NULL, as NULL means having no value, so you can't compare 1 NULL to another NULL and have a Unique Key collision. Which I think is a PITA, but I understand the reasoning behind it, but I still don't like the practical implications.
 

Ardax

Ars Legatus Legionis
19,794
Subscriptor
Which I think is a PITA, but I understand the reasoning behind it, but I still don't like the practical implications.
I've got cases where it's a good thing rather than bad. Yes, some version of beyond 3NF would indicate that field should be in its own table because NULL is evil or something.

Depends on the type of index. If you don't use it as filtering criteria but do return the column in queries, including that column as an "included" column on an index (that's indexing other columns that are used in filter criteria) can make the queries faster, even if you aren't actually indexing on that column itself.
Right, and that gets farther into The Fine Art of Indexing rather than it being indexed Because Reasons. Entity Framework likes to put an index on every FK relationship and I was wondering if there was a point to it. Apparently not.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
. Entity Framework likes to put an index on every FK relationship and I was wondering if there was a point to it. Apparently not.

Surely an index is needed in almost all cases to allow the database to maintain the Key part of that relationship efficiently (on inserts to the child table, or deletes from the parent). There might be occasions where you could design it knowing it would do full table scan, (or fallback to another index that contained it perhaps), but it seems like a bad idea except in very specialist cases (knowing the tables are tiny and Big-O behaviour doesn't matter) so it being the default in a tool is not surprising.
 
Is there a point in creating the index if you don't do any querying by that value?
As others mentioned, the column being in the SELECT list could leverage an index if one exists.

Foreign keys are the predominant JOIN criteria (WHERE if you *shudder* still use ANSI-89 syntax), and a foreign key constraint only ensures referential integrity (that a value in the column is one of the defined values). Without an index, the JOIN will be doing a table scan (compared to an index lookup).

Indexes (or constraints of any kind) don't come for free. These impact record insertion speed because values need to be inserted/shuffled/etc (validated for constraints), and require maintenance. Most tables are constructed with data retrieval in mind, but logging tables (keeping a log of activity) benefit from having the least restriction (only a primary key - heap tables surprisingly don't come out ahead).
 
Status
Not open for further replies.