I have come up with an OO-like design of my DB tables, with having a "super table" that contains columns that all my other tables share, each of the "sub tables" using a rowid ptr to the super table.
Like this:
CREATE TABLE 'SuperTable' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created DATETIME
);
CREATE TABLE 'SubTable1' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
super_id INTEGER, -- reference to SuperTable
additionalData TEXT
);
CREATE TABLE 'SubTable2' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
super_id INTEGER, -- reference to SuperTable
moreData BLOB
);
Now, I like to make query over all sub tables, giving me a row per entry in the SuperTable, with their linked data in the respective sub table.
I've come up with this:
SELECT * FROM SuperTable
LEFT OUTER JOIN SubTable1 ON SubTable1.super_id = SuperTable.id
LEFT OUTER JOIN SubTable2 ON SubTable2.super_id = SuperTable.id
WHERE
SubTable1.super_id IS NOT NULL OR
SubTable2.super_id IS NOT NULL
I saw that without the WHERE part, I would get quite a few permutations with both sub tables being NULL. Now I wonder what happens if I add even more Sub Tables, thereby causing a lot more permutations. Won't that become rather inefficient (memory-wise and speed-wise) with the query I use above?
So I wonder if there is a better way to select these items. Maybe with the UNION operator? I am using sqlite at the moment, but a more generic answer would be appreciated as well.
It's well possible that this answer has been answered numerous times already, but I find it hard to come up with a good search phrase for this.
Aucun commentaire:
Enregistrer un commentaire