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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to delete data from MySQL table using PHP? How to delete data from MySQL table using PHP? Jun 05, 2024 pm 12:40 PM

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

How to set up MySQL connection pool using PHP? How to set up MySQL connection pool using PHP? Jun 04, 2024 pm 03:28 PM

Setting up a MySQL connection pool using PHP can improve performance and scalability. The steps include: 1. Install the MySQLi extension; 2. Create a connection pool class; 3. Set the connection pool configuration; 4. Create a connection pool instance; 5. Obtain and release connections. With connection pooling, applications can avoid creating a new database connection for each request, thereby improving performance.

How to improve application performance using pipes in Go? How to improve application performance using pipes in Go? Jun 05, 2024 pm 05:10 PM

Pipes in Go are a communication mechanism used to safely and efficiently transfer data between goroutines to improve application performance. There are two types of pipeline operations: Unbuffered: data must be sent and received synchronously. Buffered: The pipe has allocated storage space, allowing asynchronous send and receive. Example: When calculating the Fibonacci sequence, pipelines are used to communicate between the main goroutine and the calculation goroutine, thereby achieving concurrent calculations and significantly improving performance.

Which country is the Nexo exchange from? Where is it? A comprehensive introduction to the Nexo exchange Which country is the Nexo exchange from? Where is it? A comprehensive introduction to the Nexo exchange Mar 05, 2025 pm 05:09 PM

Nexo Exchange: Swiss cryptocurrency lending platform In-depth analysis Nexo is a platform that provides cryptocurrency lending services, supporting the mortgage and lending of more than 40 crypto assets, fiat currencies and stablecoins. It dominates the European and American markets and is committed to improving the efficiency, security and compliance of the platform. Many investors want to know where the Nexo exchange is registered, and the answer is: Switzerland. Nexo was founded in 2018 by Swiss fintech company Credissimo. Nexo Exchange Geographical Location and Regulation: Nexo is headquartered in Zug, Switzerland, a well-known cryptocurrency-friendly region. The platform actively cooperates with the supervision of various governments and has been in the US Financial Crime Law Enforcement Network (FinCEN) and Canadian Finance

Monitoring Redis Droplets Using Redis Exporter Service Monitoring Redis Droplets Using Redis Exporter Service Jan 06, 2025 am 10:19 AM

Effective monitoring of Redis databases is essential for maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a robust utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you establish a monitoring solution seamlessly. By following this tutorial, you’ll achieve a fully operational monitoring setup to effectively monitor the performance metrics of your Redis database.

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

See all articles