ali akbar azizkhani
ali akbar azizkhani

Reputation: 2279

postgresql date filed query performance

i have two table . news table have 7m record and news_publish table have 70m record when i execute this query took enormous amounts of time and very slow . i add three index for tuning but query is slow. when i google this problem i found that someone suggest that change statistics to 1000 and i chage it ,but problem is yet

alter table khb_news alter submitteddate set statistics 1000;

SELECT  n.id as newsid ,n.title,p.submitteddate as publishdate,
    n.summary ,n.smallImageid  ,
    n.classification ,n.submitteddate as newsdate,
    p.toorganizationid

from  khb_news  n  
     join   khb_news_publish p    
        on n.id=p.newsid    
    left join dataitem b on b.id=n.classification 

where  
    n.classification in (1) and  n.newstype=60  
    AND n.submitteddate    >= '2014/06/01'::timestamp AND   n.submitteddate <'2014/08/01'::timestamp and p.toorganizationid=123123
order by p.id desc

limit 10 offset 0

indexes is :

CREATE INDEX "p.id"
ON khb_news_publish
USING btree
(id DESC);

CREATE INDEX idx_toorganization
ON khb_news_publish
USING btree
(toorganizationid);


CREATE INDEX "idx_n.classification_n.newstype_n.submitteddate"
ON khb_news
USING btree
(classification, newstype, submitteddate);

after add this indexes and run explain analyze i get this explain

"Limit  (cost=0.99..10100.13 rows=10 width=284) (actual time=24711.831..24712.849 rows=10 loops=1)"
        "  ->  Nested Loop  (cost=0.99..5946373.12 rows=5888 width=284) (actual time=24711.827..24712.837 rows=10 loops=1)"
        "        ->  Index Scan using "p.id" on khb_news_publish p  (cost=0.56..4748906.31 rows=380294 width=32) (actual time=2.068..23338.731 rows=194209 loops=1)"
        "              Filter: (toorganizationid = 95607)"
        "              Rows Removed by Filter: 36333074"
        "        ->  Index Scan using khb_news_pkey on khb_news n  (cost=0.43..3.14 rows=1 width=260) (actual time=0.006..0.006 rows=0 loops=194209)"
        "              Index Cond: (id = p.newsid)"
        "              Filter: ((submitteddate >= '2014-06-01 00:00:00'::timestamp without time zone) AND (submitteddate < '2014-08-01 00:00:00'::timestamp without time zone) AND (newstype = 60) AND (classification = ANY ('{19,20,21}'::bigint[])))"
        "              Rows Removed by Filter: 1"
        "Planning time: 3.871 ms"
        "Execution time: 24712.982 ms"

i add explain in https://explain.depesz.com/s/Gym how can change query to make it faster ??

Upvotes: 0

Views: 47

Answers (1)

Jakub Kania
Jakub Kania

Reputation: 16487

You should start with creating an index on khb_news_publish(toorganizationid, id)

CREATE INDEX idx_toorganization_id
ON khb_news_publish
USING btree
(toorganizationid, id);

This should fix the problem but you might also need index:

CREATE INDEX idx_id_classification_newstype_submitteddate
ON khb_news
USING btree
(classification, newstype, submitteddate, id);

Upvotes: 2

Related Questions