Simple MySQL queries taking long time to compute

你说的曾经没有我的故事 提交于 2019-12-05 04:51:57
Alim Ul Gias

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.

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!