最高效的MySQL查询从相同的内存表更新表(Most Efficient MySQL query to update a table from identical memory table)

编程入门 行业动态 更新时间:2024-10-06 23:29:18
高效的MySQL查询从相同的内存表更新表(Most Efficient MySQL query to update a table from identical memory table)

我正在为一个看起来像这样的表(简化)实现一个内存缓存:

Item1(整数),Item2(整数),cnt(整数)

原始表包含数百万对这样的对。 它会迅速更新。

为了提高效率,我想将新对写入相同的内存表,并通过cron定期更新磁盘上的实际表。

cron应该执行以下操作:对于每对,如果非内存表中存在类似的对,则将计数增加内存表中的计数。 如果不存在这样的对,则使用内存表中的count来创建它。

如何使刷新(从内存表到实际表)最有效?

注意:环境是Mysql 5.0.45 PHP 5.2.6 CentOS

I'm implementing a memory cache for a table that looks like this (simplified):

Item1 (integer), Item2 (integer), cnt (Integer)

The original table includes millions of pairs like this. and it updates rapidly.

To make it all more efficient I want to write new pairs to an identical memory table and update the real table on disk periodically by cron.

The cron should do the following: for each pair if there is similar pair in the non-memory table increase the count by the count from the memory table. If no such pair exist create it with count from the memory table.

How can I make the flush (from memory table to real table) most efficient?

Notes: The environment is Mysql 5.0.45 PHP 5.2.6 CentOS

最满意答案

您可以使用INSERT ... ON DUPLICATE KEY UPDATE查询 - 但这取决于主表上的主键或UNIQUE索引。

INSERT INTO <<master_table>> (Item1, Item2, cnt) SELECT Item1, Item2, cnt FROM <<memory_table>> ON DUPLICATE KEY UPDATE cnt = cnt + VALUES(cnt);

You could use a INSERT ... ON DUPLICATE KEY UPDATE query - but that depends on the primary keys or UNIQUE indexes on your master table.

INSERT INTO <<master_table>> (Item1, Item2, cnt) SELECT Item1, Item2, cnt FROM <<memory_table>> ON DUPLICATE KEY UPDATE cnt = cnt + VALUES(cnt);

更多推荐

本文发布于:2023-07-19 15:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高效   内存   Efficient   query   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!