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 (preces.id = realizacija.prece_id) AND (realizacija.datums = (('now'::text)::date - a.a)))"
database type: Postgres
select count(*) from( select * from( select current_date - a as dat from generate_series(2,370) as a) Q1 join( select id, artikuls from preces where sort_a=true and stop_prece=false) Q2 on q2.id=q2.id left join( select prece_id,datums from realizacija where veikala_id=1) Q3 on q3.datums=q1.dat and q2.id=q3.prece_id) as Q1 where datums is null
”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
With your subscription - you'll gain access to our exclusive IT community of thousands of IT pros. You'll also be able to connect with highly specified Experts to get personalized solutions to your troubleshooting & research questions. It’s like crowd-sourced consulting.
We can't always guarantee that the perfect solution to your specific problem will be waiting for you. If you ask your own question - our Certified Experts will team up with you to help you get the answers you need.
Our certified Experts are CTOs, CISOs, and Technical Architects who answer questions, write articles, and produce videos on Experts Exchange. 99% of them have full time tech jobs - they volunteer their time to help other people in the technology industry learn and succeed.
We can't guarantee quick solutions - Experts Exchange isn't a help desk. We're a community of IT professionals committed to sharing knowledge. Our experts volunteer their time to help other people in the technology industry learn and succeed.
Our community of experts have been thoroughly vetted for their expertise and industry experience.