Optimize mysql or use cache?
Specifically, the two optimization strategies I want to compare are optimizing MySQL and caching. Point out upfront that these optimizations are orthogonal, and the only reason you should choose one over the other is that they both cost resources, namely development time.
Optimizing MySQL
When optimizing MySQL, you usually first check the query statement sent to mysql, and then run the explain command. A common approach after a little review is to add indexes or make some adjustments to the schema.
Advantages
1. An optimized query is fast for all users using the application. Because indexes retrieve data at logarithmic complexity speed (aka fractionalization, as you search a phone book, gradually narrowing the search scope), and maintain good performance as the amount of data increases. Caching the results of an unindexed query may sometimes perform worse as the data grows. As data grows, users who miss the cache may get a bad experience and the application becomes unavailable.
2. When optimizing MySQL, you don’t need to worry about cache invalidation or cached data expiration.
3. Optimizing MySQL can simplify the technical architecture, making it easier to copy and work in the development environment.
Disadvantages
1. Some queries cannot improve performance through indexing alone, and may also need to change the mode. In some cases, this may be very troublesome for some applications.
2. Some schema changes may be used for denormalization (data backup). Although this is a common technique for DBAs, it requires ownership to ensure that everything is updated by the application, or triggers need to be installed to guarantee such changes.
3. Some optimization methods may be unique to MySQL. That is, if the underlying software is ported to work on multiple databases, it is difficult to ensure that some of the more complex optimization techniques other than adding indexes are universal.
Using caching
This kind of optimization requires people to analyze the actual situation of the application, and then separate the expensive processing parts from MySQL and replace them with third-party caches, such as memcached or Redis.
Advantages
1. Caching will work well for some queries that are difficult to optimize by MySql itself, such as large-scale aggregation or grouping queries.
2. Caching may be a good solution to improve the throughput of the system. For example, the response speed is very slow when multiple people access the application at the same time.
3. The cache may be easier to build on top of another application. For example: your application may be the front end of another software package that uses MySQL to store data, and it is very difficult to make any database changes to this software package.
Disadvantages
1. If the data provides multiple external access paradigms (for example, displayed in different forms on different pages), then it may be difficult to expire or update the cache, and/or it may be necessary to tolerate expired data The data. A feasible alternative is to design a more sophisticated caching mechanism. Of course, it also has the disadvantage that obtaining the cache multiple times will increase the latency.
2. Caching an expensive object may have a potential performance difference for users who miss the cache (see Advantages of Optimizing MySQL #1). Some good performance practices suggest that you should try to minimize the differences between users, rather than just average them out (which caches tend to do).
3. Naive cache implementation is unable to deal with some subtle vulnerabilities, such as avalanche effect. Just last week I helped someone whose database server was overwhelmed by multiple user requests trying to regenerate the same cached content at the same time. The correct strategy is to introduce a certain level of locking to serialize cache regeneration requests.
Summary
Under normal circumstances, I would recommend users to optimize MySQL first, because this is the most appropriate solution in my opinion at the beginning. But in the long run, most applications will have some use cases that require the above solutions to be implemented at the same time to some extent.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
