【发布时间】:2012-12-24 05:10:01
【问题描述】:
我有一个大约有 100,000 行的表。
每一行包含一个句子、句子片段或短语。
我想编写一个查询来查找包含 all 组词的所有行,即使条件中的词与句子。
例如,如果我的表格如下所示:
id sentence
-- ---------------------------------------------------------------------------
1 How now brown cow
2 Alas, poor Yorick! I knew him
3 Call me Ishmael
4 A screaming comes across the sky
5 It was a bright cold day in April, and the clocks were striking thirteen
6 It was the best of times, it was the worst of times
7 You don't know about me without you have read a book
8 In the late summer of that year we lived in a house in a village
9 One summer afternoon Mrs. Oedipa Maas came home from a Tupperware party
10 It was a queer, sultry summer, the summer they electrocuted the Rosenbergs
我的查询条件是一个或多个单词,按任何特定顺序。
结果集应该包含包含所有单词的所有句子。
例如,如果条件为the was,则结果应包括第 5、6、10 行。
理想情况下,我想改进这一点,以便查询只需要包含单词的开始。 (请注意,我希望允许用户只输入单词的开头,而不能只输入中间或结尾)。
例如,如果条件是 elect sul,则结果将包括第 10 行。
目前,我是这样做的:
SELECT
id, sentence
WHERE
(sentence LIKE 'elect%' OR sentence LIKE '% elect%')
AND
(sentence LIKE 'sul%' OR sentence LIKE '% sul%')
这行得通(我认为......) - 它找到了它应该找到的一切。但是,速度很慢。
有没有更好的方法来做到这一点?
为了它的价值 - 我可以灵活地重新设计表格,或创建额外的“帮助”表格。
例如,我考虑创建一个表,其中包含每个唯一单词的一行以及包含它的句子的每一行的键。
另外 - 查询需要在 MySQL 中工作。
非常感谢。
【问题讨论】:
-
SQL 不是为模糊搜索而设计的。您可以将文本存储在 MySql 中,但我建议使用 Lucene 搜索。