troubleshooting Question

optimize query

Avatar of KarlisB
KarlisBFlag for Latvia asked on
16 Comments3 Solutions540 ViewsLast Modified:

I have a following query:
as you see, i have joined atfirst one "item" id table with a auto generated table of dates.
as a result i got table where every item_id has assigned about 365 last dates.
After that i join it to a massive table which contains every day statistics for items. in the end query returns item_id's and dates where data is missing and is not present in statistics table.

I wish to know, is there anything i can do to optimize the query?
or is there another way how to get id's of missing items at certain dates?

item table has about 12 000 records;(after filtering)
statistic table has about 50 000 000 records

first time query call takes 160 sec, after cashing 20 secs ;
am using multicolumn index acording to all 3 fields in where clause on statistics table;

query plan:
"Aggregate  (cost=375007.39..375007.40 rows=1 width=0)"
"  ->  Nested Loop Anti Join  (cost=0.01..375007.39 rows=1 width=0)"
"        ->  Nested Loop  (cost=0.00..3130.29 rows=30000 width=8)"
"              ->  Seq Scan on preces  (cost=0.00..2455.29 rows=30 width=4)"
"                    Filter: (sort_a AND (NOT stop_prece) AND (id = id))"
"              ->  Function Scan on generate_series a  (cost=0.00..12.50 rows=1000 width=4)"
"        ->  Index Scan using realizacija_pkey on realizacija  (cost=0.01..12.90 rows=1 width=8)"
"              Index Cond: ((realizacija.veikala_id = 1) AND ( = realizacija.prece_id) AND (realizacija.datums = (('now'::text)::date - a.a)))"

database type: Postgres

select count(*) 
     select * 
               select current_date - a as dat from generate_series(2,370) as a) Q1
               select id, artikuls  from preces where sort_a=true and stop_prece=false) Q2 
     left join(
              select prece_id,datums
              from realizacija 
              where veikala_id=1) Q3 
     on q3.datums=q1.dat and as Q1 
where datums is null

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros