From 53c06a3625f21539c13daaba49165cc42e3ce837 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 2 Nov 2013 10:49:44 +0100 Subject: [PATCH] Several performance improvements. --- boot.php | 2 +- include/delivery.php | 8 +++---- include/items.php | 52 ++++++++++++++++++++++++++++++++++++---- include/session.php | 22 ++++++++--------- include/socgraph.php | 10 ++++---- mod/dfrn_notify.php | 36 ++++++++++++++-------------- mod/dfrn_poll.php | 40 +++++++++++++++---------------- mod/network.php | 12 +++++++--- update.php | 56 +++++++++++++++++++++++++++++++++++++++++++- 9 files changed, 171 insertions(+), 67 deletions(-) diff --git a/boot.php b/boot.php index a9c2cf1f98..2325c2dac7 100644 --- a/boot.php +++ b/boot.php @@ -14,7 +14,7 @@ require_once('include/features.php'); define ( 'FRIENDICA_PLATFORM', 'Friendica'); define ( 'FRIENDICA_VERSION', '3.2.1744' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); -define ( 'DB_UPDATE_VERSION', 1164 ); +define ( 'DB_UPDATE_VERSION', 1165 ); define ( 'EOL', "
\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); diff --git a/include/delivery.php b/include/delivery.php index 5e37ad2e0c..1de45255e9 100644 --- a/include/delivery.php +++ b/include/delivery.php @@ -52,7 +52,7 @@ function delivery_run(&$argv, &$argc){ ); if(! count($r)) { continue; - } + } $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) @@ -67,7 +67,7 @@ function delivery_run(&$argv, &$argc){ // It's ours to deliver. Remove it from the queue. - q("delete from deliverq where cmd = '%s' and item = %d and contact = %d limit 1", + q("delete from deliverq where cmd = '%s' and item = %d and contact = %d", dbesc($cmd), dbesc($item_id), dbesc($contact_id) @@ -331,7 +331,7 @@ function delivery_run(&$argv, &$argc){ if($x && count($x)) { $write_flag = ((($x[0]['rel']) && ($x[0]['rel'] != CONTACT_IS_SHARING)) ? true : false); if((($owner['page-flags'] == PAGE_COMMUNITY) || ($write_flag)) && (! $x[0]['writable'])) { - q("update contact set writable = 1 where id = %d limit 1", + q("update contact set writable = 1 where id = %d", intval($x[0]['id']) ); $x[0]['writable'] = 1; @@ -430,7 +430,7 @@ function delivery_run(&$argv, &$argc){ if($cmd === 'wall-new') $it = $items[0]; else { - $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", + $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($argv[2]), intval($uid) ); diff --git a/include/items.php b/include/items.php index fb7f488f4d..14d3de09db 100755 --- a/include/items.php +++ b/include/items.php @@ -983,7 +983,23 @@ function item_store($arr,$force_parent = false) { $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : ''); $arr['origin'] = ((x($arr,'origin')) ? intval($arr['origin']) : 0 ); $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid()); + $arr['network'] = ((x($arr,'network')) ? trim($arr['network']) : ''); + if ($arr['network'] == "") { + $r = q("SELECT `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", + intval($arr['contact-id']), + intval($arr['uid']) + ); + + if(count($r)) + $arr['network'] = $r[0]["network"]; + + // Fallback to friendica (why is it empty in some cases?) + if ($arr['network'] == "") + $arr['network'] = NETWORK_DFRN; + + logger("item_store: Set network to ".$arr["network"]." for ".$arr["uri"], LOGGER_DEBUG); + } $arr['thr-parent'] = $arr['parent-uri']; if($arr['parent-uri'] === $arr['uri']) { @@ -1285,7 +1301,7 @@ function tag_deliver($uid,$item_id) { } return; } - + // send a notification @@ -1730,12 +1746,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) else { $resource_id = photo_new_resource(); } - + $img_str = fetch_url($photo_url,true); // guess mimetype from headers or filename $type = guess_image_type($photo_url,true); - - + + $img = new Photo($img_str, $type); if($img->is_valid()) { if($have_photo) { @@ -4082,6 +4098,9 @@ function drop_item($id,$interactive = true) { // clean up item_id and sign meta-data tables + /* + // Old code - caused very long queries and warning entries in the mysql logfiles: + $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)", intval($item['id']), intval($item['uid']) @@ -4091,6 +4110,31 @@ function drop_item($id,$interactive = true) { intval($item['id']), intval($item['uid']) ); + */ + + // The new code splits the queries since the mysql optimizer really has bad problems with subqueries + + // Creating list of parents + $r = q("select id from item where parent = %d and uid = %d", + intval($item['id']), + intval($item['uid']) + ); + + $parentid = ""; + + foreach ($r AS $row) { + if ($parentid != "") + $parentid .= ", "; + + $parentid .= $row["id"]; + } + + // Now delete them + if ($parentid != "") { + $r = q("DELETE FROM item_id where iid in (%s)", dbesc($parentid)); + + $r = q("DELETE FROM sign where iid in (%s)", dbesc($parentid)); + } // If it's the parent of a comment thread, kill all the kids diff --git a/include/session.php b/include/session.php index 6c32e299ff..df3871bd78 100644 --- a/include/session.php +++ b/include/session.php @@ -6,12 +6,12 @@ $session_exists = 0; $session_expire = 180000; -if(! function_exists('ref_session_open')) { +if(! function_exists('ref_session_open')) { function ref_session_open ($s,$n) { return true; }} -if(! function_exists('ref_session_read')) { +if(! function_exists('ref_session_read')) { function ref_session_read ($id) { global $session_exists; if(x($id)) @@ -23,20 +23,20 @@ function ref_session_read ($id) { return ''; }} -if(! function_exists('ref_session_write')) { +if(! function_exists('ref_session_write')) { function ref_session_write ($id,$data) { global $session_exists, $session_expire; - if(! $id || ! $data) { - return false; + if(! $id || ! $data) { + return false; } $expire = time() + $session_expire; $default_expire = time() + 300; if($session_exists) - $r = q("UPDATE `session` - SET `data` = '%s', `expire` = '%s' - WHERE `sid` = '%s' LIMIT 1", + $r = q("UPDATE `session` + SET `data` = '%s', `expire` = '%s' + WHERE `sid` = '%s'", dbesc($data), dbesc($expire), dbesc($id)); else $r = q("INSERT INTO `session` @@ -46,18 +46,18 @@ function ref_session_write ($id,$data) { return true; }} -if(! function_exists('ref_session_close')) { +if(! function_exists('ref_session_close')) { function ref_session_close() { return true; }} -if(! function_exists('ref_session_destroy')) { +if(! function_exists('ref_session_destroy')) { function ref_session_destroy ($id) { q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id)); return true; }} -if(! function_exists('ref_session_gc')) { +if(! function_exists('ref_session_gc')) { function ref_session_gc($expire) { q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time())); q("OPTIMIZE TABLE `sess_data`"); diff --git a/include/socgraph.php b/include/socgraph.php index 45048d8da5..24f584db76 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -92,8 +92,8 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) { } if((! $name) || (! $profile_url) || (! $profile_photo)) - continue; - + continue; + $x = q("select * from `gcontact` where `nurl` = '%s' limit 1", dbesc(normalise_link($profile_url)) ); @@ -102,8 +102,8 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) { $gcid = $x[0]['id']; if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) { - q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s' - where `nurl` = '%s' limit 1", + q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s' + where `nurl` = '%s'", dbesc($name), dbesc($profile_photo), dbesc($connect_url), @@ -146,7 +146,7 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) { ); } else { - q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1", + q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d", dbesc(datetime_convert()), intval($cid), intval($uid), diff --git a/mod/dfrn_notify.php b/mod/dfrn_notify.php index 44761be153..7f160de443 100644 --- a/mod/dfrn_notify.php +++ b/mod/dfrn_notify.php @@ -40,7 +40,7 @@ function dfrn_notify_post(&$a) { xml_status(3); } - $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1", + $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'", dbesc($dfrn_id), dbesc($challenge) ); @@ -62,21 +62,21 @@ function dfrn_notify_post(&$a) { xml_status(3); break; // NOTREACHED } - + // be careful - $importer will contain both the contact information for the contact // sending us the post, and also the user information for the person receiving it. // since they are mixed together, it is easy to get them confused. - $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, - `contact`.`pubkey` AS `cpubkey`, - `contact`.`prvkey` AS `cprvkey`, - `contact`.`thumb` AS `thumb`, + $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`, + `contact`.`pubkey` AS `cpubkey`, + `contact`.`prvkey` AS `cprvkey`, + `contact`.`thumb` AS `thumb`, `contact`.`url` as `url`, `contact`.`name` as `senderName`, - `user`.* - FROM `contact` - LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` - WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 + `user`.* + FROM `contact` + LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` + WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1", dbesc($a->argv[1]) ); @@ -87,12 +87,12 @@ function dfrn_notify_post(&$a) { //NOTREACHED } - // $importer in this case contains the contact record for the remote contact joined with the user record of our user. + // $importer in this case contains the contact record for the remote contact joined with the user record of our user. $importer = $r[0]; if((($writable != (-1)) && ($writable != $importer['writable'])) || ($importer['forum'] != $forum) || ($importer['prv'] != $prv)) { - q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d LIMIT 1", + q("UPDATE `contact` SET `writable` = %d, forum = %d, prv = %d WHERE `id` = %d", intval(($writable == (-1)) ? $importer['writable'] : $writable), intval($forum), intval($prv), @@ -117,7 +117,7 @@ function dfrn_notify_post(&$a) { * Relationship is dissolved permanently */ - require_once('include/Contact.php'); + require_once('include/Contact.php'); contact_remove($importer['id']); logger('relationship dissolved : ' . $importer['name'] . ' dissolved ' . $importer['username']); xml_status(0); @@ -218,8 +218,8 @@ function dfrn_notify_content(&$a) { break; // NOTREACHED } - $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` - WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' + $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` + WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $sql_extra LIMIT 1", dbesc($a->argv[1]) ); @@ -265,13 +265,13 @@ function dfrn_notify_content(&$a) { header("Content-type: text/xml"); - echo '' . "\r\n" + echo '' . "\r\n" . '' . "\r\n" . "\t" . '' . $status . '' . "\r\n" . "\t" . '' . DFRN_PROTOCOL_VERSION . '' . "\r\n" . "\t" . '' . $rino . '' . "\r\n" - . "\t" . '' . $perm . '' . "\r\n" - . "\t" . '' . $encrypted_id . '' . "\r\n" + . "\t" . '' . $perm . '' . "\r\n" + . "\t" . '' . $encrypted_id . '' . "\r\n" . "\t" . '' . $challenge . '' . "\r\n" . '' . "\r\n" ; diff --git a/mod/dfrn_poll.php b/mod/dfrn_poll.php index 90b3583b86..b9fcd8c4a4 100644 --- a/mod/dfrn_poll.php +++ b/mod/dfrn_poll.php @@ -43,7 +43,7 @@ function dfrn_poll_init(&$a) { http_status_exit(403); $user = $r[0]['nickname']; } - + logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user); header("Content-type: application/atom+xml"); echo get_feed_for($a, '', $user,$last_update); @@ -71,13 +71,13 @@ function dfrn_poll_init(&$a) { break; // NOTREACHED } - $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` + $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` - WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 + WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1", dbesc($a->argv[1]) ); - + if(count($r)) { $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $my_id . '&type=profile-check'); @@ -104,10 +104,10 @@ function dfrn_poll_init(&$a) { // Visitors get 1 day session. $session_id = session_id(); $expire = time() + 86400; - q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1", + q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'", dbesc($expire), dbesc($session_id) - ); + ); } } $profile = $r[0]['nickname']; @@ -211,13 +211,13 @@ function dfrn_poll_post(&$a) { $ptype = ((x($_POST,'type')) ? $_POST['type'] : ''); $dfrn_version = ((x($_POST,'dfrn_version')) ? (float) $_POST['dfrn_version'] : 2.0); $perm = ((x($_POST,'perm')) ? $_POST['perm'] : 'r'); - + if($ptype === 'profile-check') { if((strlen($challenge)) && (strlen($sec))) { logger('dfrn_poll: POST: profile-check'); - + q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time())); $r = q("SELECT * FROM `profile_check` WHERE `sec` = '%s' ORDER BY `expire` DESC LIMIT 1", dbesc($sec) @@ -289,7 +289,7 @@ function dfrn_poll_post(&$a) { $type = $r[0]['type']; $last_update = $r[0]['last_update']; - $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1", + $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s'", dbesc($dfrn_id), dbesc($challenge) ); @@ -323,7 +323,7 @@ function dfrn_poll_post(&$a) { $contact = $r[0]; $owner_uid = $r[0]['uid']; - $contact_id = $r[0]['id']; + $contact_id = $r[0]['id']; if($type === 'reputation' && strlen($url)) { @@ -356,7 +356,7 @@ function dfrn_poll_post(&$a) { } else { - // Update the writable flag if it changed + // Update the writable flag if it changed logger('dfrn_poll: post request feed: ' . print_r($_POST,true),LOGGER_DATA); if($dfrn_version >= 2.21) { if($perm === 'rw') @@ -365,13 +365,13 @@ function dfrn_poll_post(&$a) { $writable = 0; if($writable != $contact['writable']) { - q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1", + q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d", intval($writable), intval($contact_id) ); } } - + header("Content-type: application/atom+xml"); $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update, $direction); echo $o; @@ -440,9 +440,9 @@ function dfrn_poll_content(&$a) { $nickname = $a->argv[1]; - $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` + $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` - WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 + WHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 AND `user`.`nickname` = '%s' $sql_extra LIMIT 1", dbesc($nickname) ); @@ -522,8 +522,8 @@ function dfrn_poll_content(&$a) { logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash); logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec); - - + + if(((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) { $_SESSION['authenticated'] = 1; if(! x($_SESSION,'remote')) @@ -537,12 +537,12 @@ function dfrn_poll_content(&$a) { // Visitors get 1 day session. $session_id = session_id(); $expire = time() + 86400; - q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1", + q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s'", dbesc($expire), dbesc($session_id) - ); + ); } - + goaway($dest); } goaway($dest); diff --git a/mod/network.php b/mod/network.php index 371b4ee684..38cd615255 100644 --- a/mod/network.php +++ b/mod/network.php @@ -572,7 +572,8 @@ function network_content(&$a, $update = 0) { $sql_options = (($star) ? " and starred = 1 " : ''); $sql_options .= (($bmark) ? " and bookmark = 1 " : ''); - $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : ''); + //$sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : ''); + $sql_nets = (($nets) ? sprintf(" and `item`.`network` = '%s' ", dbesc($nets)) : ''); $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) "; @@ -608,7 +609,9 @@ function network_content(&$a, $update = 0) { intval($cid) ); if(count($r)) { - $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) "; + $sql_table = "`item` INNER JOIN (SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = ".intval($cid)." and deleted = 0 ORDER BY `item`.`received` DESC) AS `temp1` ON item.parent = `temp1`.parent "; + $sql_extra = ""; + //$sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) "; $o = '

' . t('Contact: ') . $r[0]['name'] . '

' . $o; if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) { notice( t('Private messages to this person are at risk of public disclosure.') . EOL); @@ -639,7 +642,9 @@ function network_content(&$a, $update = 0) { $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` "); $sql_extra3 = (($nouveau) ? '' : $sql_extra3); $sql_order = "`item`.`received`"; - $sql_table = "`item`"; + + if ($sql_table == "") + $sql_table = "`item`"; if(x($_GET,'search')) { $search = escape_tags($_GET['search']); @@ -890,3 +895,4 @@ function network_content(&$a, $update = 0) { return $o; } + diff --git a/update.php b/update.php index d48be0a7a5..2d2d2d178b 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@