简体   繁体   English

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

[英]Autoload MEMORY table from another MyISAM table

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? 我的问题是:服务器关闭后,如何从另一个MyISAM表中自动加载MEMORY表?

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. 每次想在MEMORY中查找时,我都可以检查行数,然后通过PHP脚本逐行加载,但是从MyISAM中读取2,000,000条记录并保存到MEMORY中需要花费时间。 and checking rows count. 并检查行数。 although it is not thread safe and I should take care of it. 尽管它不是线程安全的,但我应该注意这一点。

Is there any better way? 有什么更好的办法吗?

It appears you can also configure MySQL to automatically run certain statements on startup using the init_file option: 看来您还可以使用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/zh-CN/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 http://dev.mysql.com/doc/refman/5.6/en/create-event.html

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. 最简单的方法是修改正在启动MySQL的任何脚本(mysqld_safe?),并使其运行“ mysql”以发出命令来运行存储表的操作。 I don't believe MySQL has anything internally that you can trigger automatically at startup, or any kind of internal job scheduler. 我认为MySQL在内部没有任何东西可以在启动时自动触发,也没有任何内部作业调度程序。

I have solved this problem in the following way. 我已经通过以下方式解决了这个问题。 1. Create a script on php which is set to cron. 1.在php上创建一个脚本,将其设置为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 参考: http : //dev.mysql.com/doc/refman/5.0/en/load-data.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM