If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Optimizing SQL queries on large MySQL tables

Started by yFihivPy, Jun 15, 2023, 12:15 AM

Previous topic - Next topic

yFihivPyTopic starter

This MySQL table contains around 350,000 records which are queried several thousand times in a cycle (3000-10000 requests of the same type).

CREATE TABLE IF NOT EXISTS `mails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `to` varchar(255) NOT NULL,
  `object` varchar(30) NOT NULL,
  `data` text NOT NULL,
  `temp_id` int(11) NOT NULL,
  `mailing_id` int(11) NOT NULL,
  `priority` tinyint(1) NOT NULL,
  `send_time` datetime NOT NULL,
  `error` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;



How can we improve the performance of this SQL query or optimize the table in MySQL to speed up its execution?
  •  


Bestcookware

To optimize the search process, it's recommended to create a separate table for addresses and establish a many-to-many connection between tables. It's important to avoid using the LIKE operator with the % mask at the beginning, as it can be a costly operation that is not based on an index.

Creating a separate table for addresses and establishing a many-to-many connection can help to streamline the search process. This is because using the LIKE operator with the % mask at the start can be very expensive and doesn't rely on an index.
  •  

duenuouck

Generate a combined index for the 'mailing_id' and 'to' fields. Use JOIN instead of the WHERE statement. Additionally, it is recommended to replace "several thousand requests" with one to optimize performance.

Creating composite indices and replacing WHERE statements with JOINs are just a couple of strategies to accomplish this. In addition, reducing the number of requests sent to the database can also lead to better performance.
  •  

moonlife447

Can we optimize the search query that uses multiple LIKE operators? Instead of using many, can we just use one LIKE operator to achieve the same result?

SELECT id, to FROM mails WHERE to LIKE '%info@mail.eu%' AND mailing_id = 172

can you provide more information about the meaning of "mailing_id" and whether it's possible to concatenate it with other values and use the IN operator instead?
  •  

cristine410

To improve the performance of this SQL query or optimize the table, you can consider the following suggestions:

1. Indexing: Analyze the queries that are most frequently used and add appropriate indexes on the columns involved in those queries. In this case, you could consider adding indexes to the columns that are commonly used in WHERE or JOIN clauses, such as "to," "object," "temp_id," "mailing_id," "priority," "send_time," and "error."

2. Choosing an appropriate storage engine: The current table is using the MyISAM storage engine. Consider switching to the InnoDB storage engine, as it provides better transaction support and concurrency control, which can improve performance.

3. Partitioning the table: If the table is growing significantly in size, you could consider partitioning it based on a specific column, such as the date range in the "send_time" column. Partitioning can help in distributing the data across multiple physical files and improve query performance.

4. Optimizing query execution: Review the queries used and ensure they are well-written. Use EXPLAIN command to analyze query execution plans and identify any potential bottlenecks. Make sure indexes are being utilized effectively, avoid unnecessary joins or subqueries, and optimize any expensive operations such as sorting or grouping.

5. Caching: Implement caching mechanisms to reduce the load on the database server. Consider using tools like memcached or Redis to cache frequently accessed data or query results.

6. Hardware and server optimization: Ensure that your hardware resources, such as CPU, memory, and disk, are appropriately allocated and optimized. Monitor server performance and adjust any configuration parameters, such as buffer sizes or connection limits, to match the workload requirements.


7. Use proper data types: Review the column data types and ensure they are the most appropriate for the data being stored. Using smaller data types when possible can help reduce storage requirements and disk I/O.

8. Normalize the database schema: Analyze the relationships between the columns and identify any potential redundancies. If possible, normalize the schema to eliminate redundant data. This can help reduce storage space and improve query performance.

9. Optimize table structure: Evaluate the need for each column in the table. If certain columns are not frequently used or have limited value, consider removing them from the table to reduce disk I/O and improve overall performance.

10. Use stored procedures: If you have complex queries that are frequently executed, consider using stored procedures. Stored procedures can be compiled and executed more efficiently, reducing the overhead of parsing and preparing the query each time.

11. Review server configuration: Ensure that the MySQL server is properly configured for optimal performance. Adjusting settings such as buffer sizes, query cache size, and thread concurrency can have a significant impact on query execution.

12. Consider denormalization: In some cases, denormalization can be beneficial for performance. By duplicating or precalculating certain data in the table, you can avoid costly joins or computations during query execution.

13. Optimize disk access: Ensure that the database files are stored on fast and reliable storage devices, such as SSDs. Additionally, configure the MySQL server to spread the data files across multiple disks or disk arrays to distribute the I/O load.

14. Monitor and analyze query performance: Continuously monitor the query performance using tools like MySQL's EXPLAIN command, query profiling, and performance monitoring tools. This will help identify specific queries that are causing bottlenecks and allow you to optimize them further.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...