php echo 5000条记录并取消设置

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

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

  1. for ($i=0; $i < 5; $i++) {
  2. $data = openssl_random_pseudo_bytes(1000000);
  3. echo "peak_memory_usage = ” . memory_get_peak_usage(true) . “\n”;
  4. doSomething($data);
  5. //unset($data);
  6. }
  7. echo “for loop completed, memory now at ” . memory_get_usage(true) . “\n”;
  8. function doSomething($data) {
  9. echo “size:” . strlen($data) . “\n”;
  10. }

或者类似的?
2)

  1. nRows = $pdo->query('select count(*) from employees')->fetchColumn();
  2. $users = new ArrayIterator(range(1, nRows)); // nRows are 3000000 test records
  3. foreach(new LimitIterator($users, 0, 50000) as $u) {
  4. echo $u, "\n";
  5. }


3) @sameer您是否想将您的建议添加到下面的查询中,我可能在添加usleep我的编码缺陷时出错,这会导致添加usleep时超时的问题。

  1. $data = $DB->query("SELECT * FROM user_details")->fetchAll();
  2. foreach ($data as $row) {
  3. echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br />\n";
  4. }

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

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SQL Batch List AJAX and jQuery</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  6. </head>
  7. <body>
  8. <div id="mainform">
  9. <h2>Fetch REcords 5000 at a time</h2>
  10. <div id="listData">
  11. <div>
  12. <input id="load" name="load" type="button" value ="Load Data">
  13. <input id="cancel" name="cancel" type="button" value ="Cancel">
  14. </div>
  15. </div>
  16. </div>
  17. </body>
  18. <script>
  19. // counter that allows you to get a new set of rows
  20. var step = 0;
  21. // set variable if you want to restrict the number of rows will be loaded
  22. var maxStep = 0;//
  23. // how many rows should be returned
  24. var count = 5000;
  25. // if the cancel button is pressed
  26. var cancel = false;
  27. $(function() {
  28. $('#load').click(function(){
  29. getData();
  30. })
  31. $('#cancel').click(function(){
  32. cancel = true;
  33. })
  34. });
  35. function getData()
  36. {
  37. step++;
  38. //If cancel variable is set to true stop new calls
  39. if(cancel == true) return;
  40. // checks if the variable is set and limits how many rows to be fetched
  41. if(maxStep >0 $$ step >= maxStep)
  42. $.post('ajax.php'
  43. ,{
  44. 'step':step,
  45. 'count':count,
  46. }
  47. ,function(data, textStatus, jqXHR){
  48. if(textStatus == "success")
  49. alert("Data: " + data);
  50. /* foreach (data as $row) {
  51. echo $row['username']." -- ID :" .$row['user_id']. " -- FirstName :" .$row['first_name']. "<br />\n";
  52. } */
  53. if(textStatus == "error")
  54. alert("Error: " + jqXHR.status + ": " + jqXHR.statusText);
  55. // when it finishes processing the data, call back function
  56. getData();
  57. }
  58. ,'json'
  59. )
  60. }
  61. </script>
  62. </html>
  63. ==== ajax.php =====
  64. step = 0;
  65. if(isset($_POST['step'])) $step = (int)$_POST['step'];
  66. $count = 0;
  67. if(isset($_POST['count'])) $count = (int)$_POST['count'];
  68. if($step>0 and $count>0)
  69. {
  70. $offset = ($step-1) * $count;
  71. $limit = $offset.','.$count;
  72. // --------------
  73. // your code here
  74. // --------------
  75. $data = $DB->query("SELECT * FROM user_details LIMIT .$limit")->fetchAll();
  76. $result = mysql_query($sql);
  77. $arr_result = array();
  78. foreach ($data as $row) {
  79. $arr_result[] = $row;
  80. }
  81. $arr_result_enc = json_encode($arr_result);
  82. echo $arr_result_enc;
  83. // echo rows
  84. //echo json_encode($rows);
  85. }

方法4)

  1. $query = "SELECT COUNT(*) as num FROM employees";
  2. //$select_run = mysqli_query($conn, $select);
  3. $result = mysqli_query($conn, $query) or die(mysql_error());
  4. $row = mysqli_fetch_array($result);
  5. $itemcount = $row['num']; // Roughly 300,000 total items
  6. $batches = $itemcount / 2000; // Number of while-loop calls - around 120.
  7. for ($i = 0; $i <= $batches; $i++) {
  8. $offset = $i * 2000; // MySQL Limit offset number
  9. $query = "SELECT first_name,last_name FROM employees LIMIT 2500, $offset ";
  10. $result = mysqli_query($conn,$query) or die(mysqli_error($conn));
  11. while ($row = mysqli_fetch_array($result)) {
  12. echo $row['first_name'];
  13. }
  14. echo "<BR>";
  15. echo "Run Number: ".$i."<br />";
  16. echo "<BR>";
  17. }
cuxqih21

cuxqih211#

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

toe95027

toe950272#

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

  1. for ($i=0; $i < 5; $i++) {
  2. usleep(100);
  3. $data = openssl_random_pseudo_bytes(1000000);
  4. }

相关问题