开发者

MySql, can anyone suggest how to improve my queries, speed of my script?

I need some help trying to improve the speed of my script. Heres the two queries..

$sql = "SELECT PadID FROM keywords WHERE Word = '$search'";

$sql = "SELECT Category, LongCategory, PageBase, PageName, ProgramName, 
        ProgramVersion, English250, ReleaseDate, License 
        FROM pads 
        LEFT JOIN categories ON pads.CatID = categories.CatID 
        WHERE PadID IN ($pads) 
        ORDER BY VersionAddDate DESC 
        LIMIT $variable,20";

The first query produced a list of PadId's which I use a parameter in the second query.

This开发者_JS百科 works find most of the time, however, if I get a call for a very popular keywords the script can take several seconds to run. This morning I had a keyword with 9600 records.

What can I do to fix it.

EDIT: The problem is my site has been scraped this morning, had several thousand calls within a 20 minutes. However the second query takes a few seconds with that many records anyway, so I need to improve it.


You can do all this in only one query, but I doubt this will greatly improve performances :

SELECT Category, LongCategory, PageBase, PageName, ProgramName, ProgramVersion, English250, ReleaseDate, License 
FROM pads 
INNER JOIN keywords ON pads.PadID = keywords.PadID
LEFT JOIN categories ON  pads.CatID = categories.CatID
WHERE Word = '$search'
ORDER BY VersionAddDate DESC 
LIMIT $variable,20

Otherwise, have you set indexes, primary keys and foreign keys on your tables ?

It is perfectly normal that a query returning thousands of results is slower than one returning 10 results. You can't do much about that. Your query is already pretty simple. The only thing I can think of are bad indexes.


This morning I had a keyword with 9600 records.

9600 returned records is heavy to load AND neither a helpful search result. (who browses more than five google-search-pages?)

Narrowing down the RS to 1% would make more sense, consider adding "weight" based on popularity, date etc.

For best performance, use composite indexes (highest uniqueness).


Do you have any index on the tables involved, for example on VersionAddDate?

Do you know about subqueries?


Did you make sure it is actually the sql query that is slow and not the (php) script execution and/or the rendering in the browser?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜