简体   繁体   English

无论如何,是否有必要检查php mysql中“ SELECT…INTO OUTFILE”的执行时间(已用或剩余)?

[英]Is there anyway to check the execution time(elapsed or remaining) for the “SELECT… INTO OUTFILE” in php mysql?

I am writing a query, which fetches many millions of rows from a table and puts in to a txt file. 我正在写一个查询,该查询从表中提取数百万行并将其放入txt文件。

$sQuery  = " ( ";
        $sQuery .= " SELECT number ";
        $sQuery .= " INTO OUTFILE '".$outputfile_dnd."' ";
        $sQuery .= " FIELDS TERMINATED BY ','  ";   
        $sQuery .= " LINES TERMINATED BY '\r\n' ";
        $sQuery .= " FROM `".$database."`.$temp_table_name";
        $sQuery .= " WHERE number IN (SELECT number FROM `db_dnd`.`dnd` )";
        $sQuery .= ")";

Since there will be huge data, this will take several minutes of time to complete sometimes. 由于将有大量数据,因此有时需要几分钟的时间才能完成。 I want to make the users to know how much more time the process will run(roughly). 我想让用户知道该过程将运行多少时间(大约)。 I am thinking to keep a button in frontend, when user clicks the button, it should show the time remaining. 我想在前端保留一个按钮,当用户单击该按钮时,它应该显示剩余时间。 If its not possible to findout remaining time, If i can get the elapsed time and number of rows selected,then also its fine. 如果无法找出剩余时间,如果我可以获取经过的时间和所选的行数,那么也可以。 Any help? 有什么帮助吗? Worst case I am thinking to go with INSERT IN TO other table then put to file after everything is inserted. 最糟糕的情况是我想将INSERT IN TO插入其他表,然后在插入所有内容后将其保存到文件中。

You can run an ajax request to the php script when the user presses the button. 当用户按下按钮时,可以向php脚本运行ajax请求。 Then you can count in Javascript, the time until you get a response from the ajax file. 然后,您可以使用Javascript计数,即从ajax文件获得响应之前的时间。

To get the time elapsed in php you can do (the following assumes you are using mysqli): 要获取经过php的时间,您可以执行以下操作(以下假设您正在使用mysqli):

$startTime = microtime();
$result = $mysqli->query($sQuery); #This is your function that runs the actual query
$endTime= microtime(true);
$timeTaken= $endTime- $startTime;

To get the number of rows (in mysqli) you can just grab it off of the result object. 要获取行数(在mysqli中),您可以从结果对象中获取它。

$numRows = $result->num_rows;

Edit for Js ajax version : 编辑Js ajax版本:

So if you have Jquery on your frontend the following would work. 因此,如果前端有Jquery,则可以使用以下方法。 So this involves separating all of your php that runs and displays the query into a separate file. 因此,这涉及将运行并显示查询的所有php分离到一个单独的文件中。 Then call the URL of that file below and it will show how long has elapsed before the output of said file. 然后在下面调用该文件的URL,它将显示在输出该文件之前已经经过了多长时间。

<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
    var startTime = new Date().getTime();
    var requestInterval = window.setInterval(function() {
        $("#content_area").html("Time Elapsed :"+(new Date().getTime() - startTime) / 1000+" Seconds");
    }, 500);
    jQuery.get('URLofYOURScriptHERE', 
        function(data, status, xhr) {
            clearInterval(requestInterval)
            var timeTaken = new Date().getTime() - startTime;
            $("#content_area").html(data);
        }
    );
});
</script>
<body>
<div id="content_area">
</div>
</body>

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

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