Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The reason why redis is faster than mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you the reason why redis is faster than mysql. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

MySQL data storage is stored in the table. When looking for data, you need to scan the table globally or look up the data according to the index, which involves disk lookup. Disk lookup may be faster if you look up by point, but the sequential lookup is relatively slow. While Redis does not have to be so troublesome, it is stored in memory itself and will be taken out directly according to the location of the data in memory.

Redis is based on memory storage and MySQL is based on disk storage

Redis stores data in KMTV format. The time complexity is O (1), constant order, while the underlying implementation of MySQL engine is B+Tree, time complexity is O (logn), logarithmic order. Redis will be a little faster than MySQL.

Redis is a single-threaded multiplex IO, which avoids the overhead of thread switching, while multiplexing IO avoids the overhead of IO waiting. To improve the efficiency of processors under multi-core processors, data can be partitioned, and then each processor processes different data.

First of all, we know that mysql is persistent storage, stored in disk, retrieval, will involve a certain IO, in order to solve this bottleneck, so there is a cache, such as the most used memcached (referred to as mc).

First, the user accesses the mc, if he misses, he accesses the mysql, and then, like memory and hard disk, copies the data to the MC part.

Both redis and mc are cached and run in memory, which greatly improves the access speed of high data volume web access. However, mc only provides a simple data structure.

For example, string storage; redis provides a large number of data structures, such as string, list, set, hashset, sorted set and so on, which makes it much more convenient for users. After all, it encapsulates a layer of practical functions and achieves the same effect at the same time. Of course, use redis and slowly abandon mc.

The relationship between the memory and the hard disk, the hard disk places the principal data for persistent storage, while the memory is the currently running part of the data, CPU accesses the memory rather than the disk, which greatly improves the running speed, of course, this is based on the local access principle of the program.

Inference to redis+mysql, which is a mapping of memory + disk relationship, mysql on disk, redis on memory, so that the web application only accesses redis each time, and accesses Mysql only if no data is found.

However, redis+mysql and memory + disk are best used differently.

These are the reasons why the redis shared by the editor is faster than mysql. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report