gpt4 book ai didi

MySQL 慢查询 : How to optimize the following query?

转载 作者:行者123 更新时间:2023-11-29 06:12:51 25 4
gpt4 key购买 nike

以下是我正在使用的查询:

SELECT *
FROM (
SELECT
(
CASE WHEN product_name like '%word1%' THEN 1 ELSE 0 END +
CASE WHEN product_name like '%word2%' THEN 1 ELSE 0 END +
CASE WHEN product_name like '%word3%' THEN 1 ELSE 0 END
) AS numMatches
FROM products as p
) as derived
WHERE numMatches > 0
ORDER BY numMatches DESC
LIMIT 30,10

我在 product_name 上添加了一个索引(BTREE),列中有 300 万条记录,查询在 3-5 秒内执行。

解释说'使用哪里;使用 filesort' 这样我就可以弄清楚它没有使用索引。

最佳答案

不,它没有使用索引。

为此,您必须与“word1%”、“word2%”等进行比较。但是当您在开头使用 clown 时不起作用。

但是,如果您的 mysql 版本相对较新,您可以使用全文索引,这将用于您的查询。

https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html

关于MySQL 慢查询 : How to optimize the following query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37487041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com