Sql 标记数据库对象(字符串标记)和标记查找
数据库中的多个对象需要使用字符串标记(完全任意)进行标记。一种解决方案是经典的多对多关系表示:Sql 标记数据库对象(字符串标记)和标记查找,sql,tsql,sql-server-2008,database-design,Sql,Tsql,Sql Server 2008,Database Design,数据库中的多个对象需要使用字符串标记(完全任意)进行标记。一种解决方案是经典的多对多关系表示: table Customer CustomerId, CustomerName table Product ProductId, ProductName table Tags TagId, TagName table CustomerTags CustomerId, TagId table ProductTags ProductId, TagId 另一个解决
table Customer CustomerId, CustomerName
table Product ProductId, ProductName
table Tags TagId, TagName
table CustomerTags CustomerId, TagId
table ProductTags ProductId, TagId
另一个解决方案是使用XML列来表示标记,以改进顺序查找:
table Customer CustomerId, CustomerName, Tags
table Product ProductId, ProductName, Tags
其中Tags是一个XML列,它具有类似于/Tags/tag的标记和类似于/Tags/tag的路径索引
第一种解决方案提供了更快的查找,但添加了更多的表。第二种解决方案速度较慢,但更干净
我是SQL新手,可能忽略了一些东西,因此非常感谢您的任何意见。我将投票支持第一个解决方案 首先,XML在SQLServer2008上的处理速度比等效的直接tbl桥接标记设置慢。如果您想查找标记为X的所有产品,传统的SQL查询将从tag->product\u tag->product开始。您可以创建(正如您所提到的),但这些索引甚至比XML本身更庞大,并且每个标记至少需要两个索引(一个用于主标记,一个用于值——您需要值二级索引而不是路径二级索引) 其次,如果重命名/删除标记,则必须遍历每个相关对象(产品/客户)的XML结构并使用XML.modify(它的支持非常有限,例如一次只能修改一个节点)