0

无论如何都可以从查询中获得以下结果而不加入同一个表三次(或)而不读取相同的“wordlocation”表三次(或者如果有更多的话)?如果有三个或更多的单词,大约需要一分钟多的时间才能返回结果。

目前“wordlocation”表有三行(“bookid”、“wordid”、“location”),目前有917802行。

我想做的是

  1. 检索包含“wordid”查询中指定的所有单词的“bookid”。
  2. 每本书中所有单词(来自查询)的总字数
  3. 每个词位置的最小值,例如 (min(w0.location), min (w1.location)

我尝试注释掉 count(w0.wordid) 和 min(location) 计算,看看它们是否影响性能,但事实并非如此。多次加入同一张桌子就是这种情况。

在此处输入图像描述

(这与上图的代码相同)

select 
    w0.bookid, 
    count(w0.wordid) as wcount, 
    abs(min(w0.location) + min(w1.location) + min(w2.location)) as wordlocation, 
    (abs(min(w0.location) - min(w1.location)) + abs(min(w1.location) - min(w2.location))) as distance 
    from 
    wordlocation as w0 
    inner join 
    wordlocation as w1 on w0.bookid = w1.bookid 
    join 
    wordlocation as w2 on w1.bookid = w2.bookid 
    where 
    w0.wordid =3 
    and 
    w1.wordid =52 
    and 
    w2.wordid =42
    group by w0.bookid 
    order by wcount desc;

这是我正在寻找的结果,也是我通过运行上述查询得到的结果,但如果我指定超过 3 个单词,例如 (w0 = 3, w1 = 52, w2 = 42, w3 = 71 )

在此处输入图像描述

4

1 回答 1

0

试试这个查询

    SELECT bookid,
      ABS(L3+L52+L42) as wordlocation,
      ABS(L3-L52)+ABS(L52-L42) as distance
    FROM 
      (SELECT bookid, wordid, CASE WHEN wordid=3 THEN min(location) ELSE 0 END L3,
         CASE WHEN wordid=52 THEN min(location) ELSE 0 END L52,
         CASE WHEN wordid=42 THEN min(location) ELSE 0 END L42
      FROM wordlocation WL
      WHERE wordid in (3,52,42)
      GROUP BY bookid, wordid) T
    GROUP BY bookid

您可能还需要在wordid

于 2012-11-08T00:49:40.217 回答