Optimize MySQL Select Query size

The less data your website pulls from the back-end MySQL server, the faster it is. Imagine you need to display just 3 rows from a table with 2,500 records, but your code is looping through all 2,500 records generating extra, unnecessary load.

For example:

SELECT name,lastname from UserTable;

Optimized SQL query would be:

SELECT name,lastname from UserTable Limit 3;

Or, for example, you have a blob record of an article with 550 words and you want to display only the first 50 characters for the excerpt. Why do you need to pull out all 550 words if you can write a query that is friendlier and works much faster?

SELECT title, SUBSTRING(article,1,50) from ArticleTable Where Id=”5″;

The key to the optimization is writing smart code and always optimizing for performance.

Optimize MySQL queries:

To pull only data you need,
Limit loops,
Limit answer length when needed,
Use correct indexes,
Use MySQL caching settings.


Comments

  1. HTTP Guy says:

    You can also use caching using memcached or tugela cache to cache MySQL queries for best speed & performance.

  2. akash says:

    this is very bad

Leave a Reply