Jump to content

Query Takes Forever To Run!


dev-ria

Recommended Posts

hello,

i am running the following query and takes more than 80 seconds to load.

 

 

$query = mysql_query("

SELECT artist,title,url,

COUNT(title) AS TitleCount

FROM results

GROUP BY artist

ORDER BY TitleCount DESC

LIMIT 20",$link2);

 

 

$query2 = mysql_query("

SELECT artist,title,url

FROM results

GROUP BY artist

LIMIT 88",$link2);

 

both run separate while statements and there are about 3.5 million data in the mysql db. no indexes, yet. what can i do to speed this up or clean up my query?

 

thanks

Link to comment
Share on other sites

thanks!

so basically these two queries are bringing up results to put into google graphs

 

http://code.google.c...on#column_chart

 

using column chart - i display the results in google graphs format. this is the full query that sits in the header of the page in javascript for the google graph

 

$query2 = mysql_query("
SELECT artist,title,label
FROM results
WHERE domain = '".$_GET['domain']."'
GROUP BY artist
LIMIT 200",$link2) or die (mysql_error());

while ($run2 = mysql_fetch_array($query2))
{
echo "['".$run2['artist']."', '".$run2['title']."', '".$run2['label']."'],";
}

//second graph ---------

$query = mysql_query("
SELECT artist,title,url,
COUNT(title) AS TitleCount
FROM results
WHERE domain = '".$_GET['domain']."'
GROUP BY artist
ORDER BY TitleCount DESC
LIMIT 20",$link2);.


while($run = mysql_fetch_array($query))
{
echo "['".$run['artist']."', ".$run['TitleCount']."],";
}

 

so far indexing has helped but im still looking to cut the time to about 3-5 seconds

 

 

 

here is EXPLAIN of the two queries

 

1, SIMPLE, results, ref, domain_index, domain_index, 258, const, 1, Using where; Using temporary; Using filesort

 

next one

 

1, SIMPLE, results, ref, domain_index, domain_index, 258, const, 1, Using where; Using temporary; Using filesort

Link to comment
Share on other sites

interestingly enough seems that you "forgot" to mention in every single previous post that your query was using the "domain" column too in the where clause, neither you used that column in the previous EXPLAINS, therefore the EXPLAIN that you are posting now is completely different than before...

 

take out the GROUP BY in the query2... maybe you intent was to use ORDER BY instead... good luck

Link to comment
Share on other sites

yea i realized that i need to use the domain column too. but i have indexed that as well so it should speed things up. so far composite index are artist and title and regular index is domain. i can't see why this shouldn't help speed things up. when I remove the where clause the EXPLAIN key is the artist_title_index.

Link to comment
Share on other sites

SELECT artist,title,url,COUNT(title) AS TitleCount
FROM results 
GROUP BY artist
ORDER BY TitleCount DESC
LIMIT 20


SELECT artist,title,url
FROM results 
GROUP BY artist
LIMIT 88
[/Code]


 
Since there is no WHERE clause, both of those queries need to read the [i]entire[/i] table. A composite index on artist, title, and url; [i]might[/i] prevent a table scan. I have worked with some database servers that will realize that the query is "covered" by the index, and scan only the index. I'm not sure if mySql will do that or not.
 
[code]
SELECT artist,title,label
FROM results
WHERE domain = '".$_GET['domain']."'
GROUP BY artist
LIMIT 200

 

Any given query will use only one index per table. So an index on domain will limit the number of rows to be read. It is not likely that any indexes that do not have domain as the first component will help in this situation.

 

Your queries filter on artist and title, that's all. The items in the WHERE are what matter.

 

Just FYI. Fields used in the ON clause of a JOIN matter as well. Since those are usually FOREIGN KEYS, they are usually indexed anyway; but it bears mentioning here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.