MySQL/PostgreSQL – how to improve and find issues

Things to consider to improve your queries

  1. Do you need to retrieve all the columns for each row in the database by executing queries like (Select * from user), try to avoid this especially when you are showing many rows in a list (HTML table,…), so in that case choose the needed fields;
    Notice: this can be different on Row vs Column Oriented Databases
  2. Do you retrieve all database table records to just get the actual count? well you can use count for example:
  3. Optimizing columns by reducing the size: do you need varchar(255) for a first_name, last_name, phone_number? then try to add an upper limit like varchar(80) depends on the data. In this way you will save some space.
  4. If you have a database table that will hold just country names why use datatype of unsigned int? use unsigned tiny int
  5. In MySQL old versions there was a limit for varchar to be at max 255, but in newer versions like MySQL 5.8 you are limited just by the row size, so it might be better to use varchar instead of text for 400 characters, Hint: text data is different, because it is stored in external storage
  6. Indexes – indexes can be very important to improve the performance, but use it for columns you actually use in your queries like filtering data by name, category
Find slow queries
  1. MySQL includes the EXPLAIN statement, which provides information about how MySQL executes queries. More information on this.
  2. There is a configuration options in mysqld.cnf file to configure options for the MySQL server. Find these options: (slow-query-log, long_query_time) They can help you find slow queries which exceeds a specific limit time (which you can also configure, for example two seconds) in log files. More information on this.

I hope this was helpful to you.
Will try to add other helpful things maybe in another post.

mhmd

Hello, I'm Mohammad Daka, software engineer who love coding. In my spare time I blogs about JavaScript,PHP, Chrome Extensions, helpful resources and tools, I'm also working on my own projects. Don't hesitate to contact me at my twitter account: @mohammad_daka :)

You may also like...

1 Response

  1. Thank you for sharing this information. Looking for such more content in the future.

Leave a Reply to online training Cancel reply

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