Mysql加速max()组

编程入门 行业动态 更新时间:2024-10-24 10:23:52
本文介绍了Mysql加速max()组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的查询来分组需要0.0045秒的行。 for 300.000 rows

SELECT cid FROM表GROUP BY cid

$ b

SELECT MAX(id)id,cid FROM表GROUP BY cid

如何加快查询速度?查询在我的本地主机上运行以进行测试。 id = primary key,并且我有cid上的索引。

解决方案

原因是两个查询之间的差异:

  • 您的第一个查询永远不会触及表 - 它只依赖于索引
  • 您的第二个查询实际需要击中所有行

因此,要回到更理想的第一种情况,您需要一个索引,它可以同时提供: cid和最小/最大ID。您可以尝试通过在(cid,id)上创建索引来实现此目的。

I have an easy query for grouping rows which takes 0.0045 sec. for 300.000 rows

SELECT cid FROM table GROUP BY cid

When I add MAX() to query it takes 0.65 sec to return.

SELECT MAX(id) id, cid FROM table GROUP BY cid

How can I speed up this query? The query runs on my local host for testing. id = primary key and I have index on cid.

解决方案

The reason is the difference between the two queries:

  • Your first query will never touch the table - it wil rely on the index only
  • Your second query actually needs to hit all rows

So to get back to the more optimal first case, you need an index, that can provide both: grouping by cid and min/maxing id. You could try to achieve this by creating an index on (cid,id)

更多推荐

Mysql加速max()组

本文发布于:2023-11-22 13:57:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mysql   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!