mysql从temp_table复制到表的值

时间:2013-05-19 15:58:28

标签: php mysql insert

1有2个表,temp_table和表,两者都是MyISAM

temp_table(经常写,每分钟运行一次cron作业插入数据)

id(auto_increment),title(varchar(200)),content(varchar(500)),date(DATETIME), mykey(tinyint(1) Default 1)

表(经常阅读,自定义搜索,每隔10分钟使用cron作业从temp_table更新)

id(int(11)),title(varchar(200)),content(varchar(500)),date(DATETIME), hour(smallint(2))

现在,数据 ~300MB / table ,每次更新都会传输 2,000-3,000行

require dirname(__FILE__) . '/../connection.php';
mysql_select_db("news",$connextion);
mysql_query("SET NAMES utf8");
$query = mysql_query("INSERT INTO table (hour,id,title,content,date) values '".date('H')."',SELECT id, title,content,date from temp_table where mykey = '1'");
$query = mysql_query("UPDATE temp_table SET mykey='2' WHERE mykey = '1'");
//if data has transfer, update mykey='2' for a mark.
mysql_close($connextion);

但是没有INSERT INTO table,如何编写正确的mysql查询并且这种方式更有效率?我需要更少的mysql连接时间和更低的内存使用率。感谢。

PS:temp_table有一些规则可以检查标题是否重复,因此temp_table无法删除,如果日期是1个月前,表格将每天清除。

1 个答案:

答案 0 :(得分:0)

尝试使用$query = mysql_query("INSERT INTO table (hour,id,title,content,date) values ('".date('H')."',SELECT id, title,content,date from temp_table where mykey = '1'")) or die('error while inserting');