Unused indexes removed, queries changed

This commit is contained in:
Michael Vogel 2016-07-26 22:10:13 +02:00
parent 747dc934f7
commit 4961fb3a45
9 changed files with 69 additions and 56 deletions

View file

@ -599,57 +599,64 @@ function posts_from_gcontact($a, $gcontact_id) {
return $o; return $o;
} }
/** /**
* @brief Returns posts from a given contact * @brief Returns posts from a given contact url
* *
* @param App $a argv application class * @param App $a argv application class
* @param int $contact_id contact * @param int $contact_url Contact URL
* *
* @return string posts in HTML * @return string posts in HTML
*/ */
function posts_from_contact($a, $contact_id) { function posts_from_contact_url($a, $contact_url) {
require_once('include/conversation.php'); require_once('include/conversation.php');
$r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id)); // There are no posts with "uid = 0" with connector networks
if (!$r) // This speeds up the query a lot
return false; $r = q("SELECT `network`, `id` AS `author-id` FROM `contact`
WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
dbesc(normalise_link($contact_url)));
if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
$sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
else
$sql = "`item`.`uid` = %d";
$contact = $r[0]; $author_id = intval($r[0]["author-id"]);
if(get_config('system', 'old_pager')) { if (get_config('system', 'old_pager')) {
$r = q("SELECT COUNT(*) AS `total` FROM `item` $r = q("SELECT COUNT(*) AS `total` FROM `item`
WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')", WHERE `author-id` = %d and $sql",
intval(local_user()), intval($author_id),
dbesc(str_replace("https://", "http://", $contact["url"])), intval(local_user()));
dbesc(str_replace("http://", "https://", $contact["url"])));
$a->set_pager_total($r[0]['total']); $a->set_pager_total($r[0]['total']);
} }
$r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`, /*
"SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
`author-name` AS `name`, `owner-avatar` AS `photo`, `author-name` AS `name`, `owner-avatar` AS `photo`,
`owner-link` AS `url`, `owner-avatar` AS `thumb` `owner-link` AS `url`, `owner-avatar` AS `thumb`
FROM `item` FORCE INDEX (`uid_contactid_id`) FROM `item` FORCE INDEX (`authorid_created`)
WHERE `item`.`uid` = %d AND `contact-id` = %d WHERE `item`.`author-id` = %d AND $sql
AND `author-link` IN ('%s', '%s') AND NOT `deleted` AND NOT `moderated` AND `visible`
AND NOT `deleted` AND NOT `moderated` AND `visible`
ORDER BY `item`.`id` DESC LIMIT %d, %d", */
$r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
" ORDER BY `item`.`created` DESC LIMIT %d, %d",
intval($author_id),
intval(local_user()), intval(local_user()),
intval($contact_id),
dbesc(str_replace("https://", "http://", $contact["url"])),
dbesc(str_replace("http://", "https://", $contact["url"])),
intval($a->pager['start']), intval($a->pager['start']),
intval($a->pager['itemspage']) intval($a->pager['itemspage'])
); );
$o .= conversation($a,$r,'community',false); $o = conversation($a,$r,'community',false);
if(!get_config('system', 'old_pager')) if(!get_config('system', 'old_pager')) {
$o .= alt_pager($a,count($r)); $o .= alt_pager($a,count($r));
else } else {
$o .= paginate($a); $o .= paginate($a);
}
return $o; return $o;
} }

View file

@ -65,6 +65,7 @@ class dba {
$this->db = @new mysqli($server,$user,$pass,$db); $this->db = @new mysqli($server,$user,$pass,$db);
if(! mysqli_connect_errno()) { if(! mysqli_connect_errno()) {
$this->connected = true; $this->connected = true;
//mysqli_set_charset($this->db, 'utf8');
} }
} }
else { else {
@ -72,6 +73,7 @@ class dba {
$this->db = mysql_connect($server,$user,$pass); $this->db = mysql_connect($server,$user,$pass);
if($this->db && mysql_select_db($db,$this->db)) { if($this->db && mysql_select_db($db,$this->db)) {
$this->connected = true; $this->connected = true;
//mysql_set_charset('utf8', $this->db);
} }
} }
if(! $this->connected) { if(! $this->connected) {

View file

@ -852,19 +852,19 @@ function db_definition() {
"uid_created" => array("uid","created"), "uid_created" => array("uid","created"),
"uid_unseen_contactid" => array("uid","unseen","contact-id"), "uid_unseen_contactid" => array("uid","unseen","contact-id"),
"uid_network_received" => array("uid","network","received"), "uid_network_received" => array("uid","network","received"),
"uid_received" => array("uid","received"), //"uid_received" => array("uid","received"),
"uid_network_commented" => array("uid","network","commented"), //"uid_network_commented" => array("uid","network","commented"),
"uid_commented" => array("uid","commented"), //"uid_commented" => array("uid","commented"),
"uid_title" => array("uid","title"), "uid_title" => array("uid","title"),
"uid_thrparent" => array("uid","thr-parent"), "uid_thrparent" => array("uid","thr-parent"),
"uid_parenturi" => array("uid","parent-uri"), "uid_parenturi" => array("uid","parent-uri"),
"uid_contactid_id" => array("uid","contact-id","id"), "uid_contactid_id" => array("uid","contact-id","id"),
"uid_contactid_created" => array("uid","contact-id","created"), //"uid_contactid_created" => array("uid","contact-id","created"),
"gcontactid_uid_created" => array("gcontact-id","uid","created"), "gcontactid_uid_created" => array("gcontact-id","uid","created"),
"authorid_created" => array("author-id","created"), "authorid_created" => array("author-id","created"),
"ownerid_created" => array("owner-id","created"), "ownerid_created" => array("owner-id","created"),
"wall_body" => array("wall","body(6)"), //"wall_body" => array("wall","body(6)"),
"uid_visible_moderated_created" => array("uid","visible","moderated","created"), //"uid_visible_moderated_created" => array("uid","visible","moderated","created"),
"uid_uri" => array("uid","uri"), "uid_uri" => array("uid","uri"),
"uid_wall_created" => array("uid","wall","created"), "uid_wall_created" => array("uid","wall","created"),
"resource-id" => array("resource-id"), "resource-id" => array("resource-id"),
@ -873,7 +873,8 @@ function db_definition() {
"contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"), "contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"),
"uid_wall_parent_created" => array("uid","wall","parent","created"), "uid_wall_parent_created" => array("uid","wall","parent","created"),
"uid_type_changed" => array("uid","type","changed"), "uid_type_changed" => array("uid","type","changed"),
"contactid_verb" => array("contact-id","verb"), //"contactid_verb" => array("contact-id","verb"),
"contactid" => array("contact-id"),
"deleted_changed" => array("deleted","changed"), "deleted_changed" => array("deleted","changed"),
"uid_wall_changed" => array("uid","wall","changed"), "uid_wall_changed" => array("uid","wall","changed"),
"uid_eventid" => array("uid","event-id"), "uid_eventid" => array("uid","event-id"),

View file

@ -476,9 +476,10 @@ function onepoll_run(&$argv, &$argc){
// If it seems to be a reply but a header couldn't be found take the last message with matching subject // If it seems to be a reply but a header couldn't be found take the last message with matching subject
if(!x($datarray,'parent-uri') and $reply) { if(!x($datarray,'parent-uri') and $reply) {
$r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d ORDER BY `created` DESC LIMIT 1", $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d AND `network` = '%s' ORDER BY `created` DESC LIMIT 1",
dbesc(protect_sprintf($datarray['title'])), dbesc(protect_sprintf($datarray['title'])),
intval($importer_uid)); intval($importer_uid),
dbesc(NETWORK_MAIL));
if(count($r)) if(count($r))
$datarray['parent-uri'] = $r[0]['parent-uri']; $datarray['parent-uri'] = $r[0]['parent-uri'];
} }

View file

@ -887,16 +887,7 @@ function contact_posts($a, $contact_id) {
$o .= $tab_str; $o .= $tab_str;
$r = q("SELECT `id` FROM `item` WHERE `contact-id` = %d LIMIT 1", intval($contact_id)); $o .= posts_from_contact_url($a, $contact["url"]);
if ($r)
$o .= posts_from_contact($a, $contact_id);
elseif ($contact["url"]) {
$r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
dbesc(normalise_link($contact["url"])));
if ($r[0]["id"] <> 0)
$o .= posts_from_gcontact($a, $r[0]["id"]);
}
return $o; return $o;
} }

View file

@ -137,13 +137,13 @@ function follow_content(&$a) {
$a->page['aside'] = ""; $a->page['aside'] = "";
profile_load($a, "", 0, get_contact_details_by_url($ret["url"])); profile_load($a, "", 0, get_contact_details_by_url($ret["url"]));
// Show last public posts
if ($gcontact_id <> 0) { if ($gcontact_id <> 0) {
$o .= replace_macros(get_markup_template('section_title.tpl'), $o .= replace_macros(get_markup_template('section_title.tpl'),
array('$title' => t('Status Messages and Posts') array('$title' => t('Status Messages and Posts')
)); ));
$o .= posts_from_gcontact($a, $gcontact_id); // Show last public posts
$o .= posts_from_contact_url($a, $ret["url"]);
} }
return $o; return $o;

View file

@ -1032,7 +1032,7 @@ function item_post(&$a) {
// Currently the only realistic fixes are to use a reliable server - which precludes shared hosting, // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
// or cut back on plugins which do remote deliveries. // or cut back on plugins which do remote deliveries.
proc_run('php', "include/notifier.php", $notify_type, "$post_id"); proc_run('php', "include/notifier.php", $notify_type, $post_id);
logger('post_complete'); logger('post_complete');

View file

@ -577,8 +577,8 @@ function network_content(&$a, $update = 0) {
$sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search))); $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
else else
$sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search)))); $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
$sql_order = "`item`.`received`"; $sql_order = "`item`.`id`";
$order_mode = "received"; $order_mode = "id";
} }
} }
if(strlen($file)) { if(strlen($file)) {
@ -596,8 +596,7 @@ function network_content(&$a, $update = 0) {
// only setup pagination on initial page view // only setup pagination on initial page view
$pager_sql = ''; $pager_sql = '';
} } else {
else {
if(get_config('system', 'old_pager')) { if(get_config('system', 'old_pager')) {
$r = q("SELECT COUNT(*) AS `total` $r = q("SELECT COUNT(*) AS `total`
FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = $sql_table.`contact-id` FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = $sql_table.`contact-id`
@ -636,7 +635,7 @@ function network_content(&$a, $update = 0) {
$simple_update = (($update) ? " AND `item`.`unseen` " : ''); $simple_update = (($update) ? " AND `item`.`unseen` " : '');
if ($sql_order == "") if ($sql_order == "")
$sql_order = "`item`.`received`"; $sql_order = "`item`.`id`";
// "New Item View" - show all items unthreaded in reverse created date order // "New Item View" - show all items unthreaded in reverse created date order
$items = q("SELECT %s FROM $sql_table $sql_post_table %s $items = q("SELECT %s FROM $sql_table $sql_post_table %s

View file

@ -174,7 +174,7 @@ function nodeinfo_cron() {
return; return;
$last = get_config('nodeinfo','last_calucation'); $last = get_config('nodeinfo','last_calucation');
/*
if($last) { if($last) {
// Calculate every 24 hours // Calculate every 24 hours
$next = $last + (24 * 60 * 60); $next = $last + (24 * 60 * 60);
@ -183,7 +183,7 @@ function nodeinfo_cron() {
return; return;
} }
} }
logger("cron_start"); */ logger("cron_start");
$users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date` $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid` FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
@ -224,12 +224,24 @@ function nodeinfo_cron() {
set_config('nodeinfo','active_users_monthly', $active_users_monthly); set_config('nodeinfo','active_users_monthly', $active_users_monthly);
} }
//$posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND `uid` != 0 AND `id` = `parent` AND left(body, 6) != '[share'"); // $posts = q("SELECT COUNT(*) AS `local_posts` FROM `thread`
// INNER JOIN `contact` ON `contact`.`id` = `thread`.`contact-id` AND `contact`.`uid` = `thread`.`uid`
// WHERE `contact`.`self`");
$posts = q("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall`");
/*
$posts = q("SELECT COUNT(*) AS local_posts FROM `thread`
INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
WHERE `thread`.`wall` AND NOT `thread`.`private` AND
`thread`.`uid` != 0 AND LEFT(`item`.`body`, 6) != '[share' AND
`thread`.`network` IN ('%s', '%s', '%s')",
dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
*/
/*
$posts = q("SELECT COUNT(*) AS `local_posts` FROM `item` $posts = q("SELECT COUNT(*) AS `local_posts` FROM `item`
INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
WHERE `contact`.`self` and `item`.`id` = `item`.`parent` AND left(body, 6) != '[share' AND `item`.`network` IN ('%s', '%s', '%s')", WHERE `contact`.`self` and `item`.`id` = `item`.`parent` AND left(body, 6) != '[share' AND `item`.`network` IN ('%s', '%s', '%s')",
dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN)); dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
*/
if (!is_array($posts)) if (!is_array($posts))
$local_posts = -1; $local_posts = -1;
else else