From 179b311978d511513ff03586d415e51677092bfd Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 14 Dec 2017 22:47:58 -0500 Subject: [PATCH 1/4] Remove group_member.uid - Use contact.uid alternatively - Use JOIN with group table --- include/acl_selectors.php | 2 +- include/uimport.php | 2 -- mod/uexport.php | 2 +- src/Database/DBStructure.php | 2 -- src/Model/Contact.php | 8 +++++--- src/Model/Group.php | 3 +-- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/acl_selectors.php b/include/acl_selectors.php index d1b791136..7a488ca40 100644 --- a/include/acl_selectors.php +++ b/include/acl_selectors.php @@ -529,7 +529,7 @@ function acl_lookup(App $a, $out_type = 'json') { // This can be done when we can delete cache entries via wildcard $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids FROM `group` - INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid` + INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` WHERE NOT `group`.`deleted` AND `group`.`uid` = %d $sql_extra GROUP BY `group`.`name`, `group`.`id` diff --git a/include/uimport.php b/include/uimport.php index e3c2f4e6c..d9b040597 100644 --- a/include/uimport.php +++ b/include/uimport.php @@ -232,8 +232,6 @@ function import_account(App $a, $file) { } foreach ($account['group_member'] as &$group_member) { - $group_member['uid'] = $newuid; - $import = 0; foreach ($account['group'] as $group) { if ($group['id'] == $group_member['gid'] && isset($group['newid'])) { diff --git a/mod/uexport.php b/mod/uexport.php index 5a897f4ab..396082508 100644 --- a/mod/uexport.php +++ b/mod/uexport.php @@ -109,7 +109,7 @@ function uexport_account($a) { ); $group_member = _uexport_multirow( - sprintf("SELECT * FROM `group_member` WHERE uid = %d", intval(local_user())) + sprintf("SELECT `group_member`.`gid`, `group_member`.`contact-id` FROM `group_member` INNER JOIN `group` ON `group`.`id` = `group_member`.`gid` WHERE `group`.`uid` = %d", intval(local_user())) ); $output = array( diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index c118d862f..3dc555c86 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -990,7 +990,6 @@ class DBStructure { $database["group_member"] = array( "fields" => array( "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), - "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "relation" => array("user" => "uid")), "gid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "relation" => array("group" => "id")), "contact-id" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "relation" => array("contact" => "id")), ), @@ -998,7 +997,6 @@ class DBStructure { "PRIMARY" => array("id"), "contactid" => array("contact-id"), "gid_contactid" => array("gid", "contact-id"), - "uid_gid_contactid" => array("UNIQUE", "uid", "gid", "contact-id"), ) ); $database["gserver"] = array( diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 2e1bc01c7..444ecf3c0 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -42,7 +42,7 @@ class Contact extends BaseObject INNER JOIN `group_member` ON `contact`.`id` = `group_member`.`contact-id` WHERE `gid` = ? - AND `group_member`.`uid` = ? + AND `contact`.`uid` = ? AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending` @@ -73,7 +73,7 @@ class Contact extends BaseObject INNER JOIN `group_member` ON `contact`.`id` = `group_member`.`contact-id` WHERE `gid` = ? - AND `group_member`.`uid` = ? + AND `contact`.`uid` = ? AND `contact`.`network` = ? AND `contact`.`notify` != ""', $gid, @@ -605,7 +605,9 @@ class Contact extends BaseObject AND NOT `pending` AND `id` NOT IN ( SELECT DISTINCT(`contact-id`) - FROM `group_member` WHERE `uid` = %d + FROM `group_member` + JOIN `group` ON `group`.`id` = `group_member`.`gid` + WHERE `group`.`uid` = %d ) LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count) ); diff --git a/src/Model/Group.php b/src/Model/Group.php index cdc2a821d..542827151 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -96,8 +96,7 @@ class Group extends BaseObject AND `contact-id` IN (SELECT `contact-id` FROM `group_member` - WHERE `group_member`.`gid` = `group`.`id` - AND `group_member`.`uid` = ?) + WHERE `group_member`.`gid` = `group`.`id`) ) AS `count` FROM `group` WHERE `group`.`uid` = ?;", From c8c7ff2a2e0bc6d3a4058ba421add26dbf065e9a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 15 Dec 2017 08:43:37 -0500 Subject: [PATCH 2/4] Add new UNIQUE index to group_member - Add new index on gid --- src/Database/DBStructure.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 3dc555c86..8f879c032 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -996,7 +996,8 @@ class DBStructure { "indexes" => array( "PRIMARY" => array("id"), "contactid" => array("contact-id"), - "gid_contactid" => array("gid", "contact-id"), + "gid" => array("gid"), + "gid_contactid" => array("UNIQUE", "gid", "contact-id"), ) ); $database["gserver"] = array( From 868a84c5cc2db5b27f719687a0d9f3bd92e1209d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 15 Dec 2017 08:44:00 -0500 Subject: [PATCH 3/4] Use INNER for group_member join --- src/Model/Contact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 444ecf3c0..2203b6216 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -606,7 +606,7 @@ class Contact extends BaseObject AND `id` NOT IN ( SELECT DISTINCT(`contact-id`) FROM `group_member` - JOIN `group` ON `group`.`id` = `group_member`.`gid` + INNER JOIN `group` ON `group`.`id` = `group_member`.`gid` WHERE `group`.`uid` = %d ) LIMIT %d, %d", intval($uid), intval($uid), intval($start), intval($count) From 6dbd68e0cb96c2a79e36fc4f1e449eaca55f3fa5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 15 Dec 2017 11:36:06 -0500 Subject: [PATCH 4/4] Remove redundant index - Increase UPDATE_VERSION and DB_UPDATE_VERSION --- boot.php | 2 +- src/Database/DBStructure.php | 1 - update.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/boot.php b/boot.php index 79cf3e5b3..695f9e85c 100644 --- a/boot.php +++ b/boot.php @@ -43,7 +43,7 @@ define('FRIENDICA_PLATFORM', 'Friendica'); define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_VERSION', '3.6-dev'); define('DFRN_PROTOCOL_VERSION', '2.23'); -define('DB_UPDATE_VERSION', 1236); +define('DB_UPDATE_VERSION', 1237); define('NEW_UPDATE_ROUTINE_VERSION', 1170); /** diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 8f879c032..7a6c3eaa7 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -996,7 +996,6 @@ class DBStructure { "indexes" => array( "PRIMARY" => array("id"), "contactid" => array("contact-id"), - "gid" => array("gid"), "gid_contactid" => array("UNIQUE", "gid", "contact-id"), ) ); diff --git a/update.php b/update.php index 6bb99af57..9701e9e39 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@