From c2acab21d44bfaa8e051a42a13b72c056c95b0a3 Mon Sep 17 00:00:00 2001 From: friendica Date: Mon, 23 Mar 2015 19:56:10 -0700 Subject: improve performance of the zotfeed queries --- include/items.php | 19 +++++++++---------- include/security.php | 6 ++++-- include/text.php | 10 +++++++--- 3 files changed, 20 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/items.php b/include/items.php index 8e293d761..32e5dcbe5 100755 --- a/include/items.php +++ b/include/items.php @@ -4366,12 +4366,11 @@ function zot_feed($uid,$observer_xchan,$arr) { $sql_extra = item_permissions_sql($uid); } + $limit = " LIMIT 100 "; + if($mindate != NULL_DATE) { $sql_extra .= " and ( created > '$mindate' or edited > '$mindate' ) "; - $limit = ""; } - else - $limit = " limit 0, 50 "; if($message_id) { $sql_extra .= " and mid = '" . dbesc($message_id) . "' "; @@ -4382,20 +4381,20 @@ function zot_feed($uid,$observer_xchan,$arr) { if(is_sys_channel($uid)) { require_once('include/security.php'); - $r = q("SELECT distinct parent, created from item + $r = q("SELECT parent from item WHERE uid != %d - and uid in (" . stream_perms_api_uids(PERMS_PUBLIC) . ") AND item_restrict = 0 - AND (item_flags & %d)>0 - and item_private = 0 $sql_extra ORDER BY created ASC $limit", + AND item_private = 0 AND item_restrict = 0 AND uid in (" . stream_perms_api_uids(PERMS_PUBLIC,10,1) . ") + AND (item_flags & %d) > 0 + $sql_extra GROUP BY parent ORDER BY created ASC $limit", intval($uid), intval(ITEM_WALL) ); } else { - $r = q("SELECT distinct parent, created from item + $r = q("SELECT parent from item WHERE uid = %d AND item_restrict = 0 - AND (item_flags & %d)>0 - $sql_extra ORDER BY created ASC $limit", + AND (item_flags & %d) > 0 + $sql_extra GROUP BY parent ORDER BY created ASC $limit", intval($uid), intval(ITEM_WALL) ); diff --git a/include/security.php b/include/security.php index e9b41dcf9..c3b0e9763 100644 --- a/include/security.php +++ b/include/security.php @@ -396,13 +396,15 @@ function init_groups_visitor($contact_id) { // will likely be too expensive. // Returns a string list of comma separated channel_ids suitable for direct inclusion in a SQL query -function stream_perms_api_uids($perms = NULL ) { +function stream_perms_api_uids($perms = NULL, $limit = 0, $rand = 0 ) { $perms = is_null($perms) ? (PERMS_SITE|PERMS_NETWORK|PERMS_PUBLIC) : $perms; $ret = array(); + $limit_sql = (($limit) ? " LIMIT " . intval($limit) . " " : ''); + $random_sql = (($rand) ? " ORDER BY " . db_getfunc('RAND') . " " : ''); if(local_channel()) $ret[] = local_channel(); - $r = q("select channel_id from channel where channel_r_stream > 0 and (channel_r_stream & %d)>0 and not (channel_pageflags & %d)>0", + $r = q("select channel_id from channel where channel_r_stream > 0 and ( channel_r_stream & %d )>0 and ( channel_pageflags & %d ) = 0 $random_sql $limit_sql ", intval($perms), intval(PAGE_ADULT|PAGE_CENSORED|PAGE_SYSTEM|PAGE_REMOVED) ); diff --git a/include/text.php b/include/text.php index 86da3ee42..a21d8029f 100644 --- a/include/text.php +++ b/include/text.php @@ -1855,9 +1855,13 @@ function check_webbie($arr) { function ids_to_querystr($arr,$idx = 'id') { $t = array(); - foreach($arr as $x) - $t[] = $x[$idx]; - + if($arr) { + foreach($arr as $x) { + if(! in_array($x[$idx],$t)) { + $t[] = $x[$idx]; + } + } + } return(implode(',', $t)); } -- cgit v1.2.3