Web applications, content management systems, and other data-intensive applications frequently employ MySQL, one of the most popular relational database management systems. To guarantee effective processing and fast response times as the amount of data and users expand, improving the MySQL database’s speed and optimizing its performance regularly is essential. In this article, we will discuss some best practices for optimizing MySQL database performance, and we’ll talk about some top recommendations for improving MySQL database speed.
- Password Hashing in MySQL 8.0: Authentication Plugins
- Why is Connection Pooling better than Single Connection?.
- Automating Your MySQL Database with Triggers.
- How to use ON DELETE CASCADE in MySQL.
- MySQL + Event Scheduler: How to delete data from MYSQL table using Event Scheduler.
- Using MySQL Event Scheduler: A Complete Guide.
- How to create MySQL database using node.js.
- How to interact with MySQL database using async/await promises in node.js ?
- How to use Sequelize async/await to interact with MySQL database in Node.js.
- MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.
- ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js
- ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.
- Complete JWT Authentication and Authorization System for MySQL/Node.js API.
- How to Build a Complete API for User Login and Authentication using MySQL and Node.js.
- How to store Session in MySQL Database using express-mysql-session.
- Complete JWT Authentication and Authorization System for MySQL/Node.js API
- 1. Hardware and software requirements for MySQL
- 1.1 Hardware Requirements:
- 1.2 Software Requirements:
- 1.3 MySQL Versions:
- 2. Choose the right storage engine:
- 2.1 InnoDB
- 2.2 MyISAM
- 2.3 MEMORY
- 2.4 CSV files
- 3. Optimize database schema:
- 3.1 Normalize your data
- 3.2 Choose suitable data types.
- 3.3 Optimize indexing
- 3.4 Denormalize data for performance
- 3.5 Monitor and optimize performance.
- 4. Optimize queries:
- 4.1 Use the EXPLAIN command
- 4.2 Avoid using SELECT *
- 4.3 Optimize table structure
- 4.4 Use indexes
- 4.5 Use LIMIT clause
- 4.6 Optimize JOINs
- 4.7 Use subqueries
- 4.8 Use the DISTINCT and UNION operators only when required.
- 4.9 Avoid using a wildcard (%) at the beginning of a predicate.
- 4.10 Avoid using functions in predicates.
- 5. Tune server configuration:
- 5.1 Set the right buffer sizes:
- 5.2 Tune the query cache:
- 5.3 Configure the thread pool.
- 5.4 Optimize disk I/O:
- 6. Use caching:
- 7. Monitor performance and regularly maintain the database:
- You might also like:
1. Hardware and software requirements for MySQL
Before we go deep in explaining the practices to optimize MySQL database performance, You need to know that specific hardware and software requirements need to be met to ensure that MySQL runs effectively and efficiently. Let’s take a look at some of the key recommendations for both:
1.1 Hardware Requirements:
- Processor: You don’t need a super fancy processor to run MySQL, but it’s recommended to have a multi-core processor for optimal performance.
- Memory: The amount of memory available is directly related to MySQL’s performance. At least 2GB of memory is recommended, but it’s best to have more depending on the size of your database.
- Disk Space: The amount of disk space you need will depend on the size of your database. You should allocate at least 10% more disk space than the expected size of your database.
- Network: MySQL can be used on a local network or over the internet. A fast network connection is recommended for the best performance.
1.2 Software Requirements:
- Operating System: MySQL can run on various operating systems, such as Windows, Linux, macOS, and UNIX. Make sure your operating system is up-to-date with the latest patches and updates.
- Web Server: You can use MySQL with different web servers such as Apache, Nginx, or IIS.
- Client: To manage your MySQL database, you will need a client such as MySQL Workbench or phpMyAdmin. Make sure the client is compatible with your MySQL version.
1.3 MySQL Versions:
- MySQL Community Edition: This version of MySQL is free and can be downloaded from the MySQL website. It’s ideal for small to medium-sized applications.
- MySQL Enterprise Edition: This is a commercial version of MySQL that comes with additional features such as advanced se
- It is better to use the most current official version of MySQL.
2. Choose the right storage engine:
MySQL supports several storage engines to choose from, such as InnoDB, MyISAM, and MEMORY. Each engine has its own strengths and weaknesses. Choosing the right storage engine for your MySQL database can significantly impact performance, scalability, and reliability. You can select the storage engine that is most appropriate for your unique requirements by comprehending the various types of storage engines, taking into account the needs of your application and considering the hardware and infrastructure.
But before you decide, it’s important to test and benchmark your database with different storage engines to see which performs best for your specific workload. This will require setting up a test environment and running various performance tests to see which storage engine offers your application’s best performance, scalability, and reliability.
InnoDB is the most widely used storage engine for MySQL. It’s ACID-compliant, supports transactions, and offers better concurrency control than MyISAM. InnoDB is a good choice for applications that require high reliability and scalability.
As we already said, the storage engine you choose should be based on the specific requirements of your application. For example, if you’re building a high-traffic e-commerce site, you’ll need a storage engine that can handle many concurrent users and transactions. In this case, InnoDB would be a good choice.
MyISAM is the default storage engine for MySQL. It’s simple, fast, and easy to use but doesn’t support transactions or foreign keys. If you’re building a web application that requires fast read performance, consider using the MyISAM storage engine, as it’s faster than InnoDB for read-heavy workloads.
Memory is a storage engine that stores data in memory, which makes it very fast. However, it’s not durable, as the data is lost when the server is restarted. The Memory storage engine might be a good choice for storing temporary data if you use a server with a lot of RAM.
2.4 CSV files
CSV stores data in CSV files, which makes it easy to import and export data. However, it’s unsuitable for large datasets or applications requiring concurrent access. This clustered storage engine is designed for high availability and scalability. It’s well-suited for applications that require real-time data processing and high availability
3. Optimize database schema:
Numerous companies and groups use MySQL to store, organize, and retrieve data. However, databases can become slow and inefficient as they increase in size and complexity. A good structure of the schema’s tables and well-defined relationships between these tables can improve performance by reducing redundant data, ensuring data integrity, and enabling efficient indexing.
Here are the best practices for optimizing your MySQL database schema:
3.1 Normalize your data
Normalization is the process of organizing data to eliminate redundancy and reduce data duplication. You can increase data accuracy and decrease database size by standardizing your data. There are various normalization levels, but the most frequently applied are 1NF, 2NF, and 3NF. It’s essential to work with a knowledgeable database administrator to ensure your schema is correctly standardized because normalization can be challenging.
3.2 Choose suitable data types.
A crucial component of database architecture is selecting the appropriate data types. The smallest data type that can hold your saved data should be used when creating columns. For instance, you might select the SMALLINT data type rather than the INT data type if you’re keeping integers that are less than 65,535 in number. Using smaller data types can reduce storage requirements and improve performance.
3.3 Optimize indexing
Indexes are an important component of improving database performance because they hasten data retrieval and make locating values for a particular query easier. When designing your database schema, it’s crucial to identify the columns that will be used for querying and ensure that they’re correctly indexed. Over-indexing, however, can delay database uploads and increase storage needs and requirements, so it’s crucial to avoid it.
3.4 Denormalize data for performance
Although normalization is an essential component of database architecture, denormalization can occasionally helps with speed and database performance. Duplicating data across multiple databases during the denormalization process can enhance query speed by lowering the number of joins necessary. You should use denormalization carefully because it can lead to higher storage requirements and more difficult data updates.
3.5 Monitor and optimize performance.
To make sure that your database is operating effectively, you should monitor your database performance. The MySQL Workbench, MySQL Enterprise Monitor, and Percona Monitoring and Management are just a few tools for monitoring MySQL efficiency. You can find speed bottlenecks and optimize the required performance by tracking database performance.
4. Optimize queries:
The most common method of accessing and manipulating data in a database is through queries. By optimizing your queries, you can quickly and significantly improve the response time of your database, which will speed up the performance of your applications. You can use the following techniques to make your MySQL queries more efficient:
4.1 Use the EXPLAIN command
One of the most useful tools in MySQL for query improvement is the EXPLAIN command. It aids in comprehending how MySQL carries out a specific query. The query execution plan is returned by MySQL when the EXPLAIN command is executed before a SELECT statement. A thorough description and explanation of how MySQL will carry out your query, including the indexes it will use and the sequence in which it will join tables, is provided in the query execution plan. You can spot speed bottlenecks by looking at the query execution plan and adjusting your query optimization appropriately.
Example using EXPLAIN command
We can use the EXPLAIN command simply by adding it before the SELECT statement as follows:
EXPLAIN SELECT * FROM orders WHERE customer_id = 12;
The result of the SELECT query using the EXPLAIN command will return the query execution plan, including the type of access used to retrieve data, the indexes used, and the anticipated number of rows that will be returned. Here is an illustration of what the result might resemble:
+----+-------------+-------+------------+------+------------------+---------------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+------------------+---------------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | orders| NULL | ref | idx_customer_id | idx_customer_id | 2 | const | 123 | 100.00 | Using index | +----+-------------+-------+------------+------+------------------+---------------+---------+-------+------+----------+-------------+
4.2 Avoid using SELECT *
When creating queries, many developers make the usual error of using SELECT *. Even if they only need a few fields, the SELECT * command gets all of the table’s columns. Your query may be considerably slowed down by retrieving extraneous data. Therefore, choosing and specifying only the necessary columns in the SELECT statements is always advised.
4.3 Optimize table structure
Table structure optimization can boost query speed. The table structure should be designed in such a way that it reduces the number of table joins required to retrieve data. In addition to that, we use normalization, which is an important part of table structure optimization. Normalization removes data redundancy and enhances data integrity. Redundant data consumes extra space and slows down the queries.
4.4 Use indexes
Indexes are data structures that speed up access to data rows. To increase the performance of MySQL queries, we add indexes to columns frequently used in WHERE, ORDER BY, JOIN and GROUP BY clauses so that the MySQL server will fetch results from a database significantly faster. However, too many indexes can slow down write operations.
4.5 Use LIMIT clause
Use the LIMIT clause to restrict the number of rows returned by a query. You can limit the number of rows that MySQL must handle by using the LIMIT clause. By doing this, the performance of queries can be significantly enhanced, particularly for big tables.
Example using LIMIT clause
In the following example, we use the LIMIT clause in the SELECT statement to retrieve the first 10 customers in the customers table:
SELECT * FROM customers LIMIT 10;
We can also use the LIMIT clause to return a range of rows starting from a specific offset. In the following example, the offset is 8, which means we start to retrieve customers from the 8th row in the table, and the second parameter, 10 specifies the number of rows to return.
SELECT * FROM customers LIMIT 8, 10;
4.6 Optimize JOINs
Joining tables is a costly operation in terms of performance, so JOINs optimization can lead to significant performance improvement. And we can do this by Avoiding joining too many tables in a single query. Instead, break the query into smaller subqueries, each involving fewer tables. We also recommend using INNER JOIN instead of OUTER JOIN wherever possible because INNER JOIN is usually faster.
4.7 Use subqueries
Subqueries are a fantastic way for MySQL query optimization. A subquery is a query within a query, and it is used to limit the number of records that a query processes. For instance, you can reduce the number of rows MySQL has to join by using a subquery that filters rows before joining tables.
Example using subquery
The following query retrieves each customer’s name along with the total number of orders he made “total_orders”. We used a subquery to count the number of orders made by each customer, and it is executed for each row returned by the outer query.
SELECT name, ( SELECT COUNT(*) FROM orders WHERE orders.customer_id = customers.id ) as total_orders FROM customers;
4.8 Use the DISTINCT and UNION operators only when required.
Use the DISTINCT and UNION operators only when required, as doing so will reduce server overhead and speed up response times. This is another helpful query-tuning advice. To improve MySQL performance and speed up the process, consider replacing UNION with UNION ALL and DISTINCT with GROUP BY.
4.9 Avoid using a wildcard (%) at the beginning of a predicate.
In MySQL, wildcard usage brings significant performance limitations and leads to a performance downturn, so you should use them cautiously. For instance, when the LIKE pattern begins with a wildcard, such as ‘%abc,’ MySQL cannot use indexes and must conduct a complete and full table scan instead. So it would be best if you remembered to try using ‘abc%’ whenever possible.
4.10 Avoid using functions in predicates.
It is usually advised to avoid using functions in predicates whenever possible because doing so in MySQL can negatively affect query performance, and index usage is one of the causes. When you use a function in a predicate, MySQL cannot use an index to optimize the query. For instance:
SELECT * FROM orders WHERE DATE(created_at) = '2023-03-02';
In this case, MySQL cannot use the index to optimize this query because the DATE function is applied to the “created_at” column even the “created_at” column is indexed, and MySQL must read all rows in the orders table and use the function to each row, and this can be slow for big tables.
5. Tune server configuration:
The server setup options, including buffer sizes, cache sizes, and thread settings, can significantly impact the database speed. Adjusting these parameters to fit the workload and hardware capabilities can enhance performance. But before you start tinkering with the MySQL server configuration, you need to understand the workload that the server is handling. You need to know the database size, the number of people accessing it simultaneously, the type of queries being executed, and the available hardware resources. This information will help you decide which configuration options to tweak. We recommend using tools such as MySQL Tuner or MySQL Workbench to analyze the server configuration and suggest optimization settings.
5.1 Set the right buffer sizes:
One of the most crucial things to consider is buffer sizes. Buffers are used to cache frequently accessed data to reduce disk I/O. The InnoDB buffer pool and MyISAM key buffer are the most important buffers. These buffers store frequently accessed data and indexes for InnoDB tables and index blocks for MyISAM tables, respectively. However, the default buffer sizes are usually set to small values, so you must set them to appropriate sizes based on the available memory and workload.
5.2 Tune the query cache:
The query cache is another crucial component that you need to optimize. The query cache stores the results of SELECT queries so they can be quickly retrieved from memory the next time the same query is executed. However, if the cache is too large, it can consume too much memory and lead to increased overhead. On the other hand, if the cache is too small, it may provide little benefit. You must set the appropriate size based on the workload and monitor its hit rate and overhead.
5.3 Configure the thread pool.
MySQL uses threads to handle client connections and queries. However, by default, MySQL creates a new thread for each connection, which can lead to high overhead and reduced performance. To optimize thread handling, you can configure the thread pool plugin, which creates a pool of threads to handle client connections and queries. The thread pool plugin can improve performance by reducing the overhead of thread creation and destruction and by ensuring that the maximum number of threads is not exceeded. For more details, you can check this article: Why is Connection Pooling better than Single Connection?.
5.4 Optimize disk I/O:
Disk I/O is another significant bottleneck in database systems. To optimize disk I/O, you can use RAID arrays or SSDs to improve the speed and reliability of data access. You can also configure MySQL to use asynchronous I/O, which can reduce the overhead of synchronous I/O and enhance performance. In addition, you can optimize MySQL’s file system cache by increasing the size of the file cache and the number of file handles.
6. Use caching:
Caching is a great way to speed up MySQL databases. By caching frequently accessed data, you can reduce the number of disk reads required to retrieve data, which can result in faster query response times. There are a few different types of caching mechanisms that can be used in MySQL, but two of the most common ones are query caching and buffer caching.
Query caching stores the results of SELECT queries in memory. When a SELECT query is executed, MySQL checks if the query is already in the query cache. If it is, MySQL retrieves the results from memory instead of executing the query again, which can save a lot of time. However, query caching only works well for queries that are executed frequently and return consistent results. If your database is frequently updated, query caching may be ineffective.
The second type of caching is Buffer caching. Buffer caching stores frequently accessed data in memory. The InnoDB buffer pool is the most important buffer in MySQL, and it stores data and indexes for InnoDB tables. When data is read from the disk, MySQL stores the data in the InnoDB buffer pool. If the data is accessed again, MySQL retrieves it from the buffer pool rather than reading it from the disk again. This can significantly reduce the time required to retrieve data and improve query response times.
If you want to optimize caching in MySQL, you need to adjust the size of the query cache and the InnoDB buffer pool based on the size of your database and the available memory. It’s important to note that setting the size of the cache too large can lead to increased overhead and reduced performance. Monitoring the cache hit ratio can help you determine your caching configuration’s effectiveness and adjust it accordingly.
7. Monitor performance and regularly maintain the database:
Monitoring the performance of a MySQL database is an essential task for any database administrator. It helps to ensure that the database is running at its best and that any issues or problems can be identified and fixed on time. There are several ways to monitor the performance of a MySQL database:
- One of the most important is regularly checking the server resources, such as CPU, memory, and disk usage. This can be done using system monitoring tools like top, iostat, or vmstat.
- Another helpful way to monitor the performance of a MySQL database is to use MySQL’s built-in performance monitoring tools. These include the MySQL Performance Schema, MySQL Enterprise Monitor, and MySQL Query Analyzer. These tools provide valuable insights into the performance of the database and can help identify any bottlenecks or performance issues.
- It’s also important to monitor key database performance metrics, such as database connections, query throughput, and slow queries. You can use MySQL’s slow query log to identify queries that take too long to execute and optimize them for better performance.
- Regularly optimizing tables can also help improve the performance of a MySQL database. This involves reducing the size of tables and improving query execution times. You can use MySQL’s OPTIMIZE TABLE command to optimize tables.
- Finally, backing up the database regularly is essential to ensure you have a copy of your data in case of a failure or disaster. This can be done using MySQL’s mysqldump command or other backup tools.
In conclusion, effective server configuration, efficient queries, and a good design for the database are all necessary for maximizing the performance of a MySQL database. You can enhance the performance of the functionality of MySQL database, shorten response times, and guarantee a positive user experience by adhering to these best practices.