Search Postgresql Archives

Slow IN query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Using an OR or IN query seems to be orders of magnitudes slower than
running a query twice. There is an unique index on 'id' and an index on
'model_ns, model'. The number of row returned is less than 800.
Everything is vacuumed and analyzed. Running on 7.4.1. Perhaps this
situation is something the optimizer could be cleverer about; if not,
never mind.

explain select * from statements
where model_ns='4' and model in ('P42655', 'Q9XFM4')
order by id;

 Index Scan using statements_pkey on statements  (cost=0.00..2166475.49
rows=107 width=113)
   Filter: ((model_ns = 4::smallint) AND ((model = 'P42655'::text) OR
(model = 'Q9XFM4'::text)))


explain select * from statements
where model_ns='4' and model = 'P42655'
union
select * from statements
where model_ns='4' and model = 'Q9XFM4'
order by id;

 Sort  (cost=425.84..426.11 rows=108 width=113)
   Sort Key: id
   ->  Unique  (cost=418.14..422.19 rows=108 width=113)
         ->  Sort  (cost=418.14..418.41 rows=108 width=113)
               Sort Key: id, model_ns, model, "statement", subject_ns,
subject, predicate_ns, predicate, object_ns, object, object_string,
object_number, object_boolean, generated
               ->  Append  (cost=0.00..414.49 rows=108 width=113)
                     ->  Subquery Scan "*SELECT* 1"  (cost=0.00..207.25
rows=54 width=113)
                           ->  Index Scan using statements_uniprot_idx
on statements  (cost=0.00..206.71 rows=54 width=113)
                                 Index Cond: ((model_ns = 4::smallint)
AND (model = 'P42655'::text))
                     ->  Subquery Scan "*SELECT* 2"  (cost=0.00..207.25
rows=54 width=113)
                           ->  Index Scan using statements_uniprot_idx
on statements  (cost=0.00..206.71 rows=54 width=113)
                                 Index Cond: ((model_ns = 4::smallint)
AND (model = 'Q9XFM4'::text))


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux