postimg
Dec 2009 18

Speeding Up MySQL with Indexes0

Posted In PHP, SQL By John Hass

MySQL is an excellent database product and even though Oracle just bought out Sun, who bought out MySQL, Oracle still promised to keep up with MySQL patches and releases. (see more here)  This is great news for all us die hard MySQL and PHP guys, we no longer have to worry about giving up our MySQL for something else (because you can have my PHP when you pry it from my cold dead fingers).  Though the question of the day is, when we get Millions of records how can we speed it up. Say for example I have a callrecord database that has a few things in it

id|start_time|from_number|to_number|billsec

When I do a query by id everything is fast, but when I do a query by from_number everything is really slow, maybe even taking hours to complete. So lets create an index on the from_number to make it faster.

CREATE INDEX full_from_number ON table_name (from_number);

Now depending on the number of records you have this could take sometime. You can check on the status by opening another MySQL session and typing “show processlist”. This will show you what is currently going on in MySQL. It may appear that your MySQL has froze, but it hasn’t. Be patient, I had a database take 18 hours before.

Why not index all fields? This is a good question, but it is important to only index the fields that you perform a where on, creating an index, does slow the insert time some, but in my example with the callrecord database, I only filled it once a day and queried 100 times the next day, so creating extra indexes was a good thing to do.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn
  • MySpace
  • PDF
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes