带标签和 GROUP_CONCAT 的产品视图
我在我的应用程序上使用 Sphinx 进行全文搜索。我使用 view
来过滤 products
表中的数据,并为该视图建立索引。现在,我在产品表下有一个硬编码字段及其相关标签,但这不太好,因为标签可能会发生变化,而且我每次都必须操作硬编码字段。
所以我想我可以使用 GROUP_CONCAT 创建视图,用如下语法列出所有相关标签:
SELECT p.id AS id, GROUP_CONCAT(t.tag SEPARATOR '|') AS tags,
UNIX_TIMESTAMP(p.created) AS created
FROM products p
INNER JOIN products_tags pt ON pt.product_id = p.id
INNER JOIN tags t ON t.id = pt.tag_id
AND p.deleted = 0
AND (p.images IS NOT NULL OR p.images <> '')
AND p.status LIKE 'listed'
GROUP BY p.id;
这个查询的问题是它需要很长时间才能运行。真的很慢。如果只检索一条记录,最多需要 5 秒。这是 EXPLAIN
输出:
1, SIMPLE, pt, ALL, , , , , 165029, Using temporary; Using filesort
1, SIMPLE, p, eq_ref, PRIMARY, PRIMARY, 4, trych_default.pt.product_id, 1, Using where
1, SIMPLE, t, eq_ref, PRIMARY, PRIMARY, 4, trych_default.pt.tag_id, 1,
我想知道是否有任何方法可以改进查询,或者可能有更好的解决方案来解决我的问题。谢谢!
I use Sphinx for fulltext search on my app. I use a view
to filter the data from a products
table and Sphinx indexes this view. Right now I have a hard coded field under the products table with it's related tags, but that is not so good as tags might change and I would have to manipulate the hard coded field every time.
So I was thinking that I could create the view with GROUP_CONCAT, listing all the related tags for me with a syntax like this:
SELECT p.id AS id, GROUP_CONCAT(t.tag SEPARATOR '|') AS tags,
UNIX_TIMESTAMP(p.created) AS created
FROM products p
INNER JOIN products_tags pt ON pt.product_id = p.id
INNER JOIN tags t ON t.id = pt.tag_id
AND p.deleted = 0
AND (p.images IS NOT NULL OR p.images <> '')
AND p.status LIKE 'listed'
GROUP BY p.id;
The problem with this query is that it takes ages to run. It's really slow. To retrieve only one record, it takes up to 5 seconds. This is the EXPLAIN
output:
1, SIMPLE, pt, ALL, , , , , 165029, Using temporary; Using filesort
1, SIMPLE, p, eq_ref, PRIMARY, PRIMARY, 4, trych_default.pt.product_id, 1, Using where
1, SIMPLE, t, eq_ref, PRIMARY, PRIMARY, 4, trych_default.pt.tag_id, 1,
I wonder if there is any way to improve the query or maybe a better solution to my problem. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能缺少索引。使用以下架构,相同的查询命中每个表的索引:
以下是 EXPLAIN 查询的结果:
You're probably missing an index. With the following schema, this same query hit the index for every table:
Here are the results of the EXPLAIN query: