Toxi mysql 性能、标记。帮助理解
我正在使用 Toxi 方案来标记我网站上的项目。作为 mysql 的新手,更不用说标记了,我只是做一个健全性检查。这是我的表初始化脚本。
CREATE TABLE IF NOT EXISTS Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
item_name VARCHAR(45) NULL ,
media_type VARCHAR(20) NULL ,
file VARCHAR(45) NULL ,
description VARCHAR(500) NULL ,
PRIMARY KEY (item_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
tag_text VARCHAR(25) NOT NULL ,
PRIMARY KEY (tag_id) ,
UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Item2Tag (
item_id INT UNSIGNED NOT NULL ,
tag_id INT UNSIGNED NOT NULL ,
PRIMARY KEY (item_id, tag_id) ,
INDEX (tag_id) ,
FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id) ,
FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;
http://forge.mysql.com/wiki/TagSchema
问题1
我的理解是否正确“Item2Tag”表中是否存在每个“item_id”到“tag_id”的条目?当我有大约 3000 个项目并且每个项目可以有大约 5 个标签时,这似乎将是一个巨大的表。这不是一个问题/不是一个很大的桌子吗?
问题 2
有人可以帮助我了解拥有外键/引用的重要性吗?为什么我需要这些以及它们有什么作用?
I am using the Toxi scheme for tagging items on my website. Being quite new to mysql let alone tagging, I'm just doing a sanity check. Here is my table initialization script.
CREATE TABLE IF NOT EXISTS Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
item_name VARCHAR(45) NULL ,
media_type VARCHAR(20) NULL ,
file VARCHAR(45) NULL ,
description VARCHAR(500) NULL ,
PRIMARY KEY (item_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
tag_text VARCHAR(25) NOT NULL ,
PRIMARY KEY (tag_id) ,
UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Item2Tag (
item_id INT UNSIGNED NOT NULL ,
tag_id INT UNSIGNED NOT NULL ,
PRIMARY KEY (item_id, tag_id) ,
INDEX (tag_id) ,
FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id) ,
FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;
http://forge.mysql.com/wiki/TagSchema
Question 1
Is my understanding correct that there is an entry in the "Item2Tag" table for every "item_id" to "tag_id"? It just seems like that is going to be a huge table when I have ~3000 items and each item could have ~5 tags. Is that not a concern/not really a big table?
Question 2
Could someone help me understand importance of having Foreign Keys/References? Why do I need those and what do they do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题 1:是的,没错。
问题 2:据我所知,Toxi 模式实际上并不需要它们。但它们可以帮助您避免在参考表中包含条目而在项目表中不包含条目。这更多地是预防头痛的一种限制,而不是一种需要。 IE。您删除项目编号 x 时,与项目编号 x 关联的条目也会被删除。
Question 1: Yes, that's correct.
Question 2: You don't really need them for Toxi schema as far as I know. But they help you avoid having entries in the reference table while not in the item table. It's more of a constrain to prevent headaches than a need. ie. You delete item number x, the entry associated to item number x also gets deleted.