From: Jason Woofenden Date: Tue, 5 Feb 2019 06:31:58 +0000 (-0500) Subject: optimize and test db_reposition_respace() X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=19e2f5c79c22fd1585ca6569ad25e7ecf06ff7f0 optimize and test db_reposition_respace() --- diff --git a/db.php b/db.php index 2dccfbc..5a9ef89 100644 --- a/db.php +++ b/db.php @@ -466,10 +466,22 @@ function db_reposition_respace($table, $field, $where = '') { return; } $inc = floor(DB_ORD_MAX / ($c + 1)); - $cur = $inc; - foreach($ids as $id) { - db_update($table, $field, $cur, 'where id=%i', $id); - $cur += $inc; + $ord = $inc; + $count = count($ids); + for ($i = 0; $i < $count; $i += 1000) { + $values = []; + $j_max = min($count, $i + 1000); + for ($j = $i; $j < $j_max; ++$j) { + $id = $ids[$j]; + $values[] = "($id,$ord)"; + $ord += $inc; + } + $sql = + "insert into $table (id,$field) values " + . implode(',', $values) + . " on duplicate key update $field=VALUES($field)" + ; + db_send_query($sql); } } diff --git a/test/db_reposition.php b/test/db_reposition.php new file mode 100644 index 0000000..299974d --- /dev/null +++ b/test/db_reposition.php @@ -0,0 +1,93 @@ + $from) ? ($to - 1) : ($to), # adjust if moving into the part we moved in the first splice + 0, # don't delete anything + $moving # insert these + ); + + # check if it matches + if (($tests % 10) === 9) { + $db_ids = db_get_column('test_db_reposition', 'id', 'order by ord'); + for ($i = 0; $i < 10; ++$i) { + if ($ids[$i] != $db_ids[$i]) { + message('fail'); + return; + } + } + } + } + message("pass! $max_tests repositions checked every 10"); +}