473,516 Members | 2,910 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=1000000 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..80279.17 rows=16288 width=41)
-> Sort (cost=165.06..165.06 rows=45 width=8)
-> Index Scan using ancestry_ancestorid_key on ancestry (cost=0..00..163.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=1000000
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=1000000and ancestry.containerid=image.containerid 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..80279.17 rows=16288 width=39)
-> Sort (cost=165.19..165.19 rows=45 width=4)
-> Index Scan using ancestry_ancestorid_key on ancestry (cost=0..00..163.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 1589

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

Similar topics

0
3724
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...
10
3719
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....
1
2756
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...
13
1944
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
2628
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...
5
6306
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...
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...
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...
5
10729
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...
0
7276
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...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
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...
0
3267
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...
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.