From cc2e13fe474e8a2039ec9d66d25f71da316e51a7 Mon Sep 17 00:00:00 2001 From: Dariusz Kedzierski <dkedzierski@syncad.com> Date: Fri, 12 Jun 2020 15:07:52 +0200 Subject: [PATCH] Performance fixes for queries --- hive/server/bridge_api/methods.py | 12 +++++------- hive/server/bridge_api/objects.py | 5 ++++- hive/server/common/objects.py | 5 ++++- hive/server/condenser_api/methods.py | 13 ++++++------- hive/server/condenser_api/objects.py | 5 ++++- hive/server/database_api/methods.py | 15 +++++++-------- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/hive/server/bridge_api/methods.py b/hive/server/bridge_api/methods.py index 9ec7a7205..23d4ff0d8 100644 --- a/hive/server/bridge_api/methods.py +++ b/hive/server/bridge_api/methods.py @@ -21,20 +21,20 @@ SQL_TEMPLATE = """ community_id, ha_a.name as author, hpd_p.permlink as permlink, - hpd.title as title, - hpd.body as body, - hcd.category as category, + (SELECT title FROM hive_post_data WHERE hive_post_data.id = hp.id) as title, + (SELECT body FROM hive_post_data WHERE hive_post_data.id = hp.id) as body, + (SELECT category FROM hive_category_data WHERE hive_category_data.id = hp.category_id) as category, depth, promoted, payout, payout_at, is_paidout, children, - hpd.votes as votes, + (SELECT votes FROM hive_post_data WHERE hive_post_data.id = hp.id) as votes, hp.created_at, updated_at, rshares, - hpd.json as json, + (SELECT json FROM hive_post_data WHERE hive_post_data.id = hp.id) as json, is_hidden, is_grayed, total_votes, @@ -48,8 +48,6 @@ SQL_TEMPLATE = """ FROM hive_posts hp LEFT JOIN hive_accounts ha_a ON ha_a.id = hp.author_id LEFT JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - LEFT JOIN hive_post_data hpd ON hpd.id = hp.id - LEFT JOIN hive_category_data hcd ON hcd.id = hp.category_id LEFT OUTER JOIN hive_communities ON (hp.community_id = hive_communities.id) LEFT OUTER JOIN hive_roles ON (ha_a.id = hive_roles.account_id AND hp.community_id = hive_roles.community_id) """ diff --git a/hive/server/bridge_api/objects.py b/hive/server/bridge_api/objects.py index f4d7404f5..00d245de7 100644 --- a/hive/server/bridge_api/objects.py +++ b/hive/server/bridge_api/objects.py @@ -255,7 +255,10 @@ def _condenser_post_object(row, truncate_body=0): post['promoted'] = _amount(row['promoted']) post['replies'] = [] - post['active_votes'] = json.loads(row['votes']) + try: + post['active_votes'] = json.loads(row['votes']) + except Exception: + post['active_votes'] = _hydrate_active_votes(row['votes']) post['author_reputation'] = row['author_rep'] post['stats'] = { diff --git a/hive/server/common/objects.py b/hive/server/common/objects.py index 37f81b5e5..54ba3e2cb 100644 --- a/hive/server/common/objects.py +++ b/hive/server/common/objects.py @@ -60,7 +60,10 @@ def condenser_post_object(row, truncate_body=0): post['replies'] = [] post['body_length'] = len(row['body']) - post['active_votes'] = json.loads(row['votes']) + try: + post['active_votes'] = json.loads(row['votes']) + except Exception: + post['active_votes'] = _hydrate_active_votes(row['votes']) #post['author_reputation'] = rep_to_raw(row['author_rep']) post['root_author'] = row['root_author'] diff --git a/hive/server/condenser_api/methods.py b/hive/server/condenser_api/methods.py index ea44ad5d6..d060e52c5 100644 --- a/hive/server/condenser_api/methods.py +++ b/hive/server/condenser_api/methods.py @@ -23,20 +23,20 @@ SQL_TEMPLATE = """ community_id, ha_a.name as author, hpd_p.permlink as permlink, - hpd.title as title, - hpd.body as body, - hcd.category as category, + (SELECT title FROM hive_post_data WHERE hive_post_data.id = hp.id) as title, + (SELECT body FROM hive_post_data WHERE hive_post_data.id = hp.id) as body, + (SELECT category FROM hive_category_data WHERE hive_category_data.id = hp.category_id) as category, depth, promoted, payout, payout_at, is_paidout, children, - hpd.votes as votes, + (SELECT votes FROM hive_post_data WHERE hive_post_data.id = hp.id) as votes, hp.created_at, updated_at, rshares, - hpd.json as json, + (SELECT json FROM hive_post_data WHERE hive_post_data.id = hp.id) as json, is_hidden, is_grayed, total_votes, @@ -58,12 +58,11 @@ SQL_TEMPLATE = """ FROM hive_posts hp LEFT JOIN hive_accounts ha_a ON ha_a.id = hp.author_id LEFT JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - LEFT JOIN hive_post_data hpd ON hpd.id = hp.id - LEFT JOIN hive_category_data hcd ON hcd.id = hp.category_id LEFT JOIN hive_accounts ha_pa ON ha_pa.id = hp.parent_author_id LEFT JOIN hive_permlink_data hpd_pp ON hpd_pp.id = hp.parent_permlink_id LEFT JOIN hive_accounts ha_ra ON ha_ra.id = hp.root_author_id LEFT JOIN hive_permlink_data hpd_rp ON hpd_rp.id = hp.root_permlink_id + WHERE """ @return_error_info diff --git a/hive/server/condenser_api/objects.py b/hive/server/condenser_api/objects.py index 68ba8ceb7..eb1bf8fee 100644 --- a/hive/server/condenser_api/objects.py +++ b/hive/server/condenser_api/objects.py @@ -210,7 +210,10 @@ def _condenser_post_object(row, truncate_body=0): post['replies'] = [] post['body_length'] = len(row['body']) - post['active_votes'] = json.loads(row['votes']) + try: + post['active_votes'] = json.loads(row['votes']) + except Exception: + post['active_votes'] = _hydrate_active_votes(row['votes']) post['author_reputation'] = rep_to_raw(row['author_rep']) post['root_author'] = row['root_author'] diff --git a/hive/server/database_api/methods.py b/hive/server/database_api/methods.py index afffc8c3f..c9e8a6525 100644 --- a/hive/server/database_api/methods.py +++ b/hive/server/database_api/methods.py @@ -7,20 +7,20 @@ SQL_TEMPLATE = """ community_id, ha_a.name as author, hpd_p.permlink as permlink, - hpd.title as title, - hpd.body as body, - hcd.category as category, + (SELECT title FROM hive_post_data WHERE hive_post_data.id = hp.id) as title, + (SELECT body FROM hive_post_data WHERE hive_post_data.id = hp.id) as body, + (SELECT category FROM hive_category_data WHERE hive_category_data.id = hp.category_id) as category, depth, promoted, payout, payout_at, is_paidout, children, - hpd.votes as votes, + (SELECT votes FROM hive_post_data WHERE hive_post_data.id = hp.id) as votes, hp.created_at, updated_at, rshares, - hpd.json as json, + (SELECT json FROM hive_post_data WHERE hive_post_data.id = hp.id) as json, is_hidden, is_grayed, total_votes, @@ -41,8 +41,6 @@ SQL_TEMPLATE = """ FROM hive_posts hp LEFT JOIN hive_accounts ha_a ON ha_a.id = hp.author_id LEFT JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id - LEFT JOIN hive_post_data hpd ON hpd.id = hp.id - LEFT JOIN hive_category_data hcd ON hcd.id = hp.category_id LEFT JOIN hive_accounts ha_pa ON ha_pa.id = hp.parent_author_id LEFT JOIN hive_permlink_data hpd_pp ON hpd_pp.id = hp.parent_permlink_id LEFT JOIN hive_accounts ha_ra ON ha_ra.id = hp.root_author_id @@ -94,7 +92,8 @@ async def list_comments(context, start: list, limit: int, order: str): assert len(start) == 2, "Expecting two arguments" sql = str(SQL_TEMPLATE) - sql += 'ha_a.name >= :author COLLATE "C" AND hpd_p.permlink >= :permlink COLLATE "C" ORDER BY ha_a.name COLLATE "C" ASC LIMIT :limit' + sql += """ hp.id IN (SELECT hp1.id FROM hive_posts_a_p hp1 WHERE hp1.author >= :author COLLATE "C" + AND hp1.permlink >= :permlink COLLATE "C" ORDER BY hp1.author COLLATE "C" ASC LIMIT :limit)""" result = await db.query_all(sql, author=start[0], permlink=start[1], limit=limit) for row in result: -- GitLab