MySQL Create a function table?
I am trying to design the layout of the table to work best in the following situation.
I have a product that is sold based on age. The age determines if that product exists for this person and the minimum and maximum one can buy. Right now i have designed the table as follows:
CREATE TABLE `tblProductsVsAge` (
`id` int(255) AUTO_INCREMENT NOT NULL,
`product_id` bigint(255) NOT NULL,
`age_min` int(255) NOT NULL,
`age_max` int(255) NOT NULL,
`quantity_min` decimal(8) NOT NULL,
`quantity_max` decimal(8) NOT NULL,
/* Keys */
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
this开发者_开发技巧 is functional and it work, but i feel as if its not the best optimized structure. any idea?
i forgot to mention a product can have many ranges. for example age min 25 age max 35 and the quantity for this would be 12 and 28, for the same product ID we might have age 36 to 60, quantity from 3 to 8.
- Use
tinyint unsigned
for age_max and age_min since none of the ages in the question pass 255 (highest unsigned tinyint). - Use
smallint unsigned
for quantity_max and quantity_min if those values > 255 and <= 65535 (highest unsigned smallint). - Use
mediumint unsigned
for quantity_max and quantity_min if those values > 65535 and <= 16777215 (highest unsigned mediumint). - Use
int unsigned
for quantity_max and quantity_min if those values > 16777215 and <= 4294967295 (highest unsigned int). (Sometimes, you gotta Think Big !!!)
My recommendation:
CREATE TABLE `tblProductsVsAge` (
`product_id` int NOT NULL,
`age_min` tinyint unsigned NOT NULL,
`age_max` tinyint unsigned NOT NULL,
`quantity_min` smallint unsigned NOT NULL,
`quantity_max` smallint unsigned NOT NULL,
/* Keys */
PRIMARY KEY (`product_id`, `age_min`)
) ENGINE = InnoDB;
Here is something to consider if the table already has data: You could ask mysql to recommend column defintions for this table.
Simply run this query:
SELECT * FROM tblProductsVsAge PROCEDURE ANALYSE();
The directive PROCEDURE ANALYSE() will cause mysql not to display the data but to examine the values from each column and come up with its own recommendation. Sometimes, the recommendation is too granular. For example, if age_min is in the teenage range, it may recommend ENUM('13','14','15','16','17',18','19') instead of tinyint. After PROCEDURE ANALYSE() is done, you still make the final call on the column definitions.
CREATE TABLE `tblProductsVsAge` (
`product_id` int NOT NULL,
`age_min` smallint NOT NULL,
`age_max` smallint NOT NULL,
`quantity_min` smallint NOT NULL,
`quantity_max` smallint NOT NULL,
/* Keys */
PRIMARY KEY (`product_id`, `age_min`)
) ENGINE = InnoDB;
Changes to your structure:
id
is probably not needed (unless you really need it), but then if you needproduct_id
to bebigint
thenid
should have the same type - after all this table can get more rows than your products table,- I changed type od
product_id
toint
, I don't think you will have more than 2147483647 products, age
andquantity
aresmallint
s, which can have a maximum value of 32767 (usemediumint
orint
if it's not enough).decimal
is intended for when you need exact precision or numbers bigger thanbigint
,- index on
(id, age_min)
to make faster searches for givenproduct_id
and for searches likeproduct_id = {some_id} AND min_age > {user_age}
(255)
in int
/bigint
definition doesn't make it 255 digits long - it's only a hint for string representation.
MySQL manual on numeric types: http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html
精彩评论