简体   繁体   English

提高SQL LIKE查询性能

[英]Improving a SQL LIKE query performance

We have a large table with product information. 我们有一个包含产品信息的大表。 Almost all the time we need to find product names that contain specific words, but unfortunately these queries take forever to run. 几乎所有时间我们都需要查找包含特定单词的产品名称,但不幸的是这些查询需要永远运行。

Example: Find all the products where the name contains the words "steel" and "102" (not necessarily next to each other, so a product like "Ninja steel iron 102 x" is a match, just like "Dragon steel 102 b" is it). 示例:查找名称中包含“钢铁”和“102”字样的所有产品(不一定要彼此相邻,因此像“Ninja钢铁102 x”这样的产品是匹配的,就像“龙钢102 b”是吗)。

Currently we are doing it like this: 目前我们这样做:

SELECT columns FROM products WHERE name LIKE '%WORD1%' AND name LIKE '%WORD2%' (the number of like words are normally 2-4, but it can in theory be 7-8 or more). SELECT columns FROM products WHERE name LIKE '%WORD1%' AND name LIKE '%WORD2%' (相似单词的数量通常为2-4,但理论上可以是7-8或更多)。

Is there a faster way of doing this? 有更快的方法吗?

We are only matching words, so I wonder if that can help somehow (ie the products in the example above are matches, but "Samurai swordsteel 102 v" is not a match since "steel" doesn't stand alone). 我们只是匹配单词,所以我想知道这是否可以帮助某种程度(即上面例子中的产品是匹配,但“Samurai swordsteel 102 v”不匹配,因为“钢铁”并不孤立)。

My own thought is to make a helper table with the words from productnames in and then use that table to get the ids of the matching products. 我自己的想法是使用productnames中的单词创建一个帮助表,然后使用该表来获取匹配产品的ID。

ie a table like: [id, word, productid] so we get for example: 即一个像:[id,word,productid]这样的表,所以我们得到例如:

1, samurai, 3
2, swordsteel, 3
3, 102, 3
4, v, 3

Just wonder if there is a built in way to do this in MySQL, so I don't have to implement my own stuff + maintain two tables. 只是想知道在MySQL中是否有内置的方法可以做到这一点,所以我不必实现自己的东西+维护两个表。

Thanks! 谢谢!

Unfortunately, you have wild cards at the beginning of the pattern name. 不幸的是,你在模式名称的开头有通配符。 Hence, MySQL cannot use a standard index for this. 因此,MySQL不能使用标准索引。

You have two options. 你有两个选择。 First, if the words are really keywords/attributes, then you should have another table, with one row per word. 首先,如果单词确实是关键字/属性,那么你应该有另一个表,每个单词一行。

If that is not the case, you can try a full text index. 如果不是这种情况,您可以尝试全文索引。 Note that MySQL has attributes for the minimum words length and uses a stop words list. 请注意,MySQL具有最小单词长度的属性,并使用停用词列表。 You should take these into account before building the index. 您应该在构建索引之前考虑这些因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM