MySQL Best Practices

MySQL Best Practices

Sometimes we may face about performance issues while opening a website. Not only databases but we need to optimize our programming code also.

MySQL Best Practices means few tips to optimize the MySQL query:

  1. Avoid SELECT * and use particular fields from a table what you require.
  2. Use ENUM rather than VARCHAR because ENUM type is very fast and compact.
  3. Every table always have an id field i.e. PRIMARY KEY, AUTO_INCREMENT  and should be UNSIGNED as no negative value will be inserted.
  4. Do Not ORDER BY RAND() as it takes much time.
  5. While you need one unique row use limit 1.
  6. EXPLAIN to check your SELECT queries for performance.
  7. Most effective methods of improving performance is optimize your queries for query cache.
  8. You can use PROCEDURE ANALYSE() to analyze the columns structures and the data in the table.
  9. Use INDEX while searching fields.
  10. For JOIN use same column type for both columns. While joining a DECIMAL column to an INT column from another table, MySQL is unable to use at least one of the indexes.
  11. Store IP Addresses as UNSIGNED INT.
  12. Use mysql_unbuffered_query() for saving amount of memory and you can start working on result set after getting the first row. You do not have wait until the completion of query.
  13. Smaller columns are always faster.
  14. Right storage engine is always good for better performance.
  15. Use Prepared statements for performance and security reasons.
  16. For performance gain use Object Relational Mapper.
  17. Big query always time consuming. So split the big query into limit wise.
  18. For huge data use multiple tables rather than multiple column.
  19. Fixed-length tables are faster.
  20. If you have any reason to use NULL value then use it, otherwise avoid it bacause NULL columns require additional space.

Read Also: Optimize a MySQL database

Leave a Reply

Your email address will not be published. Required fields are marked *