Skip to content
Snippets Groups Projects
Commit e742b287 authored by Bartek Wrona's avatar Bartek Wrona
Browse files

Added definition of materialized view to speedup posts query using order by author and permlink.

parent e2a4b7ae
No related branches found
No related tags found
5 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!16Dk issue 3 concurrent block query rebase,!15Dk issue 3 concurrent block query
...@@ -44,9 +44,6 @@ INSERT INTO hive_permlink_data (permlink) VALUES (''); ...@@ -44,9 +44,6 @@ INSERT INTO hive_permlink_data (permlink) VALUES ('');
-- RAISE NOTICE 'run on permlink field of hive_posts_cache'; -- RAISE NOTICE 'run on permlink field of hive_posts_cache';
INSERT INTO hive_permlink_data (permlink) SELECT permlink FROM hive_posts ON CONFLICT (permlink) DO NOTHING; INSERT INTO hive_permlink_data (permlink) SELECT permlink FROM hive_posts ON CONFLICT (permlink) DO NOTHING;
-- we should also scan parent_permlink and root_permlink but we will do that on raw_json scan -- we should also scan parent_permlink and root_permlink but we will do that on raw_json scan
-- Create indexes
CREATE INDEX IF NOT EXISTS hive_permlink_data_permlink_idx ON hive_permlink_data (permlink ASC);
CREATE INDEX IF NOT EXISTS hive_permlink_data_permlink_c_idx ON hive_permlink_data (permlink COLLATE "C" ASC);
-- Table to hold category data, category is unique -- Table to hold category data, category is unique
-- RAISE NOTICE 'Table to hold category data, category is unique'; -- RAISE NOTICE 'Table to hold category data, category is unique';
...@@ -68,10 +65,10 @@ CREATE INDEX IF NOT EXISTS hive_category_data_category_c_idx ON hive_category_da ...@@ -68,10 +65,10 @@ CREATE INDEX IF NOT EXISTS hive_category_data_category_c_idx ON hive_category_da
-- Table to hold post data -- Table to hold post data
-- RAISE NOTICE 'Table to hold post data'; -- RAISE NOTICE 'Table to hold post data';
CREATE TABLE IF NOT EXISTS hive_posts_new ( CREATE TABLE IF NOT EXISTS hive_posts_new (
id INT DEFAULT '-1', id INT NOT NULL,
parent_id INT DEFAULT '-1', parent_id INT DEFAULT '-1',
author_id INT DEFAULT '-1', author_id INT NOT NULL,
permlink_id INT DEFAULT '-1', permlink_id INT NOT NULL,
category_id INT DEFAULT '1', category_id INT DEFAULT '1',
community_id INT, community_id INT,
created_at DATE DEFAULT '1990-01-01T00:00:00', created_at DATE DEFAULT '1990-01-01T00:00:00',
...@@ -125,6 +122,9 @@ CREATE TABLE IF NOT EXISTS hive_posts_new ( ...@@ -125,6 +122,9 @@ CREATE TABLE IF NOT EXISTS hive_posts_new (
root_title VARCHAR(255) DEFAULT '' root_title VARCHAR(255) DEFAULT ''
); );
CREATE INDEX IF NOT EXISTS hive_posts_author_id_idx ON hive_posts_new (author_id);
CREATE INDEX IF NOT EXISTS hive_posts_permlink_id_idx ON hive_posts_new (permlink_id);
-- Table to hold bulk post data -- Table to hold bulk post data
-- RAISE NOTICE 'Table to hold bulk post data'; -- RAISE NOTICE 'Table to hold bulk post data';
CREATE TABLE IF NOT EXISTS hive_post_data ( CREATE TABLE IF NOT EXISTS hive_post_data (
...@@ -297,8 +297,6 @@ ALTER TABLE hive_posts ADD CONSTRAINT hive_posts_ux1 UNIQUE (author_id, permlink ...@@ -297,8 +297,6 @@ ALTER TABLE hive_posts ADD CONSTRAINT hive_posts_ux1 UNIQUE (author_id, permlink
-- Make indexes in hive_posts -- Make indexes in hive_posts
-- RAISE NOTICE 'Creating indexes'; -- RAISE NOTICE 'Creating indexes';
CREATE INDEX IF NOT EXISTS hive_posts_author_id_idx ON hive_posts (author_id);
CREATE INDEX IF NOT EXISTS hive_posts_permlink_id_idx ON hive_posts (permlink_id);
CREATE INDEX IF NOT EXISTS hive_posts_depth_idx ON hive_posts (depth); CREATE INDEX IF NOT EXISTS hive_posts_depth_idx ON hive_posts (depth);
CREATE INDEX IF NOT EXISTS hive_posts_parent_id_idx ON hive_posts (parent_id); CREATE INDEX IF NOT EXISTS hive_posts_parent_id_idx ON hive_posts (parent_id);
...@@ -317,3 +315,24 @@ CREATE INDEX IF NOT EXISTS hive_posts_sc_hot_idx ON hive_posts (sc_hot); ...@@ -317,3 +315,24 @@ CREATE INDEX IF NOT EXISTS hive_posts_sc_hot_idx ON hive_posts (sc_hot);
CREATE INDEX IF NOT EXISTS hive_posts_created_at_idx ON hive_posts (created_at); CREATE INDEX IF NOT EXISTS hive_posts_created_at_idx ON hive_posts (created_at);
-- Create a materialized view and associated index to significantly speedup query for hive_posts
drop materialized view if exists hive_posts_a_p;
create materialized view hive_posts_a_p
as
select hp.id as id,
ha_a.name as author,
hpd_p.permlink as permlink
FROM hive_posts hp
inner JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
inner JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
with data
;
drop index if exists hive_posts_a_p_idx;
create unique index hive_posts_a_p_idx
on hive_posts_a_p
(author collate "C", permlink collate "C")
;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment