将单个数字除以范围

我无法用PHP的方式将数字划分为范围。</ p>

< 我有大量的MySQL数据库记录集,并希望为我的脚本分散一些工作量。</ p>

示例:</ p>

表中有2435行, 我希望每个PHP脚本只选择特定范围的记录:</ p>

 脚本1:选择[1到150]; 
脚本2:选择[151到270];
</ code> </ pre>

主要问题:我想不出一种方法如何将2435划分为偶数范围并将它们传递给MySQL SELECT。 </ p>
</ div>

展开原文

原文

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.

dou12352
dou12352 是否有任何规则要划分行?以自然的,非技术的方式写下来可能有助于理解问题
一年多之前 回复
dpleylxzx47207117
dpleylxzx47207117 MYSQLLIMIT,OFFSET怎么样?
一年多之前 回复

2个回答

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.



尝试这样 - </ p>

  SELECT * FROM table_name ORDER BY id ASC LIMIT  150 OFFSET 0 
</ code> </ pre>

对于第一个脚本,</ p>

  LIMIT 150 OFFSET 0 
</ code> </ pre>

然后是第二个脚本</ p>

  LIMIT 150 OFFSET 150 
</ code> </ pre>

所以在 ...... </ p>
</ div>

展开原文

原文

Try Like this way-

SELECT * FROM table_name ORDER BY id ASC LIMIT 150 OFFSET 0

For 1st script,

LIMIT 150 OFFSET 0

then For 2nd script

LIMIT 150 OFFSET 150

So on...

douzhuo2002
douzhuo2002 这个解决方案似乎很好,但表中的总行数是动态值,并且可以显着增加。 我需要一些方法将动态值划分为范围。 今天它是5000,明天它是10756.嗯,我猜我可以将动态行计数分成块,并产生尽可能多的具有静态偏移的实例,我需要为每个脚本。 一旦实现,我将发布完整的解决方案。
一年多之前 回复
doumi1311
doumi1311 做先生..
一年多之前 回复
duanpie2834
duanpie2834 你能从你的第二个和第三个例子中删除逗号吗? 它们会导致语法错误。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问