MySQL data storage is stored in tables. When looking for data, you must first perform a global scan of the table or search based on the index. This involves a disk search. If the disk search is Searching by points may be faster, but searching sequentially is slower; Redis does not have to be so troublesome. It is stored in the memory and will be retrieved directly according to the location of the data in the memory. (Recommended learning: Redis video tutorial)
Redis is based on memory storage, MySQL is based on disk storage
Redis stores data in k-v format . The time complexity is O(1), constant order, while the underlying implementation of the MySQL engine is B Tree, and the time complexity is O(logn), logarithmic order. Redis will be slightly faster than MySQL.
Redis is a single-threaded multiplexed IO. Single-threaded avoids the overhead of thread switching, while multiplexed IO avoids the overhead of IO waiting, improving processor usage efficiency under multi-core processors. Data can be partitioned and then each processor processes different data.
First of all, we know that mysql is a persistent storage, stored in the disk, and retrieval will involve a certain amount of IO. In order to solve this bottleneck, so Cache appeared, such as memcached (referred to as mc) which is the most used nowadays.
First, the user accesses mc. If it misses, access mysql. Then, like the memory and hard disk, the data is copied to the mc part.
Redis and mc are both caches, and both run in memory, which greatly improves the access speed of web access with high data volume. However, mc only provides simple data structures.
For example, string storage; redis provides a large number of data structures, such as string, list, set, hashset, sorted set, etc., which makes it a lot more convenient for users. After all, it encapsulates a layer of practical functions and simultaneously implements To achieve the same effect, of course use redis and slowly abandon mc.
The relationship between memory and hard disk. The hard disk places the main data for persistent storage, while the memory is the part of the data currently running. The CPU accesses the memory instead of the disk, which greatly improves the running speed. Of course, this It is based on the principle of localized access of the program.
Reasoning to redis mysql, it is a mapping of the memory disk relationship. Mysql is placed on the disk and redis is placed in the memory. In this case, the web application only accesses redis every time. If no data is found, it will be accessed. Mysql.
However, the usage of redis mysql and memory disk is preferably different.
For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!
The above is the detailed content of Why redis is faster than mysql. For more information, please follow other related articles on the PHP Chinese website!