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 1 2774 rw****@iarchive s.com ("Robert Wille") wrote in message news:<012c01c34 0ce$dfc89110$64 02a8c0@zucchini >... I have a number of very common queries that the optimizer plans a very inef ficient plan for. I am using postgres 7.2.3. I vacuum hourly. I'm wondering what 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 uni quely 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 anc estry table. The following is representative of some of the common queries I issue:
select * from image natural join ancestry where ancestorid=1000 000 and (s tate & 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 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=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 applyin g the (state & 7::bigint) = 0::bigint constraint to the results. I realiz e that the query planner is going to have a difficult time estimating the n umber of rows returned by the bit operator. However, I'd be happy forcing i t 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 000 and 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 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 update so that it executes the way I want?
Thanks in advance.
Robert Wille
--
Try an index on the condition of the join -
CREATE INDEX "stateplus7isno ne" on image (containerid) where
(state&7=0);
select * from image i inner join ancestry a using (containerid) where
ancestorid=1000 000 and (state&7) = 0;
the index will be used in the query you give - hopefully speeding
access to the data.
--
Tom Hebbron This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian Oster |
last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a
query that used to execute in under 2 seconds, now takes over 8
Minutes to complete. Any ideas on what the heck might be going on?
I have tested this extensively and can say for certain that installing
this hot fix is what has caused the performance problem. I just don't
know why or how to fix it.
Brian Oster
|
by: Paul Mateer |
last post by:
Hi,
I have been running some queries against a table in a my database and
have noted an odd (at least it seems odd to me) performance issue.
The table has approximately 5 million rows and includes the following
columns:
DocID (INTEGER, PRIMARY KEY, CLUSTERED)
IsRecord (INTEGER, NONCLUSTERED)
|
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...
|
by: Bob |
last post by:
Hi there,
Need a little help with a certain query that's causing a lot of acid
in my stomach...
Have a table that stores sales measures for a given client. The sales
measures are stored per year and there could be multiple sales
measures every year per client. There is another field called last
update date. If there are multiple sales measures then need to select
the one that's been entered last based on this field. Also, if there
|
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,...
| |
by: apb18 |
last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree
such that the columns 'ID' and 'field' appear in the top level table,
call that table XXX. tables YYY and ZZZ both inherit XXX. Now suppose
there exists some query that returns a set of IDs that match some criteria
(that query may involve various tests/joins/etc on other arbitrary
tables). Executing that query alone produces an optimal plan and the
exact result set...
|
by: Együd Csaba |
last post by:
Hi All,
how can I improve the query performance in the following situation:
I have a big (4.5+ million rows) table. One query takes approx. 9 sec to
finish resulting ~10000 rows. But if I run simultaneously 4 similar queries
it takes nearly 5 minutes instead of 4 times 9 seconds or something near of
that.
here is a sample query:
select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon
in (select distinct fomeazon...
|
by: Ryan |
last post by:
I came across a situation that I've been unable to explain and was
hoping somebody had an answer:
I had written an update query which was taking about 8 seconds to run
and considered it too slow. I copied the SQL statement from the query
and tried executing it from code which then ran in 1 second. To make
sure that I didn't miss anything, I copied the SQL statement back into
a query and tried running it again. It now also only took 1...
|
by: stig |
last post by:
hi.
coming from postgresql, i am used to textual references to most of the
things i do with the database. i feel a little lost with all the graphical.
i have few questions regarding MS SQL 2000
1. what is the best (or easiest) way of getting a table definition in text?
it could be either a CREATE TABLE sql-query or a just a definition,
something like:
TABLE thisTable
id integer
|
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...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |