douxing9641 2019-02-14 09:23
浏览 111
已采纳

将单个数字除以范围

I can't think a way in PHP to divide number into ranges.

I have massive MySQL database record set and want to spread some workload for my scripts.

Example:

There is 2435 rows in table, I want that each PHP script select only specific range of records:

Script 1: select [1 to 150];
Script 2: select [151 to 270];

The main problem: I can't think a method how to divide that 2435 into even ranges and pass them into MySQL SELECT.

  • 写回答

2条回答 默认 最新

  • dsbfbz75185 2019-02-20 09:43
    关注

    So, for a few days I was testing how offset behaves with massive data sets (100+k rows) and it was terrible. Offset is insanely resource hungry and definitely not for the task if you have large number of rows in your database.

    My final code looks like this (abstraction):

    chunker.php

    // Divide table rows into 50 chunks and produce array
    $rows = DB::query("SELECT id FROM data_set GROUP BY id DESC");
    $chunks = array_chunk($rows, 50, TRUE);
    
    // Extract endings of each chunk array
    $ends = array();
    foreach ($chunks as $c) {
            $f = flattenArray($c);
            $arr_end = end($f);
            array_push($ends, $arr_end);
    }
    
    // Spawn separate PHP processes to work with a chunk array ending
    foreach ($ends as $e) {
        $bb = shell_exec("php -q worker.php '".$e."' > /dev/null &");
    }
    

    worker.php

    // Catch argv
    $exec_args = $_SERVER['argv'];
    
    // Select predefined amount of rows from DB which is more than or equal to argv value
    $data = DB::query("SELECT * FROM data_set WHERE id >= %i LIMIT 50", $exec_args[1]);
    
    foreach ($data as $d) {
     // Do you stuff here
    }
    

    This adaptation came from this article http://mysql.rjweb.org/doc.php/pagination

    Performance wise I offseting data required 8 CPU cores with 32 GB RAM. With LIMIT method I only need 4 GB RAM and 2 CPU's. So, think twice before using offset in LARGE data sets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要