标记数据库对象(字符串标签)和标签查找
我们数据库中的多个对象需要用字符串标签来标记(完全任意)。一种解决方案是经典的多对多关系表示:
table Customer CustomerId, CustomerName
table Product ProductId, ProductName
table Tags TagId, TagName
table CustomerTags CustomerId, TagId
table ProductTags ProductId, TagId
另一种解决方案是使用一个 XML 列来表示带有 PATH 辅助 XML 索引,以改进顺序查找:
table Customer CustomerId, CustomerName, Tags
table Product ProductId, ProductName, Tags
其中 Tags 是一个 XML 列,其中包含诸如
第一个解决方案提供了更快的查找速度,但添加了更多表。第二种解决方案速度较慢但更干净。
我是 SQL 新手,可能忽略了一些内容,因此任何输入都将受到高度赞赏。
Multiple objects in our database need to be tagged by string tags (completely arbitrary). One solution is a classic many-to-many relationship representation:
table Customer CustomerId, CustomerName
table Product ProductId, ProductName
table Tags TagId, TagName
table CustomerTags CustomerId, TagId
table ProductTags ProductId, TagId
Another solution would be to have an XML column that represents tags with a PATH secondary XML index to improve sequential lookup:
table Customer CustomerId, CustomerName, Tags
table Product ProductId, ProductName, Tags
Where Tags is an XML column that would have tags like <tags><tag name="tag1" /></tags> and PATH index like /tags/tag
The first solution gives a faster lookup, but adds more tables. The second solution is slower but cleaner.
I'm new to SQL and might have overlooked something, so any input would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的投票将投票给第一个解决方案。
首先,XML 在 SQL Server 2008 上的处理速度比同等的直接 tbl-bridge-tag 设置慢。如果你想找到所有带有X标签的产品,传统的SQL查询将从tag->product_tag->product开始。您可以创建 XML 索引(如你已经提到过),但它们比 XML 本身还要庞大,并且每个标签至少需要两个索引(一个用于主索引,一个用于 VALUE - 你需要一个 VALUE 二级索引而不是 PATH 二级索引指数)。
其次,如果您重命名/删除标签,则必须遍历每个相关对象(产品/客户)的 XML 结构并使用 xml.modify (其支持非常有限 - 例如一次只能修改一个节点) )。
My vote would be on the first solution.
First of all, XML is slower to process on SQL Server 2008 than the equivalent straight tbl-bridge-tag setup. If you wanted to find all products that are tagged X, a conventional SQL query will start from tag->product_tag->product. You could create XML indexes (as you have mentioned), but those are even bulkier than the XML themselves and you need at least two indexes per tag (one for primary and one for VALUE - you want a VALUE secondary index instead of a PATH secondary index).
Secondly, if you rename/drop a tag, you would have to go through the XML structure of each related object (product/customer) and use xml.modify (which has very limited support - e.g. only one node can be modified at a time).