473,732 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CHECK constraints and optimizations

Greetings!

Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT
or does Postgresql rely mostly on normal index search?

For example, I want to create some tables to manage different data in a
kind of <object, relationship, object2> manner, but where object2 could be
an IP address, text, a number, etc. So I thought of doing the following:

----------

create table tmp (
luid bigserial,
object_luid bigint,
relationship ltree
);

create table tmp1
(
child_luid bigint,
check (relationship <@ 'Object')
)
inherits (tmp);

create table tmp2 (
ip inet,
check (relationship <@ 'IP')
)
inherits (tmp);

insert into tmp1 (object_luid, relationship, child_luid) values (1, 'Object', 2);
insert into tmp2 (object_luid, relationship, ip) values (1, 'IP.Packet.Sour ce', '10.1.1.2');
insert into tmp2 (object_luid, relationship, ip) values (2, 'IP.Packet.Sour ce', '10.11.0.1');

create view tmp_view as
select luid, object_luid, relationship, child_luid, null as ip
from tmp1
union
select luid, object_luid, relationship, null, ip
from tmp2
;

explain analyze select * from tmp_view where object_luid = 2;
explain analyze select * from tmp_view where relationship <@ 'IP.Packet';

explain analyze select * from (
select luid, object_luid, relationship, child_luid, null as ip
from tmp1
union all
select luid, object_luid, relationship, null, ip
from tmp2
) as foo where relationship <@ 'IP.Packet';
;

-----------------------------------------

When I do the above analyzes, the table for <tmp1> is still scanned, even
though the WHERE clause cannot meet the CHECK clause. Now, this is a
fairly edge case for optimizations, so I just wanted to check that this
indeed will not be utilized. Or will it be only after I have lots of rows
in the table, thereby justifying the check? Is there ever a time where
CONSTRAINTS will be used to optimize a SELECT?

Alternatively, is there another way of accomplishing what I want without
the ugly VIEW (as each new table type I add will necessitate rebuilding
the VIEW with a new column)?

Regards!
Ed

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #1
9 3432
Edmund Dengler <ed*****@eSenti re.com> writes:
Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT


It is not.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2
Edmund Dengler <ed*****@eSenti re.com> writes:
Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT


It is not.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
On Wed, 5 May 2004, Edmund Dengler wrote:
Greetings!

Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT
or does Postgresql rely mostly on normal index search?


I think the only kind of constraint that incidentally improves performance
is a unique constraint, which creates a unique index.

A check constraint is run on a record when it is changed to make sure it
still meets the requirements of the constraint. There is no seperate file
that says "this row meets the constraint". Deferred constraints mean the
check is to be done at the commit time of the transaction.

Note that unique constraints are not necessarily deferrable due to issues
caused by using an immediate acting unique index. I don't think this is
easily fixable either.

So, a check constraint is of no use during a read from the table, and
is a performance penalty when writing to it.
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #4
On Wed, 05 May 2004 21:09:25 -0400, Tom Lane wrote:
Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT


It is not.


I one were to try to add some constraint-based optimizations ("semantic
query optimizations") , what parts of the code would be most relevant to
study?

In particular, I'm interested in the case of join eliminations, based on
foreign key constraints. E.g. having a SUPPLIER(s_id,. ..) and a
SUPPLIER_PART(s _id,p_id) table where SUPPLIER_PART.s _id references
SUPPLIER.s_id. Then, a "SELECT p_id FROM SUPPLIER_PART NATURAL JOIN
SUPPLIER" could skip the join and just look in SUPPLIER_PART.

Another thing:

Oracle and PostgreSQL uses IOs to respond to
SELECT * FROM person WHERE age < 30 AND age > 30.
DB2 and MySQL sees that the result is the empty set, without wasting IOs.
- So here's another place for potential optimizations, although the area
is rather hairy, as soon as one moves beyond the most simple cases.
By the way, in "An Introduction to Database Systems", Date writes about
semantic optimizations:

"... such optimization could provide significant
performance improvements - much greater improvements, very likely,
than are obtained by any of today's more traditional optimization
techniques".

--
Greetings from Troels Arvin, Copenhagen, Denmark

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

Nov 23 '05 #5
On Thu, 6 May 2004, Troels Arvin wrote:
On Wed, 05 May 2004 21:09:25 -0400, Tom Lane wrote:
Just trying some tests out, and wanted to know about some optimizations.
If I do a CHECK constraint on a table, is this used to optimize a SELECT
It is not.


I one were to try to add some constraint-based optimizations ("semantic
query optimizations") , what parts of the code would be most relevant to
study?


I'll leave the answer to such a question to someone who knows the
internals of pgsql a bit better than me.
Oracle and PostgreSQL uses IOs to respond to
SELECT * FROM person WHERE age < 30 AND age > 30.
DB2 and MySQL sees that the result is the empty set, without wasting IOs.
- So here's another place for potential optimizations, although the area
is rather hairy, as soon as one moves beyond the most simple cases.


The postgresql team considers the load on their plates to be great enough
without bothering to optimize highly non-optimal, poorly thought out
queries.

I.e. if you're asking for things like in that where clause, no one's gonna
optimize that. There's already too much to do around here without
focusing on that. Now, if someone gets an itch and wants to try and code
it in their spare time...
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

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

Nov 23 '05 #6
On Thu, May 06, 2004 at 09:29:42AM -0600, scott.marlowe wrote:
A check constraint is run on a record when it is changed to make sure it
still meets the requirements of the constraint. There is no seperate file
that says "this row meets the constraint". Deferred constraints mean the
check is to be done at the commit time of the transaction.

Note that unique constraints are not necessarily deferrable due to issues
caused by using an immediate acting unique index. I don't think this is
easily fixable either.

So, a check constraint is of no use during a read from the table, and
is a performance penalty when writing to it.
I have been thinking though, imagine a table with the constraint:

x < 1000

If I have a query that has WHERE x > 2000, can't that be optimised to
WHERE FALSE? Or WHERE x < 1200 optimised to x < 1000?

Obviously not if the constraint is deferred, but otherwise?

The other person is correct in that (x < 1000 and x > 2000) is not
optimised away by postgresql. Odd, because the capability is there as
very similar tests are use by partial indexes and the index code in
general. If that worked, the system could just add the (simple) CHECK
constraints to the WHERE clause of a query, do the optimisation phrase
and then remove any that remain.

I can't see why this wouldn't work.

Any thoughts?
--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAmqPSY5T wig3Ge+YRAtxeAJ wIFaSmRFPrz7bgj w0feIudUwcjXwCg pGHQ
qN89RvtQE8v3Z3E RU/i5M6M=
=5yOw
-----END PGP SIGNATURE-----

Nov 23 '05 #7
Martijn van Oosterhout <kl*****@svana. org> writes:
I can't see why this wouldn't work.


Doubtless you could do it. The problem with the idea is that those
inference tests are pretty expensive. I think that any such thing would
waste significant numbers of cycles on ordinary queries while only being
a win on a few poorly-written queries.

We do have to make a tradeoff between planning time and execution time,
and I fear that this idea is not going to be a win in those terms.
If you feel like experimenting, though, go for it ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #8
On Thu, May 06, 2004 at 09:02:21PM -0400, Tom Lane wrote:
Martijn van Oosterhout <kl*****@svana. org> writes:
I can't see why this wouldn't work.
Doubtless you could do it. The problem with the idea is that those
inference tests are pretty expensive. I think that any such thing would
waste significant numbers of cycles on ordinary queries while only being
a win on a few poorly-written queries.


Is it really that expensive? From the index code I remember playing
with way back when I was fiddling with the partial index stuff, there
is a table where it takes:

X OPa Val1
X OPb Val2

and it has a lookup table on (OPa,OPb) to provide an OPc that can be
applied to (Val1,Val2) to determine if one implies the other. I was
very impressed actually, quite a neat idea. Quite simple I thought.

I wasn't really considering anything more complicated than this. No
subclauses, only ANDs.
We do have to make a tradeoff between planning time and execution time,
and I fear that this idea is not going to be a win in those terms.
If you feel like experimenting, though, go for it ...
Ofcourse, my ulterior motive is that I want table partitioning based on
values within the tuple. And then have queries avoid scanning tables
that query things that are not in those tables as inferred by parts of
the WHERE clause. Think phone calls with a different subtable for each
year, automatically.

I toyed with creating a script that would generate the RULEs necessary
to implement it in the current system, but splitting a table into four
peices would require around 50+ RULEs (4 subtables x 4 conditions x 3
query types), obviously massively more inefficient that what's being
suggested here.

The solution is to build it right into the storage manager, but I
haven't tried that yet.

--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAmuYNY5T wig3Ge+YRAjwKAJ 9zVK0sWsb7SRzPP j8XZHi0d3aRHwCf ZZpw
VBOjZzUUNsDZmhW znR/BxSw=
=rRCO
-----END PGP SIGNATURE-----

Nov 23 '05 #9
Martijn van Oosterhout <kl*****@svana. org> writes:
On Thu, May 06, 2004 at 09:02:21PM -0400, Tom Lane wrote:
Doubtless you could do it. The problem with the idea is that those
inference tests are pretty expensive.
Is it really that expensive?


I'm afraid it would be. You're correct that the basic test in the
pred_test routine is simple enough, but there are a few issues:

1. You gotta root through all the index opclasses to see if you can find
one involving the operators at hand. (I'm pretty sure this is the most
expensive part of pred_test_simpl e_clause() as it stands.)

2. Using this to detect redundant/contradictory clauses implies
comparing every WHERE clause to every other WHERE clause, hence O(N^2)
effort.

3. You'll be paying the price even on pretty simple queries. I usually
figure that extra planner effort is worthwhile if it only fires on
complex queries, or queries where there's particular reason to think a
win might be had. (Eg, 7.4 goes out of its way when it sees an
IN-subselect clause, but I don't think anyone has a problem with that.)
But with this idea, I don't see any way to avoid expending a lot of
effort on queries where no win will actually result.
BTW, there actually is code in the btree index stuff to detect
contradictory index quals, so "x > 10 AND x < 10" will in fact result in
no I/O if the chosen plan is an indexscan on x. (This path doesn't have
the same problems mentioned above, because by the time control gets
there, we've already determined that the operators are indeed in the
same index opclass and that the same variable is involved.) So that's
another hole in the scope of usefulness of a planning-time test.

I was a tad surprised by the assertion up at the top of this thread
that MySQL has a test for this case. From what I know of their design
philosophy, they'd have even less interest than us in optimizing
badly-written queries at the cost of slowing down the normal path.
Am I right to guess that what they actually have is a short-circuit case
similar to ours for contradictory index quals, and not a blanket check
for contradictory WHERE conditions in general?

regards, tom lane

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

Nov 23 '05 #10

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

Similar topics

2
7184
by: Doug Baroter | last post by:
Hi, DDLs and DMLs: create table #job (jobID int identity(1,1) primary key, jobName varchar(25) unique not null, jobEndDate dateTime, jobComplete bit default(0), check (( is null and = 0) OR ( is not null and = 1))); Q1 with check constraint: sample dagta
3
9070
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called "Ticker." When new company stock tickers (i.e., MSFT for Microsoft) are entered into the field, I'd like them in all caps--whether the user types msft, Msft, MsFt, etc. In Access, this was easy--simply set the Format to ">" in table design view. In...
11
3998
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we can expect when Generics are released? Thanks,
0
356
by: Edmund Dengler | last post by:
Greetings! Just trying some tests out, and wanted to know about some optimizations. If I do a CHECK constraint on a table, is this used to optimize a SELECT or does Postgresql rely mostly on normal index search? For example, I want to create some tables to manage different data in a kind of <object, relationship, object2> manner, but where object2 could be an IP address, text, a number, etc. So I thought of doing the following:
6
19609
by: sarada7 | last post by:
How to check if DB Constraints are enabled in a database?
3
6322
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql, which is called once the user clicks the OK button on this dialog. try { int rows = cmd.ExecuteNonQuery(); } catch(SqlException se)
1
5958
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
1
1608
by: PaulR | last post by:
Hi, (DB2 LUW 8.2) Is the DB2 optimiser able to use check constraints ? e.g table1 ( name varchar(30)
1
3301
by: Dan Holmes | last post by:
How do you connect the check constraints on the Table object with the columns in the table? I ended up doing this and it smells bad. foreach (Column cn in t.Columns) { foreach (Check ch in t.Checks) { if (ch.Text.Contains(cn.Name))
0
8944
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
9445
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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
8186
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
6733
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
4548
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...
1
3259
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
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.