热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

简单的MySQL查询花费很长时间来计算-SimpleMySQLqueriestakinglongtimetocompute

IamjustlearningMySQLandIhaveaproblem.我只是在学习MySQL而且我遇到了问题。SometimesforvariousqueriesM

I am just learning MySQL and I have a problem.

我只是在学习MySQL而且我遇到了问题。

Sometimes for various queries MySQL starts calculating at 100% CPU usage for 15-20 seconds and than it returns the result normally saying:

有时,对于各种查询,MySQL开始以100%的CPU使用率计算15-20秒而不是通常会返回结果:

Query took 0.1780 sec.

查询耗时0.1780秒。

It happens on very simple queries. For example this query took 0.36 seconds.

它发生在非常简单的查询上。例如,此查询花了0.36秒。

(SELECT DISTINCT a1.actor 
 FROM   actors AS a1, 
        actors AS a2 
 WHERE  a1.title = a2.title 
        AND a1.YEAR = a2.YEAR 
        AND a1.actor = a2.actor 
        AND a1.character_name <> a2.character_name) 

The listing of the table (7000 rows) took 0.001 seconds.

该表(7000行)的列表耗时0.001秒。

On the other hand when I just want to combine these two, MySQL goes crazy and starts calculating for 30 seconds and then finally returning: Query took 0.1800 sec)

另一方面,当我只想将这两者结合起来时,MySQL会疯狂并开始计算30秒然后最终返回:查询耗时0.1800秒)

SELECT actor 
FROM   actors 
WHERE  actor NOT IN (SELECT DISTINCT a1.actor 
                     FROM   actors AS a1, 
                            actors AS a2 
                     WHERE  a1.title = a2.title 
                            AND a1.YEAR = a2.YEAR 
                            AND a1.actor = a2.actor 
                            AND a1.character_name <> a2.character_name) 

Why is this happening?

为什么会这样?

Here is an other example. This query takes around 2 second and reports 0.5

这是另一个例子。此查询大约需要2秒钟并报告0.5

SELECT DISTINCT a1.character_name 
FROM   (actors AS a1 
        NATURAL JOIN movies AS m1), 
       (actors AS a2 
        NATURAL JOIN movies AS m2) 
WHERE  a1.character_name = a2.character_name 
       AND ( m1.title <> m2.title 
              OR ( m1.title = m2.title 
                   AND m1.year <> m2.year ) ) 
       AND m1.country <> m2.country 

On the other hand this query takes 15-20 seconds, CPU 100% but reports 0.3 seconds. (The only difference is a bracket after AND ( .... )

另一方面,此查询需要15-20秒,CPU 100%,但报告0.3秒。 (唯一的区别是AND后的括号(....)

SELECT DISTINCT a1.character_name 
FROM   (actors AS a1 
        NATURAL JOIN movies AS m1), 
       (actors AS a2 
        NATURAL JOIN movies AS m2) 
WHERE  a1.character_name = a2.character_name 
       AND m1.title <> m2.title 
        OR ( m1.title = m2.title 
             AND m1.YEAR <> m2.YEAR ) 
           AND m1.country <> m2.country 

I am using phpMyAdmin and the latest XAMPP for testing.

我正在使用phpMyAdmin和最新的XAMPP进行测试。

Update:

The wrong query times seem to be related to phpMyAdmin, on command line I get the following times:

错误的查询时间似乎与phpMyAdmin有关,在命令行上我得到以下时间:

  • 1st query: MySQL: 0.36 s - PostgreSQL: 0.37 s
  • 第一个查询:MySQL:0.36秒 - PostgreSQL:0.37秒

  • 2nd query: MySQL: 43 s - PostgreSQL: 0.42 s
  • 第二个查询:MySQL:43秒 - PostgreSQL:0.42秒

  • 3rd query: MySQL: 4.86 s - PostgreSQL: 0.05 s
  • 第三个查询:MySQL:4.86秒 - PostgreSQL:0.05秒

  • 4th query: MySQL: 1 min 5 s - PostgreSQL: 15 seconds
  • 第4次查询:MySQL:1分5秒 - PostgreSQL:15秒

So I have the answer for why were the query times reported wrongly (bug either in phpMyAdmin or XAMPP), I am interested in why do such similar queries have such a big difference in running time?

所以我有答案为什么错误地报告查询时间(phpMyAdmin或XAMPP中的错误),我感兴趣的是为什么这样的类似查询在运行时间上有这么大的差异?

Update 2:

Just for completeness I did the testing with PostgreSQL too

为了完整起见,我也使用PostgreSQL进行了测试

4 个解决方案

#1


4  

Have you tried testing your queries with mysql cmd prompt ??? If the problem still persists then the issue might have been with mysql but if the problem is solved then i think you have a problem with phpmyadmin. So let me know that whether your problem still persists after trying your queries with mysql cmd prompt.

你试过用mysql cmd提示测试你的查询吗?如果问题仍然存在,那么问题可能是mysql,但如果问题解决了,那么我认为你有phpmyadmin的问题。因此,请在使用mysql cmd提示尝试查询后,是否仍然存在问题。

#2


1  

Change this (Your first query)

改变这个(你的第一个查询)

SELECT DISTINCT a1.actor 
FROM   actors AS a1, 
actors AS a2 
WHERE  a1.title = a2.title 
AND a1.YEAR = a2.YEAR 
AND a1.actor = a2.actor 
AND a1.character_name <> a2.character_name) 

to this:

SELECT *
FROM actors a1
JOIN actors a2 ON (a1.title = a2.title AND a1.actor = a2.actor)
GROUP BY a1.actor
HAVING a1.character_name <> a2.character_name

and use the same style for the others, also make sure that you have proper indexes on your tables.

并为其他人使用相同的样式,同时确保在表上有适当的索引。

#3


1  

SELECT actor 
FROM   actors 
WHERE  actor NOT IN (SELECT DISTINCT a1.actor 
                     FROM   actors AS a1, 
                            actors AS a2 
                     WHERE  a1.title = a2.title 
                            AND a1.YEAR = a2.YEAR 
                            AND a1.actor = a2.actor 
                            AND a1.character_name <> a2.character_name)

The above query looks like it's trying to select actors who have never played multiple characters on a single title. You could have just said:

上面的查询看起来像是在尝试选择从未在单个标题上播放过多个角色的演员。你可以说:

select   actor
from     actors
group by actor, year, title
having   count(character_name) = 1

However, I know that your question wasn't in regard to your sql writing abilities and you are just trying to figure out why the strange behaviour from MySql. My guess is that it is excluding certain things from the execution time. For example, when google says it took .09 seconds to get your results but you know you waited 10 seconds for the page to load. Google didn't account for the 9.91 seconds it was gonna take to get from its server to your computer... just how long it took for them to query for the data.

但是,我知道你的问题与你的sql编写能力有关,而你只是想弄清楚为什么来自MySql的奇怪行为。我猜测它是从执行时间中排除某些东西。例如,谷歌表示花了0.09秒才能得到你的结果,但你知道你等了10秒才能加载页面。谷歌没有考虑从服务器到你的计算机所需的9.91秒...他们需要多长时间才能查询数据。

This definitely looks like a glaring problem that MySql should address though because unlike Google who can't know the other part of the equation, MySql should be able to incorporate the whole process into the time calculation.

这肯定看起来像MySql应该解决的一个明显的问题,因为不像谷歌谁不知道方程的其他部分,MySql应该能够将整个过程纳入时间计算。

#4


0  

Try to use EXPLAIN to profile your queries. And my advise - not to use subqueries.

尝试使用EXPLAIN来分析您的查询。我建议 - 不要使用子查询。


推荐阅读
author-avatar
15-Sports
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有