繁体   English   中英

每行插入到MySQL数据库所需的时间

[英]Time Taken for each row to be inserted to MySQL database

嗨,我有要上传到Mysql数据库的数据,我需要获取插入每一行所花费的时间,以便可以将该数字传递给进度条。 我已经尝试通过确定插入所影响的行数来实现此目的,然后找到该数的百分比,这不是执行此操作的正确方法。

这是代码

     $result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from Profusion.source_cdr") or      die(mysql_error());
     $progress=mysql_affected_rows();
     // Total processes
     $total = $progress;
     // Loop through process
     for($i=1; $i<=$total; $i++){
     // Calculate the percentage
     $percent = intval($i/$total * 100)."%";
     echo $percent;

这实际上将总行数除以1并乘以100得到百分比,这是错误的。

我需要插入每一行所花费的时间,然后找到该百分比。

非常感谢您的帮助。

在这种情况下,确定一次插入的时间并不容易。 因为普通插入INSERT INTO my_table VALUES(2,4,5)INSERT INTO my_Table SELECT foo,bar,zoo FROM my_other_table

在您的情况下,mysql服务器将尝试将两个表都保留在内存中,如果表很大,则需要大量内存。 查看此博客文章: 更快:重新访问MySQL中的十亿行以获取一些详细信息。

无论如何,在上面的脚本中,从mysql_query返回之后; 查询已执行 ,这意味着您的百分比将执行查询计算。

编辑:一种解决方案,它将表中的条目分块。 解决方案的伪代码应如下所示:

 mysql_query("SELECT count(*) FROM my_other_table");
 $total_count = get_count_from_query_result();
 $one_percent = intval($total_count/100);
 for($i=0; $i<$total_count; $i += $one_percent)
 {
   mysql_query("INSERT INTO my_Table SELECT foo,bar,zoo FROM my_other_table LIMIT $i, $one_percent");
   increment_progress_bar();
 }

暂无
暂无

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

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