2 small mysql changes to make your website 10x faster

Discuss Article Dashboard management issues, modifications here, and view modifications created by other AD users.

2 small mysql changes to make your website 10x faster

Postby GardenSimply » Sun Oct 19, 2008 3:33 am

From Tony @ eArticlesOnline

I found a couple of sweet indexes you can put into the articles table to radically kick up the speed of your website.

add 2 index to the articles table:
triple-index on status, posteddate, article_id
double-index on status,category_id

The triple-index helps with when the code gets the 50 latest approved articles to show on the main page.
The double-index is because for some reason the code calculates the count of approved articles for each category, yet never shows them.

Testing it from a 3rd party source, i was at 4.55 seconds on the main page.
After the triple-index, down to 2.67 seconds.
After the double-index, down to under 1 second, quite often down around 0.7 seconds.

I should have written down the indexes query itself, but if you use phpMyAdmin, just create 2 new indexes through the gui and you're gold.

Now, just keep in mind, the bigger the website articles table, the more the gain. I'm at around 132,000 items in the table, and I saw a huge gain. At any stage you'll see a big jump, and it makes the code much more scaleable.

Don't forget to optimize the table every so often if you delete things on the regular basis.

At HostGator I was pushing the load averages over 30. Now it hovers under 1 most of the time. This php file will setup the indexes you need. Just upload it into the main directory on your website(same one setup.php is in), and call it from your browser(i.e. http://www.example.com/add_indexes.php) if you save it as add_indexes.php

Code:
Code: Select all
<?php
include("setup.php");

$link = mysql_connect($dbhost, $dbuser, $dbpasswd)
  or die ("Could not connect to MySQL");

mysql_select_db ($dbname)
  or die ("Could not select database $dbname");

$result = mysql_query("ALTER TABLE `{$table_prefix}articles` ADD INDEX `IX_LATEST_ARTICLES` (`status`, `posteddate`, `article_id` )");
$result = mysql_query("ALTER TABLE `{$table_prefix}articles` ADD INDEX `IX_CATEGORY_COUNT` (`status`, `category_id`)");

echo "Articles table is now updated with new indexes.";

?>


Just a note, depending on the system, it can take a little bit. On most of host gator's it takes about 10 seconds, but on some slower systems it could take more than a minute. It'll echo out that it's done when it is.

Tony

Question:
My directory is relatively small - 13k articles. You guys have over 100k articles on your sites. Would small sites see a benefit too?


I bet you would - and since its a no hurt thing... you will definately see the increase in performance as your site grows. Indexing won't hurt. If you don't like it, you just delete the index. It's really a separate lookup table it creates.
User avatar
GardenSimply
Admin
 
Posts: 208
Joined: Sat Sep 15, 2007 10:31 am
Location: Oklahoma

Re: 2 small mysql changes to make your website 10x faster

Postby cjjohn » Thu Dec 18, 2008 12:12 am

I've created a new file add_indexes.php, as suggested, but in reading over your post, it looks like there's more I need to do.

I am aware of phpMyAdmin, but am not educated in it enough to know what I'm supposed to do to add indexes. I found the articles table... now what?

Thanks for sharing this! I look forward to implementing it!

~cj
cjjohn
 
Posts: 4
Joined: Wed Dec 17, 2008 11:41 pm
Location: Cyberia

Re: 2 small mysql changes to make your website 10x faster

Postby GardenSimply » Sat Dec 20, 2008 6:12 am

cj,

Did you pull up the file in a browser? This will request for the php on the page to create the index. And that is really all there is to it when using php to create them... that's why Tony took the time to create the php, so those with less ability doing it 'by hand' can run them. :)
User avatar
GardenSimply
Admin
 
Posts: 208
Joined: Sat Sep 15, 2007 10:31 am
Location: Oklahoma


Return to Article Dashboard Site Management

Who is online

Users browsing this forum: No registered users and 1 guest

cron