mysql 在带有连接查询的小表上性能缓慢
当我执行以下查询时,我将几个表连接在一起:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在
authors.last_name
和authors.id
列上添加索引。但是,你确定你的查询没问题吗?它不应该看起来像:
如果是这样,则需要
articles.author_id
上的索引 - 虽然不是针对此查询,但作为一般最佳实践Try adding indexes on columns
authors.last_name
andauthors.id
.But, are you sure your query is ok? shouldn't it look like :
If so, an index on
articles.author_id
would be required - although, not for this query, but as a general best practice正如都铎所说,添加索引。您还可以提取分组依据。
执行此操作时,您首先通过连接进行获取,然后在集合中应用聚合函数。
并
解释
看看需要改进的地方。建议的字体:
http://kccoder.com/mysql/join-group-by -性能/
As Tudor said, add indexes. You can also extract the group by.
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/