Browse Source

New function to detect heavily used indexes

pull/3096/head
Michael 4 years ago
parent
commit
fd5f151a72
4 changed files with 46 additions and 6 deletions
  1. +38
    -0
      include/dba.php
  2. +4
    -3
      include/dbstructure.php
  3. +3
    -2
      include/photos.php
  4. +1
    -1
      mod/photos.php

+ 38
- 0
include/dba.php View File

@ -138,6 +138,38 @@ class dba {
return $return;
}
public function log_index($query) {
$a = get_app();
if (($a->config["system"]["db_log_index"] == "") OR ($a->config["system"]["db_log_index_watch"] == "") OR
(intval($a->config["system"]["db_loglimit_index"]) == 0)) {
return;
}
if (strtolower(substr($query, 0, 7)) == "explain") {
return;
}
$r = $this->q("EXPLAIN ".$query);
if (!dbm::is_result($r)) {
return;
}
$watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
foreach ($r AS $row) {
if (in_array($row['key'], $watchlist) AND
($row['rows'] >= intval($a->config["system"]["db_loglimit_index"]))) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@file_put_contents($a->config["system"]["db_log_index"], datetime_convert()."\t".
$row['key']."\t".$row['rows']."\t".
basename($backtrace[1]["file"])."\t".
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
substr($query, 0, 2000)."\n", FILE_APPEND);
}
}
}
public function q($sql, $onlyquery = false) {
$a = get_app();
@ -375,6 +407,9 @@ function q($sql) {
//logger("dba: q: $stmt", LOGGER_ALL);
if ($stmt === false)
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
$db->log_index($stmt);
return $db->q($stmt);
}
@ -408,6 +443,9 @@ function qu($sql) {
$stmt = @vsprintf($sql,$args); // Disabled warnings
if ($stmt === false)
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
$db->log_index($stmt);
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
$retval = $db->q($stmt);
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");


+ 4
- 3
include/dbstructure.php View File

@ -587,10 +587,10 @@ function db_definition($charset) {
"indexes" => array(
"PRIMARY" => array("id"),
"uid_name" => array("uid", "name"),
"uid_self" => array("uid", "self"),
"self_uid" => array("self", "uid"),
"alias_uid" => array("alias(32)", "uid"),
"uid_pending" => array("uid", "pending"),
"uid_blocked" => array("uid", "blocked"),
"pending_uid" => array("pending", "uid"),
"blocked_uid" => array("blocked", "uid"),
"uid_rel_network_poll" => array("uid", "rel", "network", "poll(64)", "archive"),
"uid_network_batch" => array("uid", "network", "batch(64)"),
"addr_uid" => array("addr(32)", "uid"),
@ -1162,6 +1162,7 @@ function db_definition($charset) {
"uid_contactid" => array("uid", "contact-id"),
"uid_profile" => array("uid", "profile"),
"uid_album_created" => array("uid", "album(32)", "created"),
"uid_album_scale_created" => array("uid", "album(32)", "scale", "created"),
"uid_album_resource-id_created" => array("uid", "album(32)", "resource-id(64)", "created"),
"resource-id" => array("resource-id(64)"),
)


+ 3
- 2
include/photos.php View File

@ -49,7 +49,7 @@ function photo_albums($uid, $update = false) {
/// @todo This query needs to be renewed. It is really slow
// At this time we just store the data in the cache
$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`
FROM `photo` USE INDEX (`uid_album_created`)
FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
GROUP BY `album` ORDER BY `created` DESC",
intval($uid),
@ -57,9 +57,10 @@ function photo_albums($uid, $update = false) {
dbesc(t('Contact Photos'))
);
} else {
// USE INDEX (`uid_album`)
// This query doesn't do the count and is much faster
$albums = qu("SELECT DISTINCT(`album`), '' AS `total`
FROM `photo` USE INDEX (`uid_album_created`)
FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
GROUP BY `album` ORDER BY `created` DESC",
intval($uid),


+ 1
- 1
mod/photos.php View File

@ -1344,7 +1344,7 @@ function photos_content(App $a) {
else
$order = 'DESC';
/// @todo This query is totally bad, the whole functionality has to be changed
$prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
$sql_extra ORDER BY `created` $order ",
dbesc($ph[0]['album']),


Loading…
Cancel
Save