Browse Source

Orphans are removed without giant lock problems

pull/2861/head
Michael Vogel 5 years ago
parent
commit
fd2d8cc180
  1. 19
      include/dba.php
  2. 36
      include/dbclean.php
  3. 1
      include/dbstructure.php
  4. 2
      mod/display.php

19
include/dba.php

@ -108,6 +108,23 @@ class dba {
return $return;
}
/**
* @brief Returns the number of rows
*
* @return string
*/
public function num_rows() {
if (!$this->result)
return 0;
if ($this->mysqli) {
$return = $this->result->num_rows;
} else {
$return = mysql_num_rows($this->result);
}
return $return;
}
public function q($sql, $onlyquery = false) {
global $a;
@ -126,6 +143,8 @@ class dba {
$stamp1 = microtime(true);
$sql = "/*".$a->callstack()." */ ".$sql;
if($this->mysqli)
$result = @$this->db->query($sql);
else

36
include/dbclean.php

@ -16,22 +16,48 @@ if(is_null($db)) {
load_config('config');
load_config('system');
update_shadow_copy();
remove_orphans();
killme();
function remove_orphans() {
global $db;
logger("Deleting orphaned data from thread table");
q("DELETE FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)");
if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
logger("found thread orphans: ".$db->num_rows());
while ($orphan = $db->qfetch())
q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
}
$db->qclose();
logger("Deleting orphaned data from notify table");
q("DELETE FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)");
if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
logger("found notify orphans: ".$db->num_rows());
while ($orphan = $db->qfetch())
q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
}
$db->qclose();
logger("Deleting orphaned data from sign table");
q("DELETE FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)");
if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
logger("found sign orphans: ".$db->num_rows());
while ($orphan = $db->qfetch())
q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
}
$db->qclose();
logger("Deleting orphaned data from term table");
q("DELETE FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)");
if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
logger("found term orphans: ".$db->num_rows());
while ($orphan = $db->qfetch())
q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
}
$db->qclose();
// SELECT `id`, `received`, `created`, `guid` FROM `item` WHERE `uid` = 0 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) LIMIT 1;
logger("Done deleting orphaned data from tables");
}

1
include/dbstructure.php

@ -1121,6 +1121,7 @@ function db_definition($charset) {
"indexes" => array(
"PRIMARY" => array("id"),
"uid_contactid" => array("uid", "contact-id"),
"uid_album_created" => array("uid", "album", "created"),
"resource-id" => array("resource-id"),
"guid" => array("guid"),
)

2
mod/display.php

@ -236,7 +236,7 @@ function display_content(&$a, $update = 0) {
}
if ($nick == "") {
$r = qu("SELECT `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
$r = qu("SELECT STRAIGHT_JOIN `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''

Loading…
Cancel
Save