We help IT Professionals succeed at work.
Get Started

optimize query

KarlisB
KarlisB asked
on
539 Views
Last Modified: 2012-05-09
Greetings!

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

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 3 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE