【问题标题】:how to convert a mysql table into redis如何将mysql表转换为redis
【发布时间】:2026-02-04 10:50:01
【问题描述】:

我有一个包含超过 1000 万条条目的 MySQL 表“number_records”。该表具有以下结构。

id | number     | cat_id | dialed | uuid |

1  | 2123727345 | 148    |   0    |      |

2  | 2123727346 | 148    |   0    |      |

3  | 212372737  | 147    |   0    |      |

我有两个具有相同 cat_id(148) 但不同 UUID 的进程同时使用上表获取数字。

第一个进程 uuid = abcd-efgh-12345-78908

第二道工序 uuid = zxcv-qwrt-uuuu-kklll

我希望每个进程只获得一个唯一的号码,并在处理后将其拨号状态设置为“1”。 (两个进程得到的编号不同)

我在每个进程的三个查询中执行上述操作,以便每个进程仅获得一个唯一编号。

对于第一个过程。

  1. update numbers_records set uuid = 'abcd-efgh-12345-78908' where cat_id = 148 and dialed = 0 and uuid = "";

  2. select number from numbers_records where uuid = 'abcd-efgh-12345-78908' and dialed = 0 and cat_id = 148;

  3. update numbers_records set dialed = 1 where uuid ='abcd-efgh-12345-78908' and cat_id = 148 and dialed = 0;

对于第二个过程。

  1. update numbers_records set uuid = 'zxcv-qwrt-uuuu-kklll' where cat_id = 148 and dialed = 0 and uuid = "";

  2. select number from numbers_records where uuid = 'zxcv-qwrt-uuuu-kklll' and dialed = 0;

  3. update numbers_records set dialed = 1 where uuid = 'zxcv-qwrt-uuuu-kklll'

此过程运行良好。上面的查询确保我得到每个进程一个唯一的数字。

但表包含超过 1000 万条记录。而这些查询耗时超过 5 秒。我想加快这个过程。

我的经理让我将上表转移到 Redis。我很困惑如何在表上转移。

在上述情况下如何使用 Redis?

非常感谢任何建议。 最好的祝福。

【问题讨论】:

    标签: mysql redis


    【解决方案1】:

    看起来您正在使用 MySql 表作为任务队列。您可以使用 Redis LISTLPOPBLPOP 操作来获取工作人员中的下一个任务,并使用 HASH 来存储任务信息。此外,如果您需要基于cat_id 进行处理,您需要将任务推送到不同的列表:

    ... number_records_tasks:147 number_records_tasks:148 ...

    要一次获得多个任务,请使用来自 question 的建议。

    【讨论】:

      【解决方案2】:

      由于您没有删除此表中的条目,因此任何仅基于类似队列结构的解决方案都不会相等 - 在处理队列中的项目后,您将丢失它。

      您唯一能做的就是在 MySQL 表之上添加其他机制来处理工作队列(redis 列表、rabbitmq、beantalkd)。

      在我看来,您应该先检查其他解决方案,它们可能更容易实施:

      【讨论】: