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

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

Jun 01, 2016 pm 01:34 PM
pipeline

bitsCN.com

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

 

  ㈠ Redis 与 MySQL 的耦合性

    

    在业务架构早期、我们便该"吃着碗里的看着锅里的"、切莫让MySQL 有梦、而Redis 无心

    毕竟、有些关系型的结构不适合放到Redis跑、"男女搭配、干活不累"嘛、推荐让MySQL与Redis喜结连理

    

    其次、这 2 人、一般是在不同场景做选择、而不会在性能上选择、

    只有在 2 者都可用的情况下、综合性能、硬件成本、运维成本等选择

    比如、网页游戏启用 Redis+MySQL:

    游戏中的:好友关系、排行榜、计数器、队列、cache都很适合通过 Redis来实现

    

    再举个例子是新浪微博的架构、比如用户关注关系:

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

 

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

 

    大体流程是由 MySQL 复制到 Redis 的

    基本结构应该是:

    1. 发微博-- > 进入消息队列-- > 存入MySQL-- > 复制到Redis

    2. 查询 -- > 查询缓存-- > 查询Redis -- > 查询MySQL

 

    ㈡ 快速迁移 MySQL →→ Redis 

 

       ① MySQL 要导出的表 david_lin

 

[plain] 

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]

[plain] 

[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  

 

       ③ 开始导入

[plain] 

[root@odd ~]# mysql -uroot -poracle test --skip-column-names --raw

All data transferred. Waiting for the last reply...  

Last reply received from server.  

errors: 0, replies: 0  

 

       ④ 在Redis 里查询

[plain] 

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

bitsCN.com
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

How to use pipes to read and write files in Golang? How to use pipes to read and write files in Golang? Jun 04, 2024 am 10:22 AM

File reading and writing through pipes: Create a pipe to read data from the file and pass it through the pipe Receive the data from the pipe and process it Write the processed data to the file Use goroutines to perform these operations concurrently to improve performance

Introduction to Linux pipeline commands and basic usage Introduction to Linux pipeline commands and basic usage Feb 22, 2024 pm 05:57 PM

The pipe command in Linux is a powerful tool that can use the output of one command as the input of another command to realize data transmission and processing between different commands. This article will introduce the basics of pipe commands in Linux, as well as some common usage and code examples. Introduction to pipeline commands In Linux systems, pipeline commands use the vertical bar symbol (|) to connect two or more commands, for example: command1|command2. In this way, the output of command1 will be as command2

Use Linux pipelines to improve work efficiency Use Linux pipelines to improve work efficiency Feb 22, 2024 pm 09:30 PM

In today's information society, computers have become an indispensable tool in our work and life. As a staff member who is proficient in using Linux systems, it is very important to use the powerful functions of Linux to improve work efficiency. This article will focus on how to use the important function of pipes (Pipes) in Linux to simplify the work process and improve work efficiency. A Linux pipe is a special file type that can pass the output of one command directly to another command without storing the intermediate results.

The principle of golang function and pipeline communication The principle of golang function and pipeline communication May 04, 2024 pm 06:36 PM

In Go language, functions and pipes are used together to achieve inter-process communication. Functions can pass pipes as parameters to send or receive data through pipes. Pipes are unbuffered channels that can be used to send and receive data between goroutines and support both undirected and directed pipes. Used when sending data

How to implement timeout mechanism using pipeline in Go language? How to implement timeout mechanism using pipeline in Go language? Jun 03, 2024 pm 03:01 PM

Use a pipe to implement a timeout mechanism: Create a pipe. Create a goroutine to wait for elements in the pipeline. In another goroutine, close the pipe after a specified time. Use a select statement to select the appropriate action to perform when a pipeline element arrives or times out.

Synchronization mechanism for golang pipeline and function communication Synchronization mechanism for golang pipeline and function communication May 02, 2024 pm 04:21 PM

The synchronization mechanism of pipeline and function communication in Go language is implemented through pipeline buffer blocking to ensure the order and safety of data transmission. Specifically: when the pipe is empty, receiving data will be blocked. When the pipe is full, sending data will be blocked. Practical case: Calculate the Fibonacci sequence and use pipelines to synchronize the transmission of calculation results.

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.

Improvements and limitations of golang pipeline on function communication Improvements and limitations of golang pipeline on function communication May 04, 2024 am 10:36 AM

The Go language's pipeline is a concurrency primitive used for communication between goroutines: Create a pipeline: Use make(chantype) to create a pipeline type with send and receive channels. Send data: Use the send operator on the pipe (

See all articles