mysql 在带有连接查询的小表上性能缓慢

发布于 2024-12-08 18:35:04 字数 1514 浏览 0 评论 0原文

当我执行以下查询时,我将几个表连接在一起:

SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.id = authors.id WHERE authors.last_name =
'bloggs' GROUP BY article.year

出于某种原因,这需要 6 到 7 秒才能返回结果,鉴于它必须处理的行数相对较少,这对我来说似乎慢得令人难以置信和。我在这里做错了什么吗?

如果我对查询运行 EXPLAIN,我会得到以下信息:

select_type    table    type   possible_keys  key    key_len    ref    rows    extra
=====================================================================================
simple         article  all    null           null    null      null   762     using temporary; using filesort
simple         authors  all    null           null    null      null   5061    using where; using join buffer

两个表都是 InnoDB。我在本地机器上运行这个程序,该机器的规格相当低(windows xp,1 ghz,1 gb ram),但即便如此,我还是认为这会更快。如果我将更多行加载到表中,则开始需要几分钟而不是几秒钟。

有什么想法吗?

表结构如下:

Article:

field    type       null    key    default    extra
=======================================================
id       int        yes            null
year     char(20)   yes            null
volume   char(20)   yes            null
issue    char(20)   yes            null
title    text       yes            null

Authors:

field      type       null    key    default    extra
=======================================================
id         int        yes            null
last_name  char(100)  yes            null
initials   char(10)   yes            null

I have a couple of tables that i join together when i execute the following query:

SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.id = authors.id WHERE authors.last_name =
'bloggs' GROUP BY article.year

For some reason, this is taking between 6 and 7 seconds to return the results which seems unbelievably slow to me given the relatively small number of rows it has to deal with. Am I doing something wrong here?

If I run an EXPLAIN on the query I get the following:

select_type    table    type   possible_keys  key    key_len    ref    rows    extra
=====================================================================================
simple         article  all    null           null    null      null   762     using temporary; using filesort
simple         authors  all    null           null    null      null   5061    using where; using join buffer

Both tables are InnoDB. I'm running this from my local machine which is fairly low spec (windows xp, 1 ghz, 1gb ram) but even so, I would have thought this would be quicker. If I load a few more rows into the tables it starts to take minutes rather than seconds.

Any thoughts?

Table structures below:

Article:

field    type       null    key    default    extra
=======================================================
id       int        yes            null
year     char(20)   yes            null
volume   char(20)   yes            null
issue    char(20)   yes            null
title    text       yes            null

Authors:

field      type       null    key    default    extra
=======================================================
id         int        yes            null
last_name  char(100)  yes            null
initials   char(10)   yes            null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-12-15 18:35:04

尝试在 authors.last_nameauthors.id 列上添加索引。

但是,你确定你的查询没问题吗?它不应该看起来像:

SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.author_id = authors.id WHERE authors.last_name =
'bloggs' GROUP BY article.year

如果是这样,则需要 articles.author_id 上的索引 - 虽然不是针对此查询,但作为一般最佳实践

Try adding indexes on columns authors.last_name and authors.id.

But, are you sure your query is ok? shouldn't it look like :

SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.author_id = authors.id WHERE authors.last_name =
'bloggs' GROUP BY article.year

If so, an index on articles.author_id would be required - although, not for this query, but as a general best practice

茶色山野 2024-12-15 18:35:04

正如都铎所说,添加索引。您还可以提取分组依据。

SELECT * FROM (SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.author_id = authors.id WHERE authors.last_name =
'bloggs') GROUP BY article.year

执行此操作时,您首先通过连接进行获取,然后在集合中应用聚合函数。

解释看看需要改进的地方。

建议的字体:

http://kccoder.com/mysql/join-group-by -性能/

As Tudor said, add indexes. You can also extract the group by.

SELECT * FROM (SELECT article.year, authors.last_name, count(DISTINCT article.id) as count FROM
article LEFT JOIN authors ON article.author_id = authors.id WHERE authors.last_name =
'bloggs') GROUP BY article.year

Doing this you are first fetching by the join, and in the set, applying the aggregate function.

And explain to see where is the spot to the improvement.

Font of the suggestion:

http://kccoder.com/mysql/join-group-by-performance/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文