Delete queries are now split into chunks.

This commit is contained in:
Michael 2017-05-01 17:42:37 +00:00
parent e90ae79d35
commit b9b43e30e6
4 changed files with 35 additions and 19 deletions

View File

@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM', 'Friendica');
define ( 'FRIENDICA_CODENAME', 'Asparagus'); define ( 'FRIENDICA_CODENAME', 'Asparagus');
define ( 'FRIENDICA_VERSION', '3.5.2-dev' ); define ( 'FRIENDICA_VERSION', '3.5.2-dev' );
define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
define ( 'DB_UPDATE_VERSION', 1221 ); define ( 'DB_UPDATE_VERSION', 1222 );
/** /**
* @brief Constant with a HTML line break. * @brief Constant with a HTML line break.

View File

@ -768,7 +768,7 @@ class dba {
foreach ($structure['fields'] AS $field => $field_struct) { foreach ($structure['fields'] AS $field => $field_struct) {
if (isset($field_struct['relation'])) { if (isset($field_struct['relation'])) {
foreach ($field_struct['relation'] AS $rel_table => $rel_field) { foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
self::$relation[$rel_table][$rel_field][$table] = $field; self::$relation[$rel_table][$rel_field][$table][] = $field;
} }
} }
} }
@ -817,15 +817,17 @@ class dba {
// When the search field is the relation field, we don't need to fetch the rows // When the search field is the relation field, we don't need to fetch the rows
// This is useful when the leading record is already deleted in the frontend but the rest is done in the backend // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
if ((count($param) == 1) AND ($field == array_keys($param)[0])) { if ((count($param) == 1) AND ($field == array_keys($param)[0])) {
foreach ($rel_def AS $rel_table => $rel_field) { foreach ($rel_def AS $rel_table => $rel_fields) {
foreach ($rel_fields AS $rel_field) {
$retval = self::delete($rel_table, array($rel_field => array_values($param)[0]), true, $callstack); $retval = self::delete($rel_table, array($rel_field => array_values($param)[0]), true, $callstack);
$commands = array_merge($commands, $retval); $commands = array_merge($commands, $retval);
} }
}
} else { } else {
// Fetch all rows that are to be deleted // Fetch all rows that are to be deleted
$sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `". $sql = "SELECT ".self::$dbo->escape($field)." FROM `".$table."` WHERE `".
implode("` = ? AND `", array_keys($param))."` = ?"; implode("` = ? AND `", array_keys($param))."` = ?";
$retval = false;
$data = self::p($sql, $param); $data = self::p($sql, $param);
while ($row = self::fetch($data)) { while ($row = self::fetch($data)) {
// Now we accumulate the delete commands // Now we accumulate the delete commands
@ -833,10 +835,6 @@ class dba {
$commands = array_merge($commands, $retval); $commands = array_merge($commands, $retval);
} }
// When we don't find data then we don't need to delete it
if (is_bool($retval)) {
return $in_commit ? $commands : true;
}
// Since we had split the delete command we don't need the original command anymore // Since we had split the delete command we don't need the original command anymore
unset($commands[$key]); unset($commands[$key]);
} }
@ -848,6 +846,7 @@ class dba {
self::p("START TRANSACTION"); self::p("START TRANSACTION");
$compacted = array(); $compacted = array();
$counter = array();
foreach ($commands AS $command) { foreach ($commands AS $command) {
if (count($command['param']) > 1) { if (count($command['param']) > 1) {
$sql = "DELETE FROM `".$command['table']."` WHERE `". $sql = "DELETE FROM `".$command['table']."` WHERE `".
@ -860,12 +859,24 @@ class dba {
return false; return false;
} }
} else { } else {
$key_table = $command['table'];
$key_param = array_keys($command['param'])[0];
$value = array_values($command['param'])[0]; $value = array_values($command['param'])[0];
$compacted[$command['table']][array_keys($command['param'])[0]][$value] = $value;
// Split the SQL queries in chunks of 100 values
// We do the $i stuff here to make the code better readable
$i = $counter[$key_table][$key_param];
if (count($compacted[$key_table][$key_param][$i]) > 100) {
++$i;
}
$compacted[$key_table][$key_param][$i][$value] = $value;
$counter[$key_table][$key_param] = $i;
} }
} }
foreach ($compacted AS $table => $values) { foreach ($compacted AS $table => $values) {
foreach ($values AS $field => $field_values) { foreach ($values AS $field => $field_value_list) {
foreach ($field_value_list AS $field_values) {
$sql = "DELETE FROM `".$table."` WHERE `".$field."` IN (". $sql = "DELETE FROM `".$table."` WHERE `".$field."` IN (".
substr(str_repeat("?, ", count($field_values)), 0, -2).");"; substr(str_repeat("?, ", count($field_values)), 0, -2).");";
@ -877,6 +888,7 @@ class dba {
} }
} }
} }
}
self::p("COMMIT"); self::p("COMMIT");
return true; return true;
} }

View File

@ -1152,6 +1152,7 @@ function db_definition() {
"uid_parenturi" => array("uid","parent-uri(190)"), "uid_parenturi" => array("uid","parent-uri(190)"),
"uid_contactid_created" => array("uid","contact-id","created"), "uid_contactid_created" => array("uid","contact-id","created"),
"authorid_created" => array("author-id","created"), "authorid_created" => array("author-id","created"),
"ownerid" => array("owner-id"),
"uid_uri" => array("uid", "uri(190)"), "uid_uri" => array("uid", "uri(190)"),
"resource-id" => array("resource-id"), "resource-id" => array("resource-id"),
"contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"), // "contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"), //
@ -1627,6 +1628,9 @@ function db_definition() {
"uid_network_created" => array("uid","network","created"), "uid_network_created" => array("uid","network","created"),
"uid_contactid_commented" => array("uid","contact-id","commented"), "uid_contactid_commented" => array("uid","contact-id","commented"),
"uid_contactid_created" => array("uid","contact-id","created"), "uid_contactid_created" => array("uid","contact-id","created"),
"contactid" => array("contact-id"),
"ownerid" => array("owner-id"),
"authorid" => array("author-id"),
"uid_created" => array("uid","created"), "uid_created" => array("uid","created"),
"uid_commented" => array("uid","commented"), "uid_commented" => array("uid","commented"),
"uid_wall_created" => array("uid","wall","created"), "uid_wall_created" => array("uid","wall","created"),

View File

@ -1,6 +1,6 @@
<?php <?php
define('UPDATE_VERSION' , 1221); define('UPDATE_VERSION' , 1222);
/** /**
* *