从另一个 MyISAM 表自动加载 MEMORY 表

发布于 2024-11-30 09:51:45 字数 212 浏览 2 评论 0原文

为了提高服务器性能,我决定使用内存表来更快地读取用户信息。

我的问题是:服务器关闭后如何从另一个 MyISAM 表自动加载 MEMORY 表?

每次我想在内存中查找时,我都可以检查行数,然后通过 PHP 脚本逐行加载,但从 MyISAM 读取 2,000,000 条记录并保存在内存中需要时间。并检查行数。尽管它不是线程安全的,我应该照顾它。

还有更好的办法吗?

To increase server performance, I decided to use memory table for faster reading user's info.

My question is: How to auto load MEMORY table from another MyISAM table after server shutdown?

I can check rows count every time I want to lookup in MEMORY and then, load row by row by a PHP script, but it takes time to read 2,000,000 records from MyISAM and save in MEMORY. and checking rows count. although it is not thread safe and I should take care of it.

Is there any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-12-07 09:51:45

最简单的方法是修改启动 MySQL 的任何脚本(mysqld_safe?)并让它运行“mysql”以发出命令来运行存储过程,该存储过程会填充您的表。我不相信 MySQL 内部有任何可以在启动时自动触发的东西,或者任何类型的内部作业调度程序。

Simplest would be to modify whatever script is starting MySQL (mysqld_safe?) and have it run 'mysql' to issue a command to run a stored proc which does your table populating. I don't believe MySQL has anything internally that you can trigger automatically at startup, or any kind of internal job scheduler.

-残月青衣踏尘吟 2024-12-07 09:51:45

我通过以下方式解决了这个问题。
1. 在 php 上创建一个脚本,并将其设置为 cron。
草稿版本:

$connection = new PDO(....)
$sql = "Select id from table_memory limit 1";
$result = $connection->query($sql)->fetchColumn();
if (!$result) {
    $sql = "select * into outfile '/path' from main_table";
    $connection->exec($sql);
    if (file_exists('/path')) {
       $sql = "LOAD DATA INFILE '/path' INTO table_memory";
       $connection->exec($sql);
    }
}

//如有必要进行最终检查并发送邮件或其他通知

参考文献:
http://dev.mysql.com/doc/refman/5.0 /en/load-data.html

I have solved this problem in the following way.
1. Create a script on php which is set to cron.
Draft version:

$connection = new PDO(....)
$sql = "Select id from table_memory limit 1";
$result = $connection->query($sql)->fetchColumn();
if (!$result) {
    $sql = "select * into outfile '/path' from main_table";
    $connection->exec($sql);
    if (file_exists('/path')) {
       $sql = "LOAD DATA INFILE '/path' INTO table_memory";
       $connection->exec($sql);
    }
}

//final checking if necessary and send mail or other notification

References:
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

愁以何悠 2024-12-07 09:51:45

看来您还可以使用 init_file 选项将 MySQL 配置为在启动时自动运行某些语句:

http://dev.mysql.com/doc/refman/5.6/en/server-options.html#option_mysqld_init-file

你可以告诉它运行一个文件填充您的内存表。

如果您需要偶尔更新内存表,另一个考虑因素是使用事件:

http://dev.mysql.com/doc/refman/5.6/en/create-event.html

It appears you can also configure MySQL to automatically run certain statements on startup using the init_file option:

http://dev.mysql.com/doc/refman/5.6/en/server-options.html#option_mysqld_init-file

You could tell it to run a file that populates your memory table.

Another consideration, if you need to occasionally update your memory table is to use events:

http://dev.mysql.com/doc/refman/5.6/en/create-event.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文