mercredi 2 septembre 2015

Is a text column with a small number of distinct values a bad smell?

For example, let's say I have a table like this:

CREATE TABLE people (
    firstname TEXT,
    lastname TEXT,
    weight FLOAT,
    zodiac_sign TEXT    
);

The first three columns are going to have many distinct values, and the number of them will grow without bound as I add more rows. But zodiac_sign will always be one of 12 values.

I'm assuming that SQLite will use 11 bytes for every instance of 'sagittarius' (i.e. that it's not smart enough to infer that zodiac_sign is basically an enum that can be stored in a single byte).

Does this suggest that, if the number of rows I'll be dealing with is non-trivial, I should split off another table like this:

CREATE TABLE people (
    firstname TEXT,
    lastname TEXT,
    weight FLOAT,
    zodiac_id INTEGER NOT NULL REFERENCES zodiac_signs(zodiac_id)
);

CREATE TABLE zodiac_signs (
    zodiac_id INTEGER PRIMARY KEY,
    name TEXT
);

And would this still be a good practice for a text column that holds a small number of distinct values but which isn't constrained to some set of values that will never change? e.g. if I had a column for country of birth.

Aucun commentaire:

Enregistrer un commentaire