Query Optimization

The biggest speed-ups can be seen by reprogramming your SQL statements so they are more efficient. If you follow these tips, your server will thank you:

• Select as little data as possible. Rather than select *, select only the fields you need.

• If you only need a few rows, use limit to select the number you need.

• Declare fields as not null when creating tables to save space and increase speed.

• Provide default values for fields, and use them where you can.

• Be very careful with table joins because they are the easiest way to write inefficient queries.

• If you must use joins, be sure you join on fields that are indexed. They should also preferably be integer fields, as these are faster than strings for comparisons.

• Find and fix slow queries. Add log-long-format and log-slow-queries = /var/log/slow-queries.log to your /etc/my.cnf file, under [mysqld], and MySQL will tell you the queries that took a long time to complete.

• Use optimize table tablename to defragment tables and refresh the indexes.



Was this article helpful?

0 0

Post a comment