关于“tag标签”的实现问题(顺请foolbirdflyfirst兄接分)
搜寻了很久tag的解决方案,一直没有一种最好的办法。
今日和foolbirdflyfirst兄探讨了一下,以下两种方法可能是能想到的最好的,请大家提提意见,谢谢。
===============================================================
方法一:
create table article
(
ID int...
tag varchar(255),
index (tag)
);
create table tag
(
ID int...
tag varchar(10),
primary key (ID),
unique (tag)
)
文章表中的tag字段用来存放tag的ID的列表,用“,”分隔,例如“1,2,3”。
===============================================================
方法二:
create table article
(
ID int...
);
create table tag
(
ID int...
tag varchar(10),
primary key (ID),
unique (tag)
)
create table tag_relation
(
ID int...
ArticleID int,
TagID int,
INDEX (ArticleID),
INDEX (TagID)
);
tag关系表中,ArticleID与TagID是1对n的关系,既如果1篇文章有3个tag,那么表中就有3条数据。
=================================================================
方法1的优势在于节省了大量的空间,但是按tag搜索的时候只能用FIND_IN_SET(),这样索引就不能生效了,即使加上”force index”也不行;
方法2的优势在于能够使用索引,加快搜索速度,但是空闲开销巨大,表内会有articles×article.tags条记录。
想请教大家的是:对于第1种方法,有没有优化方案使得查询tag时效率更高?两种方法有没有更优化的方案?除了上述两种方法,还有没有更好的方法?
谢谢大家