gpt4 book ai didi

mysql 左连接全文搜索很慢

转载 作者:行者123 更新时间:2023-11-29 18:13:23 26 4
gpt4 key购买 nike

我有 2 个表:带有单词示例的单词。大约有 7 万个单词,大约有 100 万个示例。我尝试添加带有搜索和分页结果的管理员,但遇到查询需要 6-7 秒才能生成结果。
我做错了什么吗?

这是架构:

```
CREATE TABLE IF NOT EXISTS words (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
word VARCHAR(191) DEFAULT NULL,
fl VARCHAR(191) DEFAULT NULL,
UNIQUE KEY (word, fl),
FULLTEXT (word)
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;


CREATE TABLE IF NOT EXISTS examples (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
fk_words_id BIGINT UNSIGNED,
src VARCHAR(50) DEFAULT NULL,
example VARCHAR(400) DEFAULT NULL,
cite_author VARCHAR(400) DEFAULT NULL,
cite_publication VARCHAR(400) DEFAULT NULL,
cite_title VARCHAR(400) DEFAULT NULL,
FULLTEXT (example,cite_author,cite_publication),
PRIMARY KEY (id),
FOREIGN KEY (fk_words_id) REFERENCES words (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci;
```

我对分页结果的查询如下所示:

SELECT DISTINCT 
a.id,
a.word,
a.fl,
a.updated
FROM words AS a
LEFT JOIN examples AS b
ON a.id = b.fk_words_id
WHERE (a.word = :search_string OR MATCH(b.example,b.cite_author,b.cite_publication) AGAINST(+'some word' IN BOOLEAN MODE))
ORDER BY id asc
LIMIT 0,20;

有什么办法可以提高性能吗?

最佳答案

我建议如下。对一列执行 b 上的不同操作应该比执行左连接然后对 4 列执行不同操作更快。

SELECT
a.id
, a.word
, a.fl
, a.updated
FROM words AS a
LEFT JOIN (
SELECT DISTINCT fk_words_id FROM examples
WHERE MATCH(b.example,b.cite_author,b.cite_publication) AGAINST(+'some word' IN BOOLEAN MODE))
) b ON a.id = b.fk_words_id
WHERE a.word = :search_string
ORDER BY
id ASC
LIMIT 0,20;

关于mysql 左连接全文搜索很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228327/

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