dtwk6019 2013-12-18 11:46
浏览 22

要插入MySQL数据库的每一行所用的时间

Hi I have data to upload to a Mysql database and I need to get the time taken for each row to be inserted so that I can pass that number through to a progress bar. I have alreay tried accomplishing this by determining the number of rows affetced by the insertion then find the percentage of that number which is not the correct manner to do this.

here is the code

     $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;

this actually divides the total number of rows by 1 and multiplies by 100 to get the percentage and this is wrong .

I need the time taken for each row to be inserted and then find the percentage of that.

Your help will be highly appreciated.

  • 写回答

1条回答 默认 最新

  • douyu4822 2013-12-18 12:43
    关注

    It is not easy to determine the time of one insertion in this case; because normal insertion INSERT INTO my_table VALUES(2,4,5)is different from INSERT INTO my_Table SELECT foo,bar,zoo FROM my_other_table.

    In your case, the mysql server will try to keep both tables in memory, which need a lot of memory if tables are big. Check this blog post: Even faster: loading half a billion rows in MySQL revisited for some details.

    Anyway, in the above script, after returning from mysql_query; the query is ALREADY EXECUTED which means you percentage will count after the query is executed.

    EDIT: A solution it to chunk the entries in the table into chunks. Pseudo code of the solution shall be like this:

     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();
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂