473,657 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing query plans

I have a number of very common queries that the optimizer plans a very inefficient plan for. I am using postgres 7.2.3. I vacuum hourly. I'm wonderingwhat I can do to make the queries faster.

Here are the relevant tables:

create table image(
imageid integer not null, /* The image's ID */
containerid integer not null, /* The container that owns it */
name varchar(120) not null, /* Its name */
state bigint not null default 0, /* Its state */
primary key (imageid),
unique (containerid, name) /* All images in a container must be uniquely named */
);

create table ancestry(
containerid integer not null, /* The container that has an ancestor*/
ancestorid integer not null, /* The ancestor of the container */
unique (containerid, ancestorid),
unique (ancestorid, containerid)
);

I have somewhere around 3M rows in the image table, and 37K rows in the ancestry table. The following is representative of some of the common queries I issue:

select * from image natural join ancestry where ancestorid=1000 000 and (state & 7::bigint) = 0::bigint;

When I ask postgres to EXPLAIN it, I get the following:

Merge Join (cost=81858.22. .81900.60 rows=124 width=49)
-> Sort (cost=81693.15. .81693.15 rows=16288 width=41)
-> Seq Scan on image (cost=0.00..802 79.17 rows=16288 width=41)
-> Sort (cost=165.06..1 65.06 rows=45 width=8)
-> Index Scan using ancestry_ancest orid_key on ancestry (cost=0..00..16 3.83 rows=45 width=8)

It appears to me that the query executes as follows:

1. Scan every row in the image table to find those where (state & 7::bigint) = 0::bigint
2. Sort the results
3. Use an index on ancestry to find rows where ancestorid=1000 000
4. Sort the results
5. Join the two

It seems to me that if this query is going to return a small percentage of the rows (which is the common case), it could be done much faster by first joining (all columns involved in the join are indexed), and then by applying the (state & 7::bigint) = 0::bigint constraint to the results. I realize that the query planner is going to have a difficult time estimating the number of rows returned by the bit operator. However, I'd be happy forcing it to always perform the join first, and then apply the state constraint to the results.

Similarly, when I update, I get the following:

explain update image set state=0 from ancestry where ancestorid=1000 000and ancestry.contai nerid=image.con tainerid and (state & 7::bigint) = 0::bigint;

NOTICE: QUERY PLAN:

Merge Join (cost=81841.92. .81884.30 rows=124 width=43)
-> Sort (cost=81676.74. .81676.74 rows=16288 width=39)
-> Seq Scan on image (cost=0.00..802 79.17 rows=16288 width=39)
-> Sort (cost=165.19..1 65.19 rows=45 width=4)
-> Index Scan using ancestry_ancest orid_key on ancestry (cost=0..00..16 3.95 rows=45 width=4)

Is there any way to give the planner a hint, or reword the query and updateso that it executes the way I want?

Thanks in advance.

Robert Wille

Nov 11 '05 #1
0 1598

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

Similar topics

0
3728
by: Jerry Brenner | last post by:
Our users have potentially dirty legacy data that they need to get into our application. We provide a set of staging tables, which map to our source tables, that the users do their ETL into. Every row in the source tables has a generated integer id. Every row in both the source and staging tables has a unique publicid (varchar(22)). All foreign key references in the staging tables are through publicids. (The foreign key reference could...
10
3732
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty well and used a query plan that made sense. Now, I know what you're all thinking... stored procedures have to optimize for variable parameters, etc. Here's what I've tried to fix the issue: 1. Recompiled the stored procedure 2. Created a new,...
1
2775
by: Robert Wille | last post by:
I have a number of very common queries that the optimizer plans a very inefficient plan for. I am using postgres 7.2.3. I vacuum hourly. I'm wonderingwhat I can do to make the queries faster. Here are the relevant tables: create table image( imageid integer not null, /* The image's ID */ containerid integer not null, /* The container that owns it */ name varchar(120) not null, /* Its name */ state bigint not null default 0,...
13
1975
by: Dmitry Tkach | last post by:
Hi, everybody! Here is a weird problem, I ran into... I have two huge (80 million rows each) tables (a and b), with id as a PK on both of them and also an FK from b referencing a. When I try to run a query like: select * from a, b where a.id >= 7901288 and a.id=b.id limit 1; The query takes *forever*.
0
2634
by: plato | last post by:
I'm an end user of a siebel program and i want to get some info on questions relating to queries within the program. Who or where can I get some answers beyond basic query operators? Maybe I should be more specific in case someone here knows. The siebel 7 program has screen tabs which i guess you can say is the parent program e.g. plans then there is a list applet and some view tabs under which is a form applet. The highlighted list...
5
6327
by: sql-db2-dba | last post by:
We have DB2 UDB v8.1 fixpak3 on AIX 5. Production and Development configuarations (at least for DB2) are identical albeit production is a 2-way server while development has only one processor. Tables and indexes have the same schema. In fact, the dev database was taken from a prod backup recently. Size of the tables differ slightly. Yet, on a given query (with 4 tables joined), it took 30-50 times longer to run in prod than on development....
1
359
by: Jon | last post by:
Hi there. I'm trying to create a query (or two) that I can use to produce a spreadsheet but I'm having a bit of trouble. I'm using Access 2000. My db is set up like this: Table1: Intrusive Manhole Survey Fields: IntrusiveEntryID (PK), ManholeID, Date, Sewer System, Firm, Inspector, Benching Description, Manhole Type, Manhole Diameter, Sump Elevation, Source, Notes.
2
301
by: Jwhal | last post by:
I'm having trouble querying one table with multiple entries. I'm not sure if or what kind of joins I need to do this, or if it should be a make table followed by an append, then an update, or what. I have four of these db's to work with. This is the smallest db I have. The largest contains about 260 manholes, and over 500 pipes. Here are my two tables: INTRUSIVE MANHOLE SURVEY: IntrusiveEntryID,Manhole ID,Date,Sewer System,Firm...
5
10746
by: sqlgirl | last post by:
Hi, We are trying to solve a real puzzle. We have a stored procedure that exhibits *drastically* different execution times depending on how its executed. When run from QA, it can take as little as 3 seconds. When it is called from an Excel vba application, it can take up to 180 seconds. Although, at other times, it can take as little as 20 seconds from Excel.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.