使用左连接的mysql全文搜索很慢

时间:2017-11-10 17:38:44

标签: mysql full-text-search

我有2个表:带有单词示例的单词。有大约100万个单词,包含大约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;

有没有办法改善这里的表现?

1 个答案:

答案 0 :(得分:0)

我建议如下。对一列执行不同的b应该比执行左连接更快,然后执行明显超过4列。

{{1}}