DBClean now has a smaller limit/Query improvements

This commit is contained in:
Michael 2017-01-09 09:37:37 +00:00
parent 28b2e599fb
commit 16f0221e4c
4 changed files with 35 additions and 28 deletions

View File

@ -135,7 +135,7 @@ function contact_selector($selname, $selclass, $preselected = false, $options) {
$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n"; $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
$r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != '' WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
$sql_extra $sql_extra
ORDER BY `name` ASC ", ORDER BY `name` ASC ",
intval(local_user()) intval(local_user())
@ -210,7 +210,7 @@ function contact_select($selname, $selclass, $preselected = false, $size = 4, $p
$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n"; $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
$r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != '' WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
$sql_extra $sql_extra
ORDER BY `name` ASC ", ORDER BY `name` ASC ",
intval(local_user()) intval(local_user())
@ -449,8 +449,8 @@ function acl_lookup(&$a, $out_type = 'json') {
// autocomplete for editor mentions // autocomplete for editor mentions
if ($type=='' || $type=='c'){ if ($type=='' || $type=='c'){
$r = q("SELECT COUNT(*) AS c FROM `contact` $r = q("SELECT COUNT(*) AS c FROM `contact`
WHERE `uid` = %d AND `self` = 0 WHERE `uid` = %d AND NOT `self`
AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
AND `notify` != '' $sql_extra2" , AND `notify` != '' $sql_extra2" ,
intval(local_user()) intval(local_user())
); );
@ -461,8 +461,8 @@ function acl_lookup(&$a, $out_type = 'json') {
// autocomplete for Private Messages // autocomplete for Private Messages
$r = q("SELECT COUNT(*) AS c FROM `contact` $r = q("SELECT COUNT(*) AS c FROM `contact`
WHERE `uid` = %d AND `self` = 0 WHERE `uid` = %d AND NOT `self`
AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
AND `network` IN ('%s','%s','%s') $sql_extra2" , AND `network` IN ('%s','%s','%s') $sql_extra2" ,
intval(local_user()), intval(local_user()),
dbesc(NETWORK_DFRN), dbesc(NETWORK_DFRN),
@ -477,8 +477,8 @@ function acl_lookup(&$a, $out_type = 'json') {
// autocomplete for Contacts // autocomplete for Contacts
$r = q("SELECT COUNT(*) AS c FROM `contact` $r = q("SELECT COUNT(*) AS c FROM `contact`
WHERE `uid` = %d AND `self` = 0 WHERE `uid` = %d AND NOT `self`
AND `pending` = 0 $sql_extra2" , AND NOT `pending` $sql_extra2" ,
intval(local_user()) intval(local_user())
); );
$contact_count = (int)$r[0]['c']; $contact_count = (int)$r[0]['c'];
@ -525,7 +525,7 @@ function acl_lookup(&$a, $out_type = 'json') {
if ($type==''){ if ($type==''){
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact` $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != '' WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
AND NOT (`network` IN ('%s', '%s')) AND NOT (`network` IN ('%s', '%s'))
$sql_extra2 $sql_extra2
ORDER BY `name` ASC ", ORDER BY `name` ASC ",
@ -536,7 +536,7 @@ function acl_lookup(&$a, $out_type = 'json') {
elseif ($type=='c'){ elseif ($type=='c'){
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact` $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != '' WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
AND NOT (`network` IN ('%s')) AND NOT (`network` IN ('%s'))
$sql_extra2 $sql_extra2
ORDER BY `name` ASC ", ORDER BY `name` ASC ",
@ -546,7 +546,7 @@ function acl_lookup(&$a, $out_type = 'json') {
} }
elseif($type == 'm') { elseif($type == 'm') {
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
AND `network` IN ('%s','%s','%s') AND `network` IN ('%s','%s','%s')
$sql_extra2 $sql_extra2
ORDER BY `name` ASC ", ORDER BY `name` ASC ",

View File

@ -128,7 +128,7 @@ function cron_run(&$argv, &$argc){
proc_run(PRIORITY_LOW, 'include/expire.php'); proc_run(PRIORITY_LOW, 'include/expire.php');
proc_run(PRIORITY_LOW, 'include/dbclean.php'); proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
cron_update_photo_albums(); cron_update_photo_albums();
} }

View File

@ -23,8 +23,8 @@ function dbclean_run(&$argv, &$argc) {
unset($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data);
} }
load_config('config'); Config::load('config');
load_config('system'); Config::load('system');
if (!Config::get('system', 'dbclean', false)) { if (!Config::get('system', 'dbclean', false)) {
return; return;
@ -36,7 +36,7 @@ function dbclean_run(&$argv, &$argc) {
$stage = 0; $stage = 0;
} }
if (get_config("system", "worker") AND ($stage == 0)) { if (Config::get("system", "worker") AND ($stage == 0)) {
proc_run(PRIORITY_LOW, 'include/dbclean.php', 1); proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
proc_run(PRIORITY_LOW, 'include/dbclean.php', 2); proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
proc_run(PRIORITY_LOW, 'include/dbclean.php', 3); proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
@ -57,11 +57,18 @@ function remove_orphans($stage = 0) {
$count = 0; $count = 0;
// With activated worker we split the deletion in many small tasks
if (Config::get("system", "worker")) {
$limit = 1000;
} else {
$limit = 10000;
}
if (($stage == 1) OR ($stage == 0)) { if (($stage == 1) OR ($stage == 0)) {
logger("Deleting old global item entries from item table without user copy"); logger("Deleting old global item entries from item table without user copy");
if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0 if ($db->q("SELECT `id` FROM `item` WHERE `uid` = 0
AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT 10000", true)) { AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found global item orphans: ".$count); logger("found global item orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -74,7 +81,7 @@ function remove_orphans($stage = 0) {
if (($stage == 2) OR ($stage == 0)) { if (($stage == 2) OR ($stage == 0)) {
logger("Deleting items without parents"); logger("Deleting items without parents");
if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT 10000", true)) { if ($db->q("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found item orphans without parents: ".$count); logger("found item orphans without parents: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -87,7 +94,7 @@ function remove_orphans($stage = 0) {
if (($stage == 3) OR ($stage == 0)) { if (($stage == 3) OR ($stage == 0)) {
logger("Deleting orphaned data from thread table"); logger("Deleting orphaned data from thread table");
if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) { if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found thread orphans: ".$count); logger("found thread orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -100,7 +107,7 @@ function remove_orphans($stage = 0) {
if (($stage == 4) OR ($stage == 0)) { if (($stage == 4) OR ($stage == 0)) {
logger("Deleting orphaned data from notify table"); logger("Deleting orphaned data from notify table");
if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) { if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found notify orphans: ".$count); logger("found notify orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -113,7 +120,7 @@ function remove_orphans($stage = 0) {
if (($stage == 5) OR ($stage == 0)) { if (($stage == 5) OR ($stage == 0)) {
logger("Deleting orphaned data from notify-threads table"); logger("Deleting orphaned data from notify-threads table");
if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`)", true)) { if ($db->q("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found notify-threads orphans: ".$count); logger("found notify-threads orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -127,7 +134,7 @@ function remove_orphans($stage = 0) {
if (($stage == 6) OR ($stage == 0)) { if (($stage == 6) OR ($stage == 0)) {
logger("Deleting orphaned data from sign table"); logger("Deleting orphaned data from sign table");
if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) { if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found sign orphans: ".$count); logger("found sign orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -141,7 +148,7 @@ function remove_orphans($stage = 0) {
if (($stage == 7) OR ($stage == 0)) { if (($stage == 7) OR ($stage == 0)) {
logger("Deleting orphaned data from term table"); logger("Deleting orphaned data from term table");
if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) { if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit), true)) {
$count = $db->num_rows(); $count = $db->num_rows();
logger("found term orphans: ".$count); logger("found term orphans: ".$count);
while ($orphan = $db->qfetch()) { while ($orphan = $db->qfetch()) {
@ -153,8 +160,8 @@ function remove_orphans($stage = 0) {
} }
// Call it again if not all entries were purged // Call it again if not all entries were purged
if (($stage != 0) AND ($count > 0) AND get_config("system", "worker")) { if (($stage != 0) AND ($count > 0) AND Config::get("system", "worker")) {
proc_run(PRIORITY_LOW, 'include/dbclean.php'); proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
} }
} }

View File

@ -219,8 +219,8 @@ function frio_remote_nav($a,&$nav) {
// empty the server url for local user because we won't need it // empty the server url for local user because we won't need it
$server_url = ''; $server_url = '';
// user info // user info
$r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self` = 1", intval($a->user['uid'])); $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
$r[0]['photo'] = (dbm::is_result($r) ? $a->remove_baseurl($r[0]['micro']) : "images/person-48.jpg"); $r[0]['photo'] = (dbm::is_result($r) ? $a->remove_baseurl($r[0]['micro']) : "images/person-48.jpg");
$r[0]['name'] = $a->user['username']; $r[0]['name'] = $a->user['username'];
@ -298,7 +298,7 @@ function frio_acl_lookup($a, &$results) {
$r = q("SELECT COUNT(*) AS `total` FROM `contact` $r = q("SELECT COUNT(*) AS `total` FROM `contact`
WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ", WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra2 ",
intval($_SESSION['uid'])); intval($_SESSION['uid']));
if (dbm::is_result($r)) { if (dbm::is_result($r)) {
$total = $r[0]["total"]; $total = $r[0]["total"];
@ -306,7 +306,7 @@ function frio_acl_lookup($a, &$results) {
$sql_extra3 = unavailable_networks(); $sql_extra3 = unavailable_networks();
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT 100 ", $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT 100 ",
intval($_SESSION['uid']) intval($_SESSION['uid'])
); );