mysql - mysql查询全文检索标签数据库慢

标签 mysql performance full-text-search subquery

需要一些查询帮助。 数据库中有大约 70 万张图像,每个图像都有自己的标签。 我希望能够通过全文搜索查询通过图像的标签来搜索图像。 该查询完全符合我的要求,但速度非常慢。 有人可以帮我加快速度吗,或者创建另一个。 “image_tag”表中的所有 id 字段和名称字段都有索引。

SELECT
    image.*
FROM image
INNER JOIN (SELECT image_to_tag.image_id,
             GROUP_CONCAT(image_tag.`name`) AS tags
        FROM image_to_tag
        INNER JOIN image_tag
        ON (image_tag.id = image_to_tag.image_tag_id)
    GROUP BY image_to_tag.image_id) t ON t.image_id = image.id
WHERE MATCH (t.tags) AGAINST ('+justin* +backgrounds*' IN BOOLEAN MODE)

表:图像

id | filename 
1  | image1.jpg
2  | image2.jpg
3  | image3.jpg

表:image_to_tag

image_id | image_tag_id
1        | 1
1        | 2
2        | 3
2        | 4

表:image_tag

id | name
1  | justin bieber
2  | backgrounds
3  | justin timberlake
4  | other backgrounds

如果我搜索“贾斯汀背景”,我想找到图像 1 和 2。 如果我搜索“justin bieber 背景”,我想找到图像 1。

最佳答案

请尝试这个,并告诉我是否可以更快一点。

select id,filename from image
where id in(
    select image_id from image_to_tag a
    inner join image_tag b
    on b.id = a.image_tag_id and b.name like '%justin%'
)

关于mysql - mysql查询全文检索标签数据库慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811637/

相关文章:

mysql - 在 Postgresql 中使用分数/排名的 FULLTEXT 查询

mysql - 如何从sql语句中选择准确的值

performance - CreateProcess 的快速替代方案

postgresql - PostgreSQL 是否使用 tf-idf?

php - FullText 或 LIKE 搜索问题(无法找到库存)

PHP快速随机字符串函数

PHP 表单验证在提交前显示错误

带有获取文件内容并将数据插入数据库的php cron

mysql - 在 MySQL 服务器版本 8 上执行 MySQL 查询时出错

python - 比带条件的 iterrows 更快的方法(每行的前驱和后继)