]> git.mxchange.org Git - friendica.git/commitdiff
Some more improved queries
authorMichael Vogel <ike@pirati.ca>
Fri, 21 Oct 2016 18:25:21 +0000 (18:25 +0000)
committerMichael Vogel <ike@pirati.ca>
Fri, 21 Oct 2016 18:25:21 +0000 (18:25 +0000)
include/api.php
include/dbstructure.php
include/dfrn.php
include/items.php
include/onepoll.php
include/ostatus.php
mod/display.php
mod/item.php
mod/photos.php

index b93f8cf72a32b0ce14dcabe9f1754b54f17431c1..af7c2ca523efc29613b4390c48f8bd2d3160df7d 100644 (file)
 
                        //AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
                        // count public wall messages
-                       $r = q("SELECT count(*) as `count` FROM `item`
-                                       WHERE  `uid` = %d
-                                       AND `type`='wall'",
+                       $r = q("SELECT COUNT(*) as `count` FROM `item` WHERE `uid` = %d AND wall",
                                        intval($uinfo[0]['uid'])
                        );
                        $countitms = $r[0]['count'];
index f3c89474087c3e5c8e4899a75d814f8164dba83d..b28e072f9e23198b170f4e2955f4f540fe529cac 100644 (file)
@@ -1121,6 +1121,7 @@ function db_definition($charset) {
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "uid_contactid" => array("uid", "contact-id"),
+                                       "uid_profile" => array("uid", "profile"),
                                        "uid_album_created" => array("uid", "album", "created"),
                                        "resource-id" => array("resource-id"),
                                        "guid" => array("guid"),
@@ -1356,6 +1357,7 @@ function db_definition($charset) {
                                        "type_term" => array("type","term"),
                                        "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
                                        "otype_type_term_tid" => array("otype","type","term","tid"),
+                                       "uid_otype_type_url" => array("uid","otype","type","url"),
                                        "guid" => array("guid"),
                                        )
                        );
index 3b06932d8a186424d3c45ac4351bf996590a2cbe..061991c4cac7658487f34745a6c6a60c4b6e6511 100644 (file)
@@ -193,13 +193,13 @@ class dfrn {
                //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
                //      dbesc($check_date),
 
-               $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
+               $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
                        `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
                        `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
                        `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                        `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
                        FROM `item` $sql_post_table
-                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                        AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                        LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
                        WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
index 0741e1b3d43ee8340ad568281fa89a08f22c3871..b969a75d6db21e308a1937381a37b0cde10611d6 100644 (file)
@@ -779,6 +779,9 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
 
        logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
 
+       q("COMMIT;");
+       q("START TRANSACTION;");
+
        $r = dbq("INSERT INTO `item` (`"
                        . implode("`, `", array_keys($arr))
                        . "`) VALUES ('"
@@ -804,6 +807,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                        dbesc($arr['network']),
                        intval($r[0]["id"])
                );
+               q("COMMIT");
                return 0;
        } elseif(count($r)) {
 
@@ -813,6 +817,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                item_set_last_item($arr);
        } else {
                logger('item_store: could not locate created item');
+               q("COMMIT");
                return 0;
        }
 
@@ -891,10 +896,12 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
        create_tags_from_item($current_post);
        create_files_from_item($current_post);
 
+       q("COMMIT");
+
        // Only check for notifications on start posts
-       if ($arr['parent-uri'] === $arr['uri'])
+       if ($arr['parent-uri'] === $arr['uri']) {
                add_thread($current_post);
-       else {
+       else {
                update_thread($parent_id);
                add_shadow_entry($arr);
        }
index e77937272088b31884b866471cb842957692e0eb..951b87043e06d8a014f81ad365eb62d8b5abfdc3 100644 (file)
@@ -443,7 +443,7 @@ function onepoll_run(&$argv, &$argc){
                                                                        $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
                                                        }
                                                        $qstr = implode(',',$refs_arr);
-                                                       $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
+                                                       $r = q("SELECT `uri` , `parent-uri` FROM `item` USE INDEX (`uid_uri`) WHERE `uri` IN ($qstr) AND `uid` = %d LIMIT 1",
                                                                intval($importer_uid)
                                                        );
                                                        if(count($r))
index ec53141dc7d06c386c8d070baddbfc1748e08c6a..556c81e987901b6d7f9a7805fd2d6e279a7dc836 100644 (file)
@@ -806,11 +806,19 @@ class ostatus {
                }
 
                // Get the parent
+               $parents = q("SELECT `item`.`id`, `item`.`parent`, `item`.`uri`, `item`.`contact-id`, `item`.`type`,
+                               `item`.`verb`, `item`.`visible` FROM `term`
+                               STRAIGHT_JOIN `item` AS `thritem` ON `thritem`.`parent` = `term`.`oid`
+                               STRAIGHT_JOIN `item` ON `item`.`parent` = `thritem`.`parent`
+                               WHERE `term`.`uid` = %d AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`url` = '%s'))",
+                               intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
+
+/*
                $parents = q("SELECT `id`, `parent`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `id` IN
                                (SELECT `parent` FROM `item` WHERE `id` IN
                                        (SELECT `oid` FROM `term` WHERE `uid` = %d AND `otype` = %d AND `type` = %d AND `url` = '%s'))",
                                intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
-
+*/
                if ($parents)
                        $parent = $parents[0];
                elseif (count($item) > 0) {
@@ -1961,9 +1969,21 @@ class ostatus {
                        $last_update = 'now -30 days';
 
                $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
-
-               $items = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id` FROM `item`
-                               INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`
+               $authorid = get_contact($owner["url"], 0);
+
+               $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
+                               STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
+                               WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
+                                       `item`.`author-id` = %d AND `item`.`created` > '%s' AND
+                                       NOT `item`.`deleted` AND NOT `item`.`private` AND
+                                       `thread`.`network` IN ('%s', '%s')
+                               ORDER BY `item`.`created` DESC LIMIT 300",
+                               intval($owner["uid"]), intval($owner["id"]),
+                               intval($authorid), dbesc($check_date),
+                               dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+/*
+               $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item`
+                               STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
                                LEFT JOIN `item` AS `thritem` ON `thritem`.`uri`=`item`.`thr-parent` AND `thritem`.`uid`=`item`.`uid`
                                WHERE `item`.`uid` = %d AND `item`.`received` > '%s' AND NOT `item`.`private` AND NOT `item`.`deleted`
                                        AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
@@ -1981,7 +2001,7 @@ class ostatus {
                                dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"])),
                                dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"]))
                        );
-
+*/
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
index 0e0e7b30314a5efd21240ba591c641fa9a445b43..dcb9eb0941bf49b94bc0d329dfd274fa8591f722 100644 (file)
@@ -27,9 +27,9 @@ function display_init(&$a) {
 
                // Or is it anywhere on the server?
                if ($nick == "") {
-                       $r = qu("SELECT STRAIGHT_JOIN `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
+                       $r = qu("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
                                `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
-                               FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+                               FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
                                WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
                                        AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
                                        AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
@@ -236,7 +236,7 @@ function display_content(&$a, $update = 0) {
                        }
 
                        if ($nick == "") {
-                               $r = qu("SELECT STRAIGHT_JOIN `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+                               $r = qu("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
                                        WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
                                                AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
                                                AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
index adc46f120822787ffe48d139e8db7e1d090abf64..94ff6b93d4f1836ad6353df1c0646cc5a9ce6b42 100644 (file)
@@ -788,6 +788,9 @@ function item_post(&$a) {
        } else
                $post_id = 0;
 
+       q("COMMIT;");
+       q("START TRANSACTION;");
+
        $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,
                                        `owner-name`,`owner-link`,`owner-avatar`, `owner-id`,
                                        `author-name`, `author-link`, `author-avatar`, `author-id`,
@@ -864,6 +867,7 @@ function item_post(&$a) {
        $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
                dbesc($datarray['uri']));
        if(!count($r)) {
+               q("COMMIT");
                logger('mod_item: unable to retrieve post that was just stored.');
                notice( t('System error. Post not saved.') . EOL);
                goaway($a->get_baseurl() . "/" . $return_path );
@@ -986,6 +990,8 @@ function item_post(&$a) {
        create_tags_from_item($post_id);
        create_files_from_item($post_id);
 
+       q("COMMIT");
+
        if ($post_id == $parent)
                add_thread($post_id);
        else {
index 741181b2e9fa4628e1f65cbaf01859f2cd646d93..ced9523e284586cf46b3b13b577450566ef5325e 100644 (file)
@@ -53,8 +53,8 @@ function photos_init(&$a) {
 
                $sql_extra = permissions_sql($a->data['user']['uid']);
 
-               $albums = q("SELECT count(distinct `resource-id`) AS `total`, `album` FROM `photo` WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s'
-                       $sql_extra group by album order by created desc",
+               $albums = q("SELECT count(distinct `resource-id`) AS `total`, `album` FROM `photo` USE INDEX (`uid_album_created`) WHERE `uid` = %d  AND `album` != '%s' AND `album` != '%s'
+                       $sql_extra GROUP BY `album` ORDER BY `created` DESC",
                        intval($a->data['user']['uid']),
                        dbesc('Contact Photos'),
                        dbesc( t('Contact Photos'))