Merge pull request #4570 from annando/issue-4556

Issue 4556: Adding index hints to improve the notifications speed
This commit is contained in:
Hypolite Petovan 2018-03-09 21:07:37 -05:00 committed by GitHub
commit 95f8423b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,7 +135,7 @@ class NotificationsManager extends BaseObject
public function setSeen($note, $seen = true) public function setSeen($note, $seen = true)
{ {
return q( return q(
"UPDATE `notify` SET `seen` = %d WHERE ( `link` = '%s' OR ( `parent` != 0 AND `parent` = %d AND `otype` = '%s' )) AND `uid` = %d", "UPDATE `notify` SET `seen` = %d WHERE (`link` = '%s' OR (`parent` != 0 AND `parent` = %d AND `otype` = '%s')) AND `uid` = %d",
intval($seen), intval($seen),
dbesc($note['link']), dbesc($note['link']),
intval($note['parent']), intval($note['parent']),
@ -384,16 +384,18 @@ class NotificationsManager extends BaseObject
private function networkTotal($seen = 0) private function networkTotal($seen = 0)
{ {
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT COUNT(*) AS `total` "SELECT COUNT(*) AS `total`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent` FROM `item` $index_hint STRAIGHT_JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND WHERE `item`.`visible` AND `pitem`.`parent` != 0 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 NOT `item`.`deleted` AND `item`.`uid` = %d AND NOT `item`.`wall`
$sql_seen", $sql_seen",
intval(local_user()) intval(local_user())
); );
@ -423,18 +425,20 @@ class NotificationsManager extends BaseObject
$total = $this->networkTotal($seen); $total = $this->networkTotal($seen);
$notifs = []; $notifs = [];
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`, "SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
`pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid` `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent` FROM `item` $index_hint STRAIGHT_JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND WHERE `item`.`visible` AND `pitem`.`parent` != 0 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 NOT `item`.`deleted` AND `item`.`uid` = %d AND NOT `item`.`wall`
$sql_seen $sql_seen
ORDER BY `item`.`created` DESC LIMIT %d, %d ", ORDER BY `item`.`created` DESC LIMIT %d, %d ",
intval(local_user()), intval(local_user()),
@ -466,7 +470,7 @@ class NotificationsManager extends BaseObject
$sql_seen = ""; $sql_seen = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `seen` = 0 "; $sql_seen = " AND NOT `seen` ";
} }
$r = q( $r = q(
@ -501,7 +505,7 @@ class NotificationsManager extends BaseObject
$sql_seen = ""; $sql_seen = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `seen` = 0 "; $sql_seen = " AND NOT `seen` ";
} }
$r = q( $r = q(
@ -536,7 +540,7 @@ class NotificationsManager extends BaseObject
$myurl = str_replace(['www.', '.'], ['', '\\.'], $myurl); $myurl = str_replace(['www.', '.'], ['', '\\.'], $myurl);
$diasp_url = str_replace('/profile/', '/u/', $myurl); $diasp_url = str_replace('/profile/', '/u/', $myurl);
$sql_extra = sprintf( $sql_extra = sprintf(
" AND ( `item`.`author-link` regexp '%s' OR `item`.`tag` regexp '%s' OR `item`.`tag` regexp '%s' ) ", " AND (`item`.`author-link` REGEXP '%s' OR `item`.`tag` REGEXP '%s' OR `item`.`tag` REGEXP '%s') ",
dbesc($myurl . '$'), dbesc($myurl . '$'),
dbesc($myurl . '\\]'), dbesc($myurl . '\\]'),
dbesc($diasp_url . '\\]') dbesc($diasp_url . '\\]')
@ -555,19 +559,21 @@ class NotificationsManager extends BaseObject
private function personalTotal($seen = 0) private function personalTotal($seen = 0)
{ {
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
$sql_extra = $this->personalSqlExtra(); $sql_extra = $this->personalSqlExtra();
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT COUNT(*) AS `total` "SELECT COUNT(*) AS `total`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent` FROM `item` $index_hint
WHERE `item`.`visible` = 1 WHERE `item`.`visible`
$sql_extra $sql_extra
$sql_seen $sql_seen
AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ", AND NOT `item`.`deleted` AND `item`.`uid` = %d AND NOT `item`.`wall`",
intval(local_user()) intval(local_user())
); );
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
@ -597,20 +603,22 @@ class NotificationsManager extends BaseObject
$sql_extra = $this->personalSqlExtra(); $sql_extra = $this->personalSqlExtra();
$notifs = []; $notifs = [];
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`, "SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
`pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid` `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent` FROM `item` $index_hint STRAIGHT_JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
WHERE `item`.`visible` = 1 WHERE `item`.`visible`
$sql_extra $sql_extra
$sql_seen $sql_seen
AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 AND NOT `item`.`deleted` AND `item`.`uid` = %d AND NOT `item`.`wall`
ORDER BY `item`.`created` DESC LIMIT %d, %d ", ORDER BY `item`.`created` DESC LIMIT %d, %d ",
intval(local_user()), intval(local_user()),
intval($start), intval($start),
@ -639,13 +647,15 @@ class NotificationsManager extends BaseObject
private function homeTotal($seen = 0) private function homeTotal($seen = 0)
{ {
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT COUNT(*) AS `total` FROM `item` "SELECT COUNT(*) AS `total` FROM `item` $index_hint
WHERE `item`.`visible` = 1 AND WHERE `item`.`visible` = 1 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
$sql_seen", $sql_seen",
@ -677,18 +687,20 @@ class NotificationsManager extends BaseObject
$total = $this->homeTotal($seen); $total = $this->homeTotal($seen);
$notifs = []; $notifs = [];
$sql_seen = ""; $sql_seen = "";
$index_hint = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` ";
$index_hint = "USE INDEX (`uid_unseen_contactid`)";
} }
$r = q( $r = q(
"SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`, "SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, `item`.`unseen`,
`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` AS `object`,
`pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid` `pitem`.`author-name` AS `pname`, `pitem`.`author-link` AS `plink`, `pitem`.`guid` AS `pguid`
FROM `item` INNER JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent` FROM `item` $index_hint STRAIGHT_JOIN `item` AS `pitem` ON `pitem`.`id`=`item`.`parent`
WHERE `item`.`visible` = 1 AND WHERE `item`.`visible` AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 NOT `item`.`deleted` AND `item`.`uid` = %d AND `item`.`wall`
$sql_seen $sql_seen
ORDER BY `item`.`created` DESC LIMIT %d, %d ", ORDER BY `item`.`created` DESC LIMIT %d, %d ",
intval(local_user()), intval(local_user()),