Finding the MySQL Queries That Need to be Optimized

by Feb 21, 2020

Categories

Tags

Administration agent-based monitoring Agentless Monitoring alert responses alert thresholds alerting Alerts Amazon Aurora Amazon EC2 Amazon RDS Amazon RDS / Aurora Amazon RDS for SQL Server Amazon Redshift Amazon S3 Amazon Web Services (AWS) Analytics application monitoring Aqua Data Studio automation availability Azure Azure SQL Database azure sql managed instance Azure VM backup Backup and recovery backup and restore backup compression backup status Backup Strategy backups big data Blocking bug fixes business architecture business data objects business intelligence business process modeling business process models capacity planning change management cloud cloud database cloud database monitoring cloud infrastructure cloud migration cloud providers Cloud Readiness Cloud Services cloud storage cloud virtual machine cloud VM clusters code completion collaboration compliance compliance audit compliance audits compliance manager compliance reporting conference configuration connect to database cpu Cross Platform custom counters Custom Views customer survey customer testimonials Dark Theme dashboards data analysis Data Analytics data architect data architecture data breaches Data Collector data governance data lakes data lineage data management data model data modeler data modeling data models data privacy data protection data security data security measures data sources data visualization data warehouse database database administration database administrator database automation database backup database backups database capacity database changes database community database connection database design database developer database developers database development database diversity Database Engine Tuning Advisor database fragmentation database GUI database IDE database indexes database inventory management database locks database management database migration database monitoring database navigation database optimization database performance Database Permissions database platforms database profiling database queries database recovery database replication database restore database schema database security database support database synchronization database tools database transactions database tuning database-as-a-service databases DB Change Manager DB Optimizer DB PowerStudio DB2 DBA DBaaS DBArtisan dBase DBMS DDL Debugging defragmentation Demo diagnostic manager diagnostics dimensional modeling disaster recovery Download drills embedded database Encryption End-user Experience entity-relationship model ER/Studio ER/Studio Data Architect ER/Studio Enterprise Team Edition events execution plans free tools galera cluster GDPR Getting Started Git GitHub Google Cloud Hadoop Healthcare high availability HIPAA Hive hybrid clouds Hyper-V IDERA IDERA ACE Index Analyzer index optimization infrastructure as a service (IaaS) infrastructure monitoring installation Integrated Development Environment interbase Inventory Manager IT infrastructure Java JD Edwards JSON licensing load test load testing logical data model macOS macros managed cloud database managed cloud databases MariaDB memory memorystorage memoryusage metadata metric baselines metric thresholds Microsoft Azure Microsoft Azure SQL Database Microsoft PowerShell Microsoft SQL Server Microsoft Windows MongoDB monitoring Monitoring Tools Monyog multiple platforms MySQL news newsletter NoSQL Notifications odbc optimization Oracle PeopleSoft performance Performance Dashboards performance metrics performance monitoring performance schema performance tuning personally identifiable information physical data model Platform platform as a service (PaaS) PostgreSQL Precise Precise for Databases Precise for Oracle Precise for SQL Server Precise Management Database (PMDB) product updates Project Migration public clouds Query Analyzer query builder query monitor query optimization query performance Query Store query tool query tuning query-level waits Rapid SQL rdbms real time monitoring Real User Monitoring recovery regulations relational databases Releases Reporting Reports repository Restore reverse engineering Roadmap sample SAP Scalability Security Policy Security Practices server monitoring Server performance server-level waits Service Level Agreement SkySQL slow query SNMP snowflake source control SQL SQL Admin Toolset SQL CM SQL code SQL coding SQL Compliance Manager SQL Defrag Manager sql development SQL Diagnostic Manager SQL Diagnostic Manager for MySQL SQL Diagnostic Manager for SQL Server SQL Diagnostic Manager Pro SQL DM SQL Doctor SQL Enterprise Job Manager SQl IM SQL Inventory Manager SQL Management Suite SQL Monitoring SQL Performance SQL Quality SQL query SQL Query Tuner SQL Safe Backup SQL script SQL Secure SQL Security Suite SQL Server sql server alert SQL Server Migration SQL Server Performance SQL Server Recommendations SQL Server Security SQL statement history SQL tuning SQL Virtual Database sqlmemory sqlserver SQLyog Storage Storage Performance structured data Subversion Support tempdb tempdb data temporal data Tips and Tricks troubleshooting universal data models universal mapping unstructured data Uptime Infrastructure Monitor user experience user permissions Virtual Machine (VM) web services webinar What-if analysis WindowsPowerShell

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.