472,352 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

planer don't use index. bad plan for where id = x or id in (select...)

Hello,

Pg make query 1. and 2. very fast (use index), but for query 3. dont use
index. I can solve its using select union, but I readed so pg 7.5 don't
problem with OR operator. I use cvs pg. I used vacuum analyze first.

table sp_op_product has 15K rows, sp_op_uct 37K rows;

regards
Pavel Stehule

query 1.

intra=# explain analyse select * from sp_op_uct where sp_op_id in (select
sp_op_id from sp_op_produkt where bal_zak = 34123);
QUERY
PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=7.81..74.59 rows=17 width=371) (actual
time=0.241..0.249 rows=1 loops=1)
-> HashAggregate (cost=7.81..7.81 rows=12 width=4) (actual
time=0.118..0.120 rows=1 loops=1)
-> Index Scan using sp_op_product_bal_zak on sp_op_produkt
(cost=0.00..7.78 rows=12 width=4) (actual time=0.077..0.082 rows=1
loops=1)
Index Cond: (bal_zak = 34123)
-> Index Scan using sp_op_uct_sp_op_id on sp_op_uct (cost=0.00..5.54
rows=2 width=371) (actual time=0.068..0.072 rows=1 loops=1)
Index Cond: (sp_op_uct.sp_op_id = "outer".sp_op_id)
Total runtime: 1.846 ms

query 2.

intra=# explain ANALYZE select * from sp_op_uct where sp_op_id = 34123;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Index Scan using sp_op_uct_sp_op_id on sp_op_uct (cost=0.00..5.54 rows=2
width=371) (actual time=0.066..0.072 rows=1 loops=1)
Index Cond: (sp_op_id = 34123)
Total runtime: 0.287 ms

slow query 3.
intra=# EXPLAIN ANALYZE select * from sp_op_uct where sp_op_id = 34123 or
sp_op_id in (select sp_op_id from sp_op_produkt where bal_zak = 34123) ;
QUERY
PLAN
----------------------------------------------------------------------------------------------------------------------------------------------
Seq Scan on sp_op_uct (cost=7.81..1283.83 rows=18602 width=371) (actual
time=3.176..240.379 rows=2 loops=1)
Filter: ((sp_op_id = 34123) OR (hashed subplan))
SubPlan
-> Index Scan using sp_op_product_bal_zak on sp_op_produkt
(cost=0.00..7.78 rows=12 width=4) (actual time=0.441..0.449 rows=1
loops=1)
Index Cond: (bal_zak = 34123)
Total runtime: 240.868 ms

Fast query 4.

intra=# explain ANALYZE select * from sp_op_uct where sp_op_id in (select
sp_op_id from sp_op_produkt where bal_zak = 34123) union select * from
sp_op_uct where sp_op_id = 34123;

QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=80.72..82.39 rows=19 width=371) (actual time=0.857..0.868
rows=2 loops=1)
-> Sort (cost=80.72..80.77 rows=19 width=371) (actual
time=0.852..0.854 rows=2 loops=1)
Sort Key: sp_op_id, perioda, uozac, uokon, setup, timely,
uamount, "LIMIT", krok, kdy, uctuj, tarif, jednotka, merfunc, tarif_id,
hl_cinnost, merene, typ_fakturace, region, prod_group, rychlost, sdilene,
produkt, vl_sluzba, per_jedn, vpn, prov_inst, pevne_komutovane, spravnost,
fakt_text, pres_rychlost, pm, pm2, sp_kvalita
-> Append (cost=7.81..80.32 rows=19 width=371) (actual
time=0.247..0.317 rows=2 loops=1)
-> Subquery Scan "*SELECT* 1" (cost=7.81..74.76 rows=17
width=371) (actual time=0.244..0.256 rows=1 loops=1)
-> Nested Loop (cost=7.81..74.59 rows=17 width=371)
(actual time=0.197..0.205 rows=1 loops=1)
-> HashAggregate (cost=7.81..7.81 rows=12
width=4) (actual time=0.096..0.098 rows=1 loops=1)
-> Index Scan using
sp_op_product_bal_zak on sp_op_produkt (cost=0.00..7.78 rows=12 width=4)
(actual time=0.040..0.044 rows=1 loops=1)
Index Cond: (bal_zak = 34123)
-> Index Scan using sp_op_uct_sp_op_id on
sp_op_uct (cost=0.00..5.54 rows=2 width=371) (actual time=0.045..0.050
rows=1 loops=1)
Index Cond: (sp_op_uct.sp_op_id =
"outer".sp_op_id)
-> Subquery Scan "*SELECT* 2" (cost=0.00..5.56 rows=2
width=371) (actual time=0.048..0.053 rows=1 loops=1)
-> Index Scan using sp_op_uct_sp_op_id on sp_op_uct
(cost=0.00..5.54 rows=2 width=371) (actual time=0.019..0.022 rows=1
loops=1)
Index Cond: (sp_op_id = 34123)
Total runtime: 2.413 ms

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 1800
Pavel Stehule <st*****@kix.fsv.cvut.cz> writes:
Hello,

Pg make query 1. and 2. very fast (use index), but for query 3. dont use
index. I can solve its using select union, but I readed so pg 7.5 don't
problem with OR operator. I use cvs pg. I used vacuum analyze first.
I don't think even in CVS that there's much hope on this query with the OR
between the IN clause and a plain equals clause, but there are a few
improvements you could make to the UNION form:
intra=# explain ANALYZE select * from sp_op_uct where sp_op_id in (select
sp_op_id from sp_op_produkt where bal_zak = 34123) union select * from
sp_op_uct where sp_op_id = 34123;


Try

SELECT *
FROM sp_op_uct
WHERE sp_op_id IN (
SELECT sp_op_id
FROM sp_op_produkt
WHERE bal_zak = 34123
UNION ALL
SELECT 34123 AS sp_op_id
)

You can select just the plain constant 34123 instead of having to fetch the
row from sp_op_uct. This probably won't save much actual time since the record
would have been in cache anyways. And you can use UNION ALL instead of UNION
since the IN will eliminate duplicates anyways. This avoids an extra
sort/uniquify step. Again it wouldn't really save much time since the extra
sort was only across 2 rows. But I like seeing simple clean plans even if they
aren't really much faster.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
Greg Stark <gs*****@mit.edu> writes:
... And you can use UNION ALL instead of UNION
since the IN will eliminate duplicates anyways. This avoids an extra
sort/uniquify step.


FWIW, CVS tip realizes that it doesn't need two unique-ification steps
in this scenario. But I agree that UNION ALL is the cleaner way to
write the query.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alvar Freude | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, the following I posted already on pgsql-bugs -- perhaps someone has a good workaround or...
17
by: Dima Tkach | last post by:
Hi, everybody! I just ran into a weird problem on 7.3.4. Here is a simple testcase: rapidb=# create table nametab (name text); CREATE TABLE...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very...
2
by: Michal Hlavac | last post by:
hello, I have in db table with 3 columns... table name is l_model_to_part columns: i_model_id, i_part_id, i_year I have index on every column...
3
by: Kevin Macdonald | last post by:
I expected Postgresql to use an indexed access method, but in certain cases it is using a sequential scan. Details are below: Table: ...
6
by: Dan Pelleg | last post by:
I'm trying to access a table with about 120M rows. It's a vertical version of a table with 360 or so columns. The new columns are: original item...
5
by: Victor | last post by:
Hello, I have a table which has around 3 billion records on an env and a simple query against it goes for a tablescan eventhough an index has been...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of...
1
by: Steffen Stellwag | last post by:
Truely is often better to scan a table in full passing by an index , but if you can force the optimizer to use an index via a hint for testing and...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.