I am optimizing a query using materialized view in PostgreSQL but query logic is not working in mat view
I want to optimize the query which involves multiple joins and its execution time also higher, so I tried the same query in Materialized view using PostgreSQL but query logic is going wrong when it comes to mat view.
I have tried this mat view creation in PostgreSQL 11.
In the below code, three key tables
1.posts
2.Topics
3.Post_topics.
Post_topics
tables is holding post_id
and topic_id
.
Topics
tables is list of topics (each topic is having multiple values;
example if topic is egg, values associated with egg were 'breakfast','dinner','cheese' etc)
and posts
tables is holding posts relating the topics. Each topic may have multiple posts.
I want to get the count
from topic
table which holds values. So how many counts for breakfast, dinner, cheese when topic id is egg. Topics table is holding 8000 where breakfast,dinner, cheese were also in the value list. If I enter cheese the count for egg, breakfast, cheese should come. In the Normal query I have done this but in mat view, I am struggling to get this logic.
Original Query:
SELECT t.id as topic_id, t.value as value, t.topic as topic, COUNT(c.topic_id) as count
FROM post_topics a
JOIN posts b ON a.post_id = b.id
JOIN post_topics c on a.post_id = c.post_id
--JOIN post_locations pl ON pl.post_id = c.post_id
JOIN topics t on c.topic_id = t.id --AND t.topic = 'cuisine'
WHERE a.topic_id = '1234547hnunsdrinfs'
AND t.id != '1234547hnunsdrinfs'
AND b.date_posted BETWEEN ('2019-06-17'::date - interval '6 month') AND '2019-06-17'::date
GROUP BY t.id, c.topic_id
ORDER BY count DESC
LIMIT 20
I have edited the mat view query with topic id list (Topic table is having 8000 values). In the original query I have mentioned just 1 topic id but I need result for entire 8000. For the below mat view I am getting top topics for each id but counts were not matched.
**Edited Mat View
Create materialized view top_associations_mv AS
SELECT a.topic_id, t.id as topic_id, t.value as value, t.topic as topic, COUNT(c.topic_id) as count
FROM post_topics a
JOIN posts b ON a.post_id = b.id
JOIN post_topics c on a.post_id = c.post_id
JOIN topics t on c.topic_id = t.id
WHERE a.topic_id in ('c108200f-e4dc-415e-9150-3f6c74b879e2', '107f8cad-75b3-43fb-9b2f-f7914bf45155') -- here all 8000 topic id should be placed
--AND t.id <> ( 'c108200f-e4dc-415e-9150-3f6c74b879e2', '107f8cad-75b3-43fb-9b2f-f7914bf45155')--,'c348af9d-dd98-49f1-b6c2-8ea36b404ffa')
and (b.date_posted > (('now'::text)::date - '6 mons'::interval))
GROUP BY t.id, c.topic_id,a.topic_id
ORDER BY count DESC limit 10 ;
Materialized View:
Create MATERIALIZED VIEW top_associations_mv as
SELECT t.id as topic_id, t.value as value, t.topic as topic, COUNT(c.topic_id) as count
FROM post_topics a
JOIN posts b ON a.post_id = b.id
JOIN post_topics c on a.post_id = c.post_id
JOIN topics t on c.topic_id = t.id
WHERE t.id != c.post_id and (b.date_posted > (('now'::text)::date - '6mons'::interval))
GROUP BY t.id, c.topic_id
ORDER BY count DESC
My expected result is:
I want to get the counts from topic table which holds values. So how many counts for breakfast, dinner, cheese when topic id is egg. But in actual result count is wrong.
I really need a help please!!!