Some more improved queries

This commit is contained in:
Michael Vogel 2016-10-21 18:25:21 +00:00
parent fd2d8cc180
commit 501c45def5
9 changed files with 50 additions and 17 deletions

View File

@ -565,9 +565,7 @@
//AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''", //AND `allow_cid`='' AND `allow_gid`='' AND `deny_cid`='' AND `deny_gid`=''",
// count public wall messages // count public wall messages
$r = q("SELECT count(*) as `count` FROM `item` $r = q("SELECT COUNT(*) as `count` FROM `item` WHERE `uid` = %d AND wall",
WHERE `uid` = %d
AND `type`='wall'",
intval($uinfo[0]['uid']) intval($uinfo[0]['uid'])
); );
$countitms = $r[0]['count']; $countitms = $r[0]['count'];

View File

@ -1121,6 +1121,7 @@ function db_definition($charset) {
"indexes" => array( "indexes" => array(
"PRIMARY" => array("id"), "PRIMARY" => array("id"),
"uid_contactid" => array("uid", "contact-id"), "uid_contactid" => array("uid", "contact-id"),
"uid_profile" => array("uid", "profile"),
"uid_album_created" => array("uid", "album", "created"), "uid_album_created" => array("uid", "album", "created"),
"resource-id" => array("resource-id"), "resource-id" => array("resource-id"),
"guid" => array("guid"), "guid" => array("guid"),
@ -1356,6 +1357,7 @@ function db_definition($charset) {
"type_term" => array("type","term"), "type_term" => array("type","term"),
"uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"), "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
"otype_type_term_tid" => array("otype","type","term","tid"), "otype_type_term_tid" => array("otype","type","term","tid"),
"uid_otype_type_url" => array("uid","otype","type","url"),
"guid" => array("guid"), "guid" => array("guid"),
) )
); );

View File

@ -193,13 +193,13 @@ class dfrn {
// AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' ) // AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
// dbesc($check_date), // 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`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
`contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`, `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
`contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
`sign`.`signed_text`, `sign`.`signature`, `sign`.`signer` `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
FROM `item` $sql_post_table 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 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0

View 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); logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
q("COMMIT;");
q("START TRANSACTION;");
$r = dbq("INSERT INTO `item` (`" $r = dbq("INSERT INTO `item` (`"
. implode("`, `", array_keys($arr)) . implode("`, `", array_keys($arr))
. "`) VALUES ('" . "`) VALUES ('"
@ -804,6 +807,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
dbesc($arr['network']), dbesc($arr['network']),
intval($r[0]["id"]) intval($r[0]["id"])
); );
q("COMMIT");
return 0; return 0;
} elseif(count($r)) { } elseif(count($r)) {
@ -813,6 +817,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
item_set_last_item($arr); item_set_last_item($arr);
} else { } else {
logger('item_store: could not locate created item'); logger('item_store: could not locate created item');
q("COMMIT");
return 0; return 0;
} }
@ -891,10 +896,12 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
create_tags_from_item($current_post); create_tags_from_item($current_post);
create_files_from_item($current_post); create_files_from_item($current_post);
q("COMMIT");
// Only check for notifications on start posts // Only check for notifications on start posts
if ($arr['parent-uri'] === $arr['uri']) if ($arr['parent-uri'] === $arr['uri']) {
add_thread($current_post); add_thread($current_post);
else { } else {
update_thread($parent_id); update_thread($parent_id);
add_shadow_entry($arr); add_shadow_entry($arr);
} }

View File

@ -443,7 +443,7 @@ function onepoll_run(&$argv, &$argc){
$refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'"; $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'";
} }
$qstr = implode(',',$refs_arr); $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) intval($importer_uid)
); );
if(count($r)) if(count($r))

View File

@ -806,11 +806,19 @@ class ostatus {
} }
// Get the parent // 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 $parents = q("SELECT `id`, `parent`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `id` IN
(SELECT `parent` 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'))", (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)); intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
*/
if ($parents) if ($parents)
$parent = $parents[0]; $parent = $parents[0];
elseif (count($item) > 0) { elseif (count($item) > 0) {
@ -1961,9 +1969,21 @@ class ostatus {
$last_update = 'now -30 days'; $last_update = 'now -30 days';
$check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s'); $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
$authorid = get_contact($owner["url"], 0);
$items = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id` FROM `item` $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` 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` 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` 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` = '' 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"])),
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 = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;

View File

@ -27,9 +27,9 @@ function display_init(&$a) {
// Or is it anywhere on the server? // Or is it anywhere on the server?
if ($nick == "") { 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` `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` WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
@ -236,7 +236,7 @@ function display_content(&$a, $update = 0) {
} }
if ($nick == "") { 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` WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''

View File

@ -788,6 +788,9 @@ function item_post(&$a) {
} else } else
$post_id = 0; $post_id = 0;
q("COMMIT;");
q("START TRANSACTION;");
$r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`, $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,
`owner-name`,`owner-link`,`owner-avatar`, `owner-id`, `owner-name`,`owner-link`,`owner-avatar`, `owner-id`,
`author-name`, `author-link`, `author-avatar`, `author-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", $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
dbesc($datarray['uri'])); dbesc($datarray['uri']));
if(!count($r)) { if(!count($r)) {
q("COMMIT");
logger('mod_item: unable to retrieve post that was just stored.'); logger('mod_item: unable to retrieve post that was just stored.');
notice( t('System error. Post not saved.') . EOL); notice( t('System error. Post not saved.') . EOL);
goaway($a->get_baseurl() . "/" . $return_path ); goaway($a->get_baseurl() . "/" . $return_path );
@ -986,6 +990,8 @@ function item_post(&$a) {
create_tags_from_item($post_id); create_tags_from_item($post_id);
create_files_from_item($post_id); create_files_from_item($post_id);
q("COMMIT");
if ($post_id == $parent) if ($post_id == $parent)
add_thread($post_id); add_thread($post_id);
else { else {

View File

@ -53,8 +53,8 @@ function photos_init(&$a) {
$sql_extra = permissions_sql($a->data['user']['uid']); $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' $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", $sql_extra GROUP BY `album` ORDER BY `created` DESC",
intval($a->data['user']['uid']), intval($a->data['user']['uid']),
dbesc('Contact Photos'), dbesc('Contact Photos'),
dbesc( t('Contact Photos')) dbesc( t('Contact Photos'))