]> git.mxchange.org Git - friendica.git/commitdiff
New function to detect heavily used indexes
authorMichael <heluecht@pirati.ca>
Fri, 13 Jan 2017 07:46:47 +0000 (07:46 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 13 Jan 2017 07:46:47 +0000 (07:46 +0000)
include/dba.php
include/dbstructure.php
include/photos.php
mod/photos.php

index 920027cbcfaeef721a02f2ec3214efed0fcf459b..17acf9432804196400ed0c1739baf9c956743c2d 100644 (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;");
index 5c89f49fb93242cf09381104a3a8ed178a83c75e..535d7c5416a66e2e6b30db35086d154e97e758c7 100644 (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)"),
                                        )
index 7cdd14bf6d3e586894748de06292c933f7173058..f3043e44ac97500e748971fdd4f6c738c08a807b 100644 (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),
index 60e5dee64361356ac0388ba62b40ab18768f8851..e23e4fcc924f18b727f2cf3862fb9e6d36c8c217 100644 (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']),