[英]MySQL: reasoning in a chain
我有一个大表,其中包含来自文本文件的单词( offset_1
只是offset
-1):
file offset offset_1 word
---- ------ -------- ----
1.txt 1 0 I
1.txt 2 1 have
1.txt 3 2 a
1.txt 4 3 large
1.txt 5 4 table
1.txt 6 5 that
1.txt 7 6 contains
我想找到给定距离或更短距离的单词对。 例如,“ a”和“ table”之间最多包含1个单词。
我现在要做的是(在MySQL中):
SELECT t1.offset, t3.offset
FROM t as t1 JOIN t as t2 JOIN t as t3
ON t2.file = t1.file AND t3.file = t1.file AND
(
(t1.offset = t2.offset_1 AND t2.offset = t3.offset_1) # "a large table"
OR (t1.offset = t3.offset_1 AND t2.offset = 1) # "a table"
)
WHERE t1.word = 'a' AND t3.word = 'table'
但这永远不会终止(表很大)。
如果删除OR下的两个条件中的任何一个,它将起作用并分别正确地找到“大表”或“表”。
正确的方法是什么?
这项工作会吗
SELECT t1.offset, t2.offset
FROM t as t1
JOIN t as t2 ON t2.file = t1.file
WHERE t1.word = 'a' AND t2.word = 'table'
AND (t2.offset - t1.offset) <= 2
我建议使用union all
将其分为两个查询。 像这样:
SELECT t1.offset, t3.offset
FROM t t1 JOIN
t t2
ON t2.file = t1.file AND t1.offset = t2.offset_1
WHERE t1.word = 'a' AND t2.word = 'table'
UNION ALL
SELECT t1.offset, t3.offset
FROM t t1 JOIN
t t2
ON t2.file = t1.file AND t1.offset = t2.offset_1 JOIN
t t3
ON t3.file = t2.file and t2.offset = t3.offset_1
WHERE t1.word = 'a' AND t3.word = 'table';
OR
在JOIN
条件下通常会对性能产生不良影响。 有时将逻辑拆分为多个子查询可能是一个大胜利
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.