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

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

Jun 07, 2016 pm 04:37 PM
mysql redis use Finish pipeline

㈠ 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 |
+----+--------+---------+
Copy after login


? ? ? ?② 编写导出脚本
? ? ? ? ??
? ? ? ? ? ? 每行数据中执行的 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
Copy after login


? ? ? ?③ 开始导入

[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"
Copy after login


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


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


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

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

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

Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles