search
HomeDatabaseMysql Tutorial浅谈 Redis 与 MySQL 的耦合性以及利用管道完成 MySQL 到 Redis

㈠ Redis 与 MySQL 的耦合性 ? ?? ? ?? ? ? 在业务架构早期、我们便该吃着碗里的看着锅里的、切莫让MySQL 有梦、而Redis 无心 ? ? 毕竟、有些关系型的结构不适合放到Redis跑、男女搭配、干活不累嘛、推荐让MySQL与Redis喜结连理 ? ?? ? ? 其次、这 2 人、一

㈠ Redis 与 MySQL 的耦合性
? ??
? ??
? ? 在业务架构早期、我们便该"吃着碗里的看着锅里的"、切莫让MySQL 有梦、而Redis 无心
? ? 毕竟、有些关系型的结构不适合放到Redis跑、"男女搭配、干活不累"嘛、推荐让MySQL与Redis喜结连理
? ??
? ? 其次、这 2 人、一般是在不同场景做选择、而不会在性能上选择、
? ? 只有在 2 者都可用的情况下、综合性能、硬件成本、运维成本等选择
? ? 比如、网页游戏启用 Redis+MySQL:
? ? 游戏中的:好友关系、排行榜、计数器、队列、cache都很适合通过 Redis来实现
? ??
? ? 再举个例子是新浪微博的架构、比如用户关注关系:

? ? 在 MySQL中是 这样一行一行存储的。而在 Redis中你可以存成一个set,或者zset等


? ? ? ? ??



? ? 大体流程是由 MySQL 复制到 Redis 的
? ? 基本结构应该是:
? ? 1. 发微博-- > 进入消息队列-- > 存入MySQL-- > 复制到Redis
? ? 2. 查询 -- > 查询缓存-- > 查询Redis -- > 查询MySQL
? ??
? ??
? ??
? ? ㈡ 快速迁移 MySQL →→ Redis?


? ??
? ? ? ?① MySQL 要导出的表 david_lin


mysql> desc david_lin;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| myname  | varchar(25) | NO   | UNI | NULL    |       |
| mymoney | int(11)     | NO   |     | 0       |       |
+---------+-------------+------+-----+---------+-------+
mysql> select * from david_lin;
+----+--------+---------+
| id | myname | mymoney |
+----+--------+---------+
|  1 | david  |  100000 |
|  2 | rocky  |  200000 |
+----+--------+---------+


? ? ? ?② 编写导出脚本
? ? ? ? ??
? ? ? ? ? ? 每行数据中执行的 Redis命令如下:
? ? ? ? ? ? HSET david_lin [myname] [mymoney]

[root@odd ~]# cat mysql_to_redis.sql 
SELECT CONCAT(
  "*4\r\n",
  '$', LENGTH(redis_cmd), '\r\n',
  redis_cmd, '\r\n',
  '$', LENGTH(redis_key), '\r\n',
  redis_key, '\r\n',
  '$', LENGTH(hkey), '\r\n',
  hkey, '\r\n',
  '$', LENGTH(hval), '\r\n',
  hval, '\r'
)
FROM (
  SELECT
  'HSET' AS redis_cmd,
  'david' AS redis_key,
  myname AS hkey,
  mymoney AS hval
  FROM david_lin
) AS t


? ? ? ?③ 开始导入

[root@odd ~]# mysql -uroot -poracle test --skip-column-names --raw   <br>  <br>? ? ? ?④ 在Redis 里查询 <p>  </p><pre class="brush:php;toolbar:false">redis 127.0.0.1:6379> hgetall david
1) "david"
2) "100000"
3) "rocky"
4) "200000"


? ? 这里仅是个 demo、数据量小、不过、看这结果、有些类似行转列哈、列运算了、有木有 :)


? ??
? ? By David Lin
? ? 2013-05-30
? ? Good Lucky


作者:linwaterbin 发表于2013-5-30 22:11:24 原文链接

阅读:82 评论:0 查看评论

浅谈 Redis 与 MySQL 的耦合性以及利用管道完成 MySQL 到 Redis

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.