Merge branch 'pg-optimize' into 'master'

drop some pointless queries now that we can use RETURNING for inserts

See merge request tt-rss/tt-rss!130
This commit is contained in:
Andrew Dolgov
2025-05-06 04:44:03 +00:00
4 changed files with 77 additions and 72 deletions

View File

@@ -124,18 +124,15 @@ class Article extends Handler_Protected {
$sth = $pdo->prepare("INSERT INTO ttrss_entries $sth = $pdo->prepare("INSERT INTO ttrss_entries
(title, guid, link, updated, content, content_hash, date_entered, date_updated) (title, guid, link, updated, content, content_hash, date_entered, date_updated)
VALUES VALUES
(?, ?, ?, NOW(), ?, ?, NOW(), NOW())"); (?, ?, ?, NOW(), ?, ?, NOW(), NOW()) RETURNING id");
$sth->execute([$title, $guid, $url, $content, $content_hash]); $sth->execute([$title, $guid, $url, $content, $content_hash]);
$sth = $pdo->prepare("SELECT id FROM ttrss_entries WHERE guid = ?");
$sth->execute([$guid]);
if ($row = $sth->fetch()) { if ($row = $sth->fetch()) {
$ref_id = $row["id"]; $ref_id = $row["id"];
$sth = $pdo->prepare("UPDATE ttrss_entries $sth = $pdo->prepare("UPDATE ttrss_entries
SET tsvector_combined = to_tsvector( :ts_content) SET tsvector_combined = to_tsvector( :ts_content)
WHERE id = :id"); WHERE id = :id");
$params = [ $params = [
":ts_content" => mb_substr(\Soundasleep\Html2Text::convert($content), 0, 900000), ":ts_content" => mb_substr(\Soundasleep\Html2Text::convert($content), 0, 900000),
":id" => $ref_id]; ":id" => $ref_id];

View File

@@ -397,14 +397,10 @@ class OPML extends Handler_Protected {
//print "F: $title, $inverse, $enabled, $match_any_rule"; //print "F: $title, $inverse, $enabled, $match_any_rule";
$sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid) $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
VALUES (?, ?, ?, ?, ?)"); VALUES (?, ?, ?, ?, ?) RETURNING id");
$sth->execute([$match_any_rule, $enabled, $inverse, $title, $owner_uid]); $sth->execute([$match_any_rule, $enabled, $inverse, $title, $owner_uid]);
$sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
owner_uid = ?");
$sth->execute([$owner_uid]);
$row = $sth->fetch(); $row = $sth->fetch();
$filter_id = $row['id']; $filter_id = $row['id'];

View File

@@ -689,14 +689,10 @@ class Pref_Filters extends Handler_Protected {
$sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2
(owner_uid, match_any_rule, enabled, title, inverse) VALUES (owner_uid, match_any_rule, enabled, title, inverse) VALUES
(?, ?, ?, ?, ?)"); (?, ?, ?, ?, ?) RETURNING id");
$sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]); $sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]);
$sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2
WHERE owner_uid = ?");
$sth->execute([$_SESSION['uid']]);
if ($row = $sth->fetch()) { if ($row = $sth->fetch()) {
$filter_id = $row['id']; $filter_id = $row['id'];

View File

@@ -990,15 +990,18 @@ class RSSUtils {
WHERE guid IN (?, ?, ?)"); WHERE guid IN (?, ?, ?)");
$csth->execute([$entry_guid, $entry_guid_hashed, $entry_guid_hashed_compat]); $csth->execute([$entry_guid, $entry_guid_hashed, $entry_guid_hashed_compat]);
if (!$csth->fetch()) { if ($row = $csth->fetch()) {
Debug::log("select returned RID: " . $row['id'], Debug::LOG_VERBOSE);
$base_record_created = false;
} else {
Debug::log("base guid [$entry_guid or $entry_guid_hashed] not found, creating...", Debug::LOG_VERBOSE); Debug::log("base guid [$entry_guid or $entry_guid_hashed] not found, creating...", Debug::LOG_VERBOSE);
// base post entry does not exist, create it // base post entry does not exist, create it
$isth = $pdo->prepare(
$usth = $pdo->prepare(
"INSERT INTO ttrss_entries "INSERT INTO ttrss_entries
(title, (title,
tsvector_combined,
guid, guid,
link, link,
updated, updated,
@@ -1013,34 +1016,49 @@ class RSSUtils {
lang, lang,
author) author)
VALUES VALUES
(?, ?, ?, ?, ?, ?, (:title,
to_tsvector(:ts_lang, :ts_content),
:guid,
:link,
:updated,
:content,
:content_hash,
false, false,
NOW(), NOW(),
?, ?, ?, ?, ?, ?)"); :date_entered,
:comments,
:num_comments,
:plugin_data,
:lang,
:author) RETURNING id");
$usth->execute([$entry_title, $isth->execute([":title" => $entry_title,
$entry_guid_hashed, ":ts_lang" => $feed_language,
$entry_link, ":ts_content" => mb_substr(strip_tags($entry_title) . " " . \Soundasleep\Html2Text::convert($entry_content), 0, 900000),
$entry_timestamp_fmt, ":guid" => $entry_guid_hashed,
"$entry_content", ":link" => $entry_link,
$entry_current_hash, ":updated" => $entry_timestamp_fmt,
$date_feed_processed, ":content" => $entry_content,
$entry_comments, ":content_hash" => $entry_current_hash,
(int)$num_comments, ":date_entered" => $date_feed_processed,
$entry_plugin_data, ":comments" => $entry_comments,
"$entry_language", ":num_comments" => (int)$num_comments,
"$entry_author"]); ":plugin_data" => $entry_plugin_data,
":lang" => $entry_language,
":author" => $entry_author]);
} $row = $isth->fetch();
$csth->execute([$entry_guid, $entry_guid_hashed, $entry_guid_hashed_compat]); Debug::log("insert returned RID: " . $row['id'], Debug::LOG_VERBOSE);
$base_record_created = true;
}
$entry_ref_id = 0; $entry_ref_id = 0;
$entry_int_id = 0; $entry_int_id = 0;
if ($row = $csth->fetch()) { if ($row['id']) {
Debug::log("base guid found, checking for user record", Debug::LOG_VERBOSE); Debug::log("base record with RID: " . $row['id'] . " found, checking for user record", Debug::LOG_VERBOSE);
$ref_id = $row['id']; $ref_id = $row['id'];
$entry_ref_id = $ref_id; $entry_ref_id = $ref_id;
@@ -1100,56 +1118,54 @@ class RSSUtils {
(ref_id, owner_uid, feed_id, unread, last_read, marked, (ref_id, owner_uid, feed_id, unread, last_read, marked,
published, score, tag_cache, label_cache, uuid, published, score, tag_cache, label_cache, uuid,
last_marked, last_published) last_marked, last_published)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, '', '', '', ".$last_marked.", ".$last_published.")"); VALUES (?, ?, ?, ?, ?, ?, ?, ?, '', '', '', ".$last_marked.", ".$last_published.")
RETURNING int_id");
$sth->execute([$ref_id, $feed_obj->owner_uid, $feed, $unread, $last_read_qpart, $marked, $sth->execute([$ref_id, $feed_obj->owner_uid, $feed, $unread, $last_read_qpart, $marked,
$published, $score]); $published, $score]);
if ($row = $sth->fetch())
$entry_int_id = $row['int_id'];
if ($marked) if ($marked)
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_ARTICLES_MARK_TOGGLED, [$ref_id]); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_ARTICLES_MARK_TOGGLED, [$ref_id]);
if ($published) if ($published)
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_ARTICLES_PUBLISH_TOGGLED, [$ref_id]); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_ARTICLES_PUBLISH_TOGGLED, [$ref_id]);
$sth = $pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
ref_id = ? AND owner_uid = ? AND
feed_id = ? LIMIT 1");
$sth->execute([$ref_id, $feed_obj->owner_uid, $feed]);
if ($row = $sth->fetch())
$entry_int_id = $row['int_id'];
} }
Debug::log("resulting RID: $entry_ref_id, IID: $entry_int_id", Debug::LOG_VERBOSE); Debug::log("resulting RID: $entry_ref_id, IID: $entry_int_id", Debug::LOG_VERBOSE);
$sth = $pdo->prepare("UPDATE ttrss_entries // it's pointless to update base record we've just created
SET title = :title, if (!$base_record_created) {
tsvector_combined = to_tsvector(:ts_lang, :ts_content), $sth = $pdo->prepare("UPDATE ttrss_entries
content = :content, SET title = :title,
content_hash = :content_hash, tsvector_combined = to_tsvector(:ts_lang, :ts_content),
updated = :updated, content = :content,
date_updated = NOW(), content_hash = :content_hash,
num_comments = :num_comments, updated = :updated,
plugin_data = :plugin_data, date_updated = NOW(),
author = :author, num_comments = :num_comments,
lang = :lang plugin_data = :plugin_data,
WHERE id = :id"); author = :author,
lang = :lang
WHERE id = :id");
$params = [":title" => $entry_title, $params = [":title" => $entry_title,
":content" => "$entry_content", ":content" => "$entry_content",
":content_hash" => $entry_current_hash, ":content_hash" => $entry_current_hash,
":updated" => $entry_timestamp_fmt, ":updated" => $entry_timestamp_fmt,
":num_comments" => (int)$num_comments, ":num_comments" => (int)$num_comments,
":plugin_data" => $entry_plugin_data, ":plugin_data" => $entry_plugin_data,
":author" => "$entry_author", ":author" => "$entry_author",
":lang" => $entry_language, ":lang" => $entry_language,
":id" => $ref_id, ":id" => $ref_id,
":ts_lang" => $feed_language, ":ts_lang" => $feed_language,
":ts_content" => mb_substr(strip_tags($entry_title) . " " . \Soundasleep\Html2Text::convert($entry_content), 0, 900000) ":ts_content" => mb_substr(strip_tags($entry_title) . " " . \Soundasleep\Html2Text::convert($entry_content), 0, 900000)
]; ];
$sth->execute($params); $sth->execute($params);
}
// update aux data // update aux data
$sth = $pdo->prepare("UPDATE ttrss_user_entries $sth = $pdo->prepare("UPDATE ttrss_user_entries