I have 3 tables. Stocks have many News articles and News articles can refer to 1 or more Stocks. This is modelled with a Stock table, a News table and a Stock_News table.
How would I get the latest News article for say 30 stock symbols I provide? What indices would make this most efficient?
My News table has id, link, published_at. (index on published_at, id primary key)
My Stocks table has id, symbol. (index on symbol, id primary key)
My stock_news table has stock_id news_id. (index on each individually and combined)
Currently I am using but i as wondering if this is the best way
SELECT n.link, s.symbol, n.published_at FROM news n
JOIN stock_news sn on n.id = sn.news_id
JOIN stocks s on s.id = sn.stock_id where s.symbol in ('AAPL', 'GOOG' ... etc)
ORDER BY n.published_at DESC;
The EXPLAIN query on some demo data shows:
Sort (cost=8.92..8.92 rows=1 width=115)
Sort Key: n.published_at DESC
-> Nested Loop (cost=3.50..8.92 rows=1 width=115)
-> Hash Join (cost=3.45..7.51 rows=1 width=12)
Hash Cond: (s.id = sn.stock_id)
-> Seq Scan on stocks s (cost=0.00..4.05 rows=2 width=12)
Filter: ((symbol)::text = ANY ('{AAPL,GOOG}'::text[]))
-> Hash (cost=2.67..2.67 rows=223 width=16)
-> Seq Scan on stock_news sn (cost=0.00..2.67 rows=223 width=16)
-> Index Scan using news_pkey on news n (cost=0.05..1.40 rows=1 width=119)
Index Cond: (id = sn.news_id)