New function to detect heavily used indexes
This commit is contained in:
parent
b067a11146
commit
fd5f151a72
|
@ -138,6 +138,38 @@ class dba {
|
||||||
return $return;
|
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) {
|
public function q($sql, $onlyquery = false) {
|
||||||
$a = get_app();
|
$a = get_app();
|
||||||
|
|
||||||
|
@ -375,6 +407,9 @@ function q($sql) {
|
||||||
//logger("dba: q: $stmt", LOGGER_ALL);
|
//logger("dba: q: $stmt", LOGGER_ALL);
|
||||||
if ($stmt === false)
|
if ($stmt === false)
|
||||||
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
||||||
|
|
||||||
|
$db->log_index($stmt);
|
||||||
|
|
||||||
return $db->q($stmt);
|
return $db->q($stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,6 +443,9 @@ function qu($sql) {
|
||||||
$stmt = @vsprintf($sql,$args); // Disabled warnings
|
$stmt = @vsprintf($sql,$args); // Disabled warnings
|
||||||
if ($stmt === false)
|
if ($stmt === false)
|
||||||
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
||||||
|
|
||||||
|
$db->log_index($stmt);
|
||||||
|
|
||||||
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
|
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
|
||||||
$retval = $db->q($stmt);
|
$retval = $db->q($stmt);
|
||||||
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
|
$db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
|
||||||
|
|
|
@ -587,10 +587,10 @@ function db_definition($charset) {
|
||||||
"indexes" => array(
|
"indexes" => array(
|
||||||
"PRIMARY" => array("id"),
|
"PRIMARY" => array("id"),
|
||||||
"uid_name" => array("uid", "name"),
|
"uid_name" => array("uid", "name"),
|
||||||
"uid_self" => array("uid", "self"),
|
"self_uid" => array("self", "uid"),
|
||||||
"alias_uid" => array("alias(32)", "uid"),
|
"alias_uid" => array("alias(32)", "uid"),
|
||||||
"uid_pending" => array("uid", "pending"),
|
"pending_uid" => array("pending", "uid"),
|
||||||
"uid_blocked" => array("uid", "blocked"),
|
"blocked_uid" => array("blocked", "uid"),
|
||||||
"uid_rel_network_poll" => array("uid", "rel", "network", "poll(64)", "archive"),
|
"uid_rel_network_poll" => array("uid", "rel", "network", "poll(64)", "archive"),
|
||||||
"uid_network_batch" => array("uid", "network", "batch(64)"),
|
"uid_network_batch" => array("uid", "network", "batch(64)"),
|
||||||
"addr_uid" => array("addr(32)", "uid"),
|
"addr_uid" => array("addr(32)", "uid"),
|
||||||
|
@ -1162,6 +1162,7 @@ function db_definition($charset) {
|
||||||
"uid_contactid" => array("uid", "contact-id"),
|
"uid_contactid" => array("uid", "contact-id"),
|
||||||
"uid_profile" => array("uid", "profile"),
|
"uid_profile" => array("uid", "profile"),
|
||||||
"uid_album_created" => array("uid", "album(32)", "created"),
|
"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"),
|
"uid_album_resource-id_created" => array("uid", "album(32)", "resource-id(64)", "created"),
|
||||||
"resource-id" => array("resource-id(64)"),
|
"resource-id" => array("resource-id(64)"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -49,7 +49,7 @@ function photo_albums($uid, $update = false) {
|
||||||
/// @todo This query needs to be renewed. It is really slow
|
/// @todo This query needs to be renewed. It is really slow
|
||||||
// At this time we just store the data in the cache
|
// At this time we just store the data in the cache
|
||||||
$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`
|
$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
|
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
|
||||||
GROUP BY `album` ORDER BY `created` DESC",
|
GROUP BY `album` ORDER BY `created` DESC",
|
||||||
intval($uid),
|
intval($uid),
|
||||||
|
@ -57,9 +57,10 @@ function photo_albums($uid, $update = false) {
|
||||||
dbesc(t('Contact Photos'))
|
dbesc(t('Contact Photos'))
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
// USE INDEX (`uid_album`)
|
||||||
// This query doesn't do the count and is much faster
|
// This query doesn't do the count and is much faster
|
||||||
$albums = qu("SELECT DISTINCT(`album`), '' AS `total`
|
$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
|
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
|
||||||
GROUP BY `album` ORDER BY `created` DESC",
|
GROUP BY `album` ORDER BY `created` DESC",
|
||||||
intval($uid),
|
intval($uid),
|
||||||
|
|
|
@ -1344,7 +1344,7 @@ function photos_content(App $a) {
|
||||||
else
|
else
|
||||||
$order = 'DESC';
|
$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
|
$prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
|
||||||
$sql_extra ORDER BY `created` $order ",
|
$sql_extra ORDER BY `created` $order ",
|
||||||
dbesc($ph[0]['album']),
|
dbesc($ph[0]['album']),
|
||||||
|
|
Loading…
Reference in a new issue