I have to track values for a combination of different resources everyday. So a table to do this might look like:
CREATE TABLE `data` (
`id` INT UNSIGNED NULL PRIMARY KEY AUTO_INCREMENT,
`datetime` DATETIME NOT NULL,
`res1` INT UNSIGNED NOT NULL,
`res2` INT UNSIGNED NOT NULL,
`res3` INT UNSIGNED NOT NULL,
`res4` INT UNSIGNED NOT NULL,
`res5` INT UNSIGNED NOT NULL,
`value` DECIMAL(10,0) NOT NULL,
UNIQUE INDEX `datetime_res1_to_res5` (`datetime`, `res1`, `res2`, `res3`, `res4`, `res5`)
)
where res1
through to res5
are foreign keys to their respective tables.
This table will contains a lot of rows--will easily crack 20 million.
What I'm curious of is if I should put the combination of foreign keys into a separate table, such that I have two tables like so:
CREATE TABLE `data` (
`id` INT UNSIGNED NULL PRIMARY KEY AUTO_INCREMENT,
`datetime` DATETIME NOT NULL,
`superKeys_id` INT UNSIGNED NOT NULL,
`value` DECIMAL(10,0) NOT NULL,
UNIQUE INDEX `datetime_superKeys_id` (`datetime`, `superKeys_id`)
)
CREATE TABLE `superKeys` (
`id` INT UNSIGNED NULL PRIMARY KEY AUTO_INCREMENT,
`res1` INT UNSIGNED NOT NULL,
`res2` INT UNSIGNED NOT NULL,
`res3` INT UNSIGNED NOT NULL,
`res4` INT UNSIGNED NOT NULL,
`res5` INT UNSIGNED NOT NULL,
UNIQUE INDEX `res1_to_res5` (`res1`, `res2`, `res3`, `res4`, `res5`)
)
where data
.superKeys_id
is a foreign key to superKeys
.id
.
This will significantly reduce the size of the table. But I'm not sure if it might be a bad idea for reasons I do not know. Obviously selects will require a join to get a breakdown of data, which would add little more overhead, but I shouldn't think this would be a problem.
In my real world situation, one of the resources would be user_id and I would frequently need to sum the values for a user, so I would probably keep such a column in data
rather than make it a part of the superKeys
table for the sake of not having to join on every query. Then only use a join when I need to sum values for other resources which will be less often.