Zerium
Zerium

Reputation: 17333

How to speed up a page with >100 mysql queries?

I have a PHP page on my website, that uses over 100 mysql queries. All the queries are different, and are all just SELECT queries from multiple tables. On average, the page takes about 5 seconds to load, and I wish to improve this time.

What method of optimization do I have? I did some research, and took a look into memcache (I don't know how it works, what it can do or if it applies to my situation, so help may be appreciated), but as I said, I don't know if that is applicable to my situation.

I was also thinking of a query caching program, but don't know of any I can use?

Any help?

Upvotes: 3

Views: 2329

Answers (2)

kwelsan
kwelsan

Reputation: 1219

Below are some points which might be useful to optimize your page load:

MySQL:

  1. Enable Query Cache
  2. Select with only specific columns, avoid select * from syntax
  3. Avoid Co-related inner queries
  4. Use Indexing
  5. Avoid too many queries. If possible then try to use joins/unions

PHP:

  1. Use singleton methodology to avoid multiple database instances
  2. If possible, try calculation work in SQL as well.

HTML:

  1. CDN to load images/js/css parallely
  2. Sprite images
  3. JS include in footer

Upvotes: 2

Alan Kael Ball
Alan Kael Ball

Reputation: 680

There are a number of options for MySQL.

First is to setup a Query Cache in your MySQL config. If your program is SELECT heavy, try setting low-priority-updates to on. This gives higher priority on the server to SELECT statements, and less priority to INSERT/DELETE/UPDATE statements.

Changing MySQL's use of memory might be a good idea, especially if you use a lot of JOIN statements - I usually set the join_buffer_size to about 8M.

From a PHP point-of-view, try caching results.

Edit: the class down the forum page that Suresh Kamrushi posted is a nice way of caching in PHP.

Upvotes: 5

Related Questions