SQL 查询以查找具有最匹配关键字的行

SQL query to find rows with the most matching keywords

我真的很不擅长 SQL 我想知道 SQL 我可以 运行 解决我怀疑是 NP 完全问题的问题但是我同意在大型数据集上花费很长时间 运行 的查询,因为这将作为后台任务完成。首选标准 sql 语句,但如果需要存储过程,那就这样吧。在 Postgres 9.3 上 运行 需要 SQL。

问题:给定一组包含一组关键词的文章,为每篇文章找到包含最多匹配关键词的前 n 篇文章。

文章 table 的精简版如下所示:

CREATE TABLE article (
  id character varying(36) NOT NULL,  -- primary key of article
  keywords character varying,         -- comma separated set of keywords

  CONSTRAINT pk_article PRIMARY KEY (id)
);

-- Test Data
INSERT INTO article(id, keywords) VALUES(0, 'red,green,blue');
INSERT INTO article(id, keywords) VALUES(1, 'red,green,yellow');
INSERT INTO article(id, keywords) VALUES(2, 'purple,orange,blue');
INSERT INTO article(id, keywords) VALUES(3, 'lime,violet,ruby,teal');
INSERT INTO article(id, keywords) VALUES(4, 'red,green,blue,yellow');
INSERT INTO article(id, keywords) VALUES(5, 'yellow,brown,black');
INSERT INTO article(id, keywords) VALUES(6, 'black,white,blue');

这将导致 SELECT * FROM article; 查询:

Table: article
------------------------
id  keywords            
------------------------
0   red,green,blue      
1   red,green,yellow    
2   purple,orange,blue  
3   lime,violet,ruby,teal
4   red,green,blue,yellow
5   yellow,brown,black
6   black,white,blue

假设我想为每篇文章找到包含最多匹配关键字的前 3 篇文章,那么输出应该是这样的:

------------------------
id  related
------------------------
0   4,1,6
1   4,0,5
2   0,4,6
3   null
4   0,1,6
5   1,6
6   5,0,4

喜欢:使用规范化设计会更简单(除了使其他任务更简单/更清晰),但仍然不简单

此外,数据类型 character varying(36) 的 PK 列非常可疑(且效率低下),很可能应该是 integer type or at least a uuid

这是一种基于您的设计的可能解决方案

WITH cte AS (
   SELECT id, string_to_array(a.keywords, ',') AS keys
   FROM   article a
   )
SELECT id, string_agg(b_id, ',') AS best_matches
FROM  (
   SELECT a.id, b.id AS b_id
        , row_number() OVER (PARTITION BY a.id ORDER BY ct.ct DESC, b.id) AS rn
   FROM   cte a
   LEFT   JOIN cte b ON a.id <> b.id AND a.keys && b.keys
   LEFT   JOIN LATERAL (
      SELECT count(*) AS ct
      FROM  (
         SELECT * FROM unnest(a.keys)
         INTERSECT ALL
         SELECT * FROM unnest(b.keys)
         ) i
      ) ct ON TRUE
   ORDER  BY a.id, ct.ct DESC, b.id  -- b.id as tiebreaker
   ) sub
WHERE  rn < 4
GROUP  BY 1;

sqlfiddle(改为使用整数 id)。

CTE cte 将字符串转换为数组。你甚至可以有一个像这样的功能性 GIN 索引......

如果前 3 顺位有多个行并列,您需要定义一个 决胜局。在我的示例中,id 较小的行排在第一位。

最近相关回答中的详细解释:

比较是在 JSON 数组和 SQL 数组之间进行的,但它基本上是相同的问题,归结为相同的解决方案。还比较了几个类似的替代方案。

为了加快速度,您至少应该在数组列上有一个 GIN 索引(而不是逗号分隔的字符串),并且查询不需要 CTE 步骤。完全规范化的设计还有其他优势,但不一定比具有 GIN 索引的数组更快。

可以 以逗号分隔的字符串形式存储列表。没问题,只要这对您来说只是一个字符串并且您对其单独的值不感兴趣即可。一旦您 对单独的值感兴趣,就像在您的示例中一样,请将它们分开存储。

这就是说,纠正你的数据库设计,然后再考虑查询。

下面的查询先选择所有的ID对,统计常用关键字。然后,它通过为另一个 ID 提供共同排名第一的关键字最多的 ID 来对这些对进行排名,等等。然后您只保留三个最匹配的 ID。 STRING_AGG 列出按共同关键字数量排序的字符串中最匹配的 ID。

select 
  this_article as id,
  string_agg(other_article, ',' order by rn) as related
from
(
  select 
    this_article, 
    other_article, 
    row_number() over (partition by this_article order by cnt_common desc) as rn
  from
  (
    select 
      this.id as this_article, 
      other.id as other_article, 
      count(other.id) as cnt_common
    from keywords this
    left join keywords other on other.keyword = this.keyword and other.id <> this.id
    group by this.id, other.id
  ) pairs
) ranked
where rn <= 3
group by this_article
order by this_article;

这里是 SQL fiddle: http://sqlfiddle.com/#!15/1d20c/9.