Finding the MySQL Queries That Need to be Optimized

Finding the MySQL Queries That Need to be Optimized

Author: Robert Agar

There are many aspects of a MySQL database that can lead to performance issues. A system that was returning blazingly fast responses when it was installed a few months ago can suddenly start to bog down for no apparent reason. There were no changes made to the database that should have caused the problem, but it needs to be resolved quickly. Users are starting to complain and pretty soon pressure from management will start to increase as well.

Coming up with a solution to the slow system will eventually come to dominate the database team’s daily activities at the expense of new projects that they have lined up. It can be frustrating for the DBAs as they try to isolate the problem. Many potential culprits have been investigated and judged to be innocent at this time. Items such as network bandwidth, disk space, and available memory have all been checked out and found to be more than adequate to provide the required level of performance.

Just because no changes have been made to the database does not mean the database has not changed. It is obvious that something is dragging down response time. After all of the possible environmental factors have been eliminated, the database team is faced with the reality that the issue must lie with the database itself.

A common mistake when considering the SQL queries that produce results from a database is thinking that they will always continue to perform as they did when the system was originally implemented. It’s easy to understand why a DBA would think this way. The queries have not been modified, so why should the results? A characteristic that is often left out of the equation is the growth in the amount of data that the database contains.

As the size of a database grows, queries will have to wade through more information before they can return the desired results. To address this issue, it may be necessary to revisit the system’s SQL queries and optimize them. Some subtle changes may be all that is needed to satisfy user expectations and allow the team to move on to their next project.

Tuning SQL Queries

One of the pervasive problems that can plague SQL queries and negatively impact their performance is retrieving excess quantities of data. While in some cases this is unavoidable, it is more likely that the query has been poorly constructed. There are numerous ways that queries can be optimized. Here are some that achieve the goal by reducing the quantity of data they process.

  • Queries returning too many rows can be a tremendous resource drain. This problem may not manifest itself when the database is new, but as it is used and more data is stored, the issue can become more than a nuisance. A simple way to fix this type of query is by using the LIMIT clause. Using LIMIT in your queries allows you to fine-tune the number of rows that will be returned in a given query which can have a great impact on its speed.

  • Queries that return too many columns are also problematic and are often the result of using SELECT *. Using this structure can be useful in development as it lends itself to easy implementation, but as the database grows it is better to limit the number of columns.

  • Simplifying queries to reduce execution time is a method that works with MySQL but may not be as useful with other database platforms. This is due to the speed at which MySQL establishes connections. It can be much more effective to use several simple queries in place of a single complex query.

Identifying the Problem Queries

Having viable methods with which to tune SQL queries is all fine and good, but which queries need to be tuned? It can be intimidating to attempt to make this selection without proper guidance. Choosing queries at random will usually not lead to the desired results.  A better method is needed.

SQL Diagnostic Manager for MySQL can help beleaguered DBAs find the queries that need to be optimized. The tool supports MySQL and MariaDB instances located in on-premises data centers and the cloud. It offers real-time monitoring that provides valuable insight into the workings of your databases. As an agent’s solution, it does not demand the installation of monitoring agents on the servers you need to observe.

Using SQL Diagnostic Manager for MySQL enables a database team to quickly identify the slow, locked, and long-running queries that are slowing down their systems. This is a necessary first step in optimizing the queries so the database performs up to expectations. It’s just one of the many features that make this tool an ideal addition to the toolbox of your MySQL DBAs.