php echo 5000条记录并取消设置

9bfwbjaz  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(391)

我想回显30万个大数据集的记录。
回显前5000条记录,而不是unset($data),并迭代到mysql表中记录的末尾。
像这样的事,
1)

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)

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我的编码缺陷时出错,这会导致添加usleep时超时的问题。

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

第三个3)选项可以很好地工作,没有太多的内存负载,但cpu,有没有办法优化这一点,以减少cpu的负载,想象一下,如果30人运行相同的查询,它将最大限度地利用cpu?如果我加上usleep(10),它会回显记录,但最后会有一个错误,叫timeout。
如有任何建议,我们将不胜感激。
非常感谢你阅读我的帖子。
修改原帖子的目的是为了减少服务器上的负载。。如果你喜欢我的长篇大论,请投票的家伙,我是落后的方式,我想成为一个贡献者在未来。
我偶然发现了一个惊人的解决方案(dm4web)数据加载-惊人的解决方案-但需要添加html表/append o并附加结果。

将返回5000行的ajax调用拆分为100行的多个ajax调用

第49行获取错误未捕获的语法错误:尝试运行以下脚本时出现意外标识:

<!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)

$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>";
}
cuxqih21

cuxqih211#

解决第三次尝试的方法:
选择所需的列,而不是选择*。
缓存结果,这样每个加载页面的人只需运行一次。
添加限制(分页),以便只选择前100或1000行,而不是50000行。当您同时加载50k行时,这也将阻止浏览器爆炸。

toe95027

toe950272#

$data 已经被覆盖了,所以这不是问题所在。
重循环在服务器上产生持续的张力,这会增加负载。
您可以添加 sleep 允许服务器释放资源和一些喘息时间,这将降低服务器负载。使用usleep并设置最佳微秒。

for ($i=0; $i < 5; $i++) {
    usleep(100);
    $data = openssl_random_pseudo_bytes(1000000);
}

相关问题