c# - 标签云算法的建议

标签 c# asp.net sql-server sql-server-2005 tag-cloud

我有一个 MSSQL 2005 表:

[Companies](
    [CompanyID] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](128),
    [Description] [nvarchar](256),
    [Keywords] [nvarchar](256)
)

我想为这家公司生成一个标签云。但我已将所有关键字保存在一列中,并用逗号分隔。有关如何通过最常用的关键字生成标签云的任何建议。可能有数百万家公司,每个公司大约有 10 个关键字。

谢谢。

最佳答案

第一步:将关键词拆分成适当的关系(表)。

CREATE TABLE Keywords (KeywordID int IDENTITY(1,1) NOT NULL
  , Keyword NVARCHAR(256)
  , constraint KeywordsPK primary key (KeywordID)
  , constraint KeywordsUnique unique (Keyword));

第 2 步:将公司和标签之间的多对多关系映射到单独的表中,就像所有多对多关系一样:

CREATE TABLE CompanyKeywords (
   CompanyID int not null
   , KeywordID int not null
   , constraint CompanyKeywords primary key (KeywordID, CompanyID)
   , constraint CompanyKeyword_FK_Companies
      foreign key (CompanyID)
      references Companies(CompanyID)
   , constraint CompanyKeyword_FK_Keywords
      foreign key (KeywordID)
      references Keywords (KeywordID));

第 3 步:使用简单的 GROUP BY 查询生成“云”(例如,以“云”表示最常见的 100 个标签):

with cte as (
SELECT TOP 100 KeywordID, count(*) as Count
FROM CompanyKeywords
group by KeywordID
order by count(*) desc)
select k.Keyword, c.Count
from cte c
join Keyword k on c.KeywordID = k.KeywordID;

第 4 步:缓存结果,因为它很少更改且计算成本较高。

关于c# - 标签云算法的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472010/

相关文章:

javascript - 通过推送数组将数据添加到 highstocks

java - org.flywaydb.core.api.FlywayException : Unable to instantiate JDBC driver when my java class is triggered from ant taskdef

c# - mkbundle组合DLL在其他项目中引用时导致Invalid Metadata异常

asp.net - 有没有办法延长已经过期的 Azure sas token 的有效期?

c# - Visual Studio 测试任务 : Test Assembly wildcard format

mysql - 如何从多个表中删除productid

sql-server - SQL Server 2012 数据转换

sql - 单个记录中的数据

c# - 我应该将图像存储在数据库还是文件夹中?

c# - 使用反射设置嵌套属性值