首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP echo 5000记录和取消设置

PHP echo 5000记录和取消设置
EN

Stack Overflow用户
提问于 2018-12-17 21:59:31
回答 2查看 107关注 0票数 -2

我想回显来自300000个大型数据集的记录。

回显前5000条记录,然后取消设置($data)并迭代,直到mysql表中的记录结束。

就像这样,

1)

代码语言:javascript
复制
 for ($i=0; $i < 5; $i++) {
        $data = openssl_random_pseudo_bytes(1000000);
        echo "peak_memory_usage = ” . memory_get_peak_usage(true) . “\n”;
        doSomething($data);

        //unset($data);

    }

    echo “for loop completed, memory now at ” . memory_get_usage(true) . “\n”;

    function doSomething($data) {
        echo “size:” . strlen($data) . “\n”;
    }

或者像这样的东西?

2)

代码语言:javascript
复制
 nRows = $pdo->query('select count(*) from employees')->fetchColumn();

        $users = new ArrayIterator(range(1, nRows)); // nRows are 3000000 test records
        foreach(new LimitIterator($users, 0, 50000) as $u) {
          echo $u, "\n";
        }

3) @Sameer你是否愿意将你的建议添加到下面的查询中,我可能做了一些错误的事情来添加我们的睡眠-我的编码缺陷,这会导致在添加usleep时出现超时问题。

代码语言:javascript
复制
 $data = $DB->query("SELECT * FROM user_details")->fetchAll();

        foreach ($data as $row) {
          echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br />\n";
        }

第三个选项可以很好地工作,50,000个记录不是很多的RAM负载,而是CPU负载,有没有办法优化它来减少CPU的负载,想象一下如果30个人运行同样的查询,它将占用CPU的最大值?如果我添加usleep(10) -它会回显记录,但在最后会出现一个错误,显示超时。

如有任何建议,欢迎光临。

非常感谢你阅读我的帖子。

  • Modified原始帖子的目的是相同的,以减少服务器负载。如果你喜欢我的长篇文章,请投票给我的朋友们,我落后了很多,我想成为future.

的贡献者

我偶然发现了一个通过(Dm4Web)数据加载的令人惊叹的解决方案-令人惊叹的解决方案-但需要添加HTML tables/append或附加结果。

Splitting an AJAX call which returns 5000 rows into multiple AJAX calls with 100 rows

尝试运行以下脚本时,第49行出现错误,未捕获SyntaxError:意外标识:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<title>SQL Batch List AJAX and jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<div id="mainform">
<h2>Fetch REcords 5000 at a time</h2>
<div id="listData">
<div>
<input id="load" name="load" type="button" value ="Load Data">
<input id="cancel" name="cancel" type="button" value ="Cancel">
</div>
</div>
</div>
</body>
<script>

// counter that allows you to get a new set of rows
    var step = 0;
    // set variable if you want to restrict the number of rows will be loaded
    var maxStep = 0;//
    // how many rows should be returned
    var count = 5000;
    // if the cancel button is pressed
    var cancel = false;

    $(function() {

        $('#load').click(function(){

            getData();
        })

        $('#cancel').click(function(){
            cancel = true;
        })
    });

    function getData()
    {
        step++;

        //If cancel variable is set to true stop new calls
        if(cancel == true) return;
        // checks if the variable is set and limits how many rows to be fetched
        if(maxStep >0 $$ step >= maxStep)


        $.post('ajax.php'
        ,{
            'step':step,
            'count':count,
        }
        ,function(data, textStatus, jqXHR){   

        if(textStatus == "success")
        alert("Data: " + data);
                /*  foreach (data as $row) {

          echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br />\n"; 
        } */
        if(textStatus == "error")
      alert("Error: " + jqXHR.status + ": " + jqXHR.statusText);


             // when it finishes processing the data, call back function
             getData();

        }
        ,'json'
    )       
}
</script>
</html>


    ==== ajax.php  =====


    step = 0;
    if(isset($_POST['step'])) $step = (int)$_POST['step'];

    $count = 0;
    if(isset($_POST['count'])) $count = (int)$_POST['count'];



    if($step>0 and $count>0)
    {
        $offset = ($step-1) * $count;        
        $limit = $offset.','.$count;

        // --------------        
        // your code here
        // --------------

        $data = $DB->query("SELECT * FROM user_details LIMIT .$limit")->fetchAll();
        $result = mysql_query($sql);
        $arr_result = array();
        foreach ($data as $row) {
               $arr_result[] = $row;
            }

        $arr_result_enc = json_encode($arr_result);
        echo $arr_result_enc;

        // echo rows
        //echo json_encode($rows);        
    }

方法4)

代码语言:javascript
复制
$query = "SELECT COUNT(*) as num FROM employees";

  //$select_run = mysqli_query($conn, $select);
$result = mysqli_query($conn, $query) or die(mysql_error());
$row = mysqli_fetch_array($result);
$itemcount = $row['num']; // Roughly 300,000 total items


$batches = $itemcount / 2000; // Number of while-loop calls - around 120.
for ($i = 0; $i <= $batches; $i++) {
  $offset = $i * 2000; // MySQL Limit offset number
  $query = "SELECT first_name,last_name FROM employees  LIMIT 2500, $offset ";
  $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
  while ($row = mysqli_fetch_array($result)) {
     echo $row['first_name'];
  }

  echo "<BR>";
  echo "Run Number: ".$i."<br />";
  echo "<BR>";
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53816767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档