473,788 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FOREIGN KEY CHECK ON INSERT

Hi,

I am trying to optimize a table for inserts. Half of the timeron cost is in
the FK lookup!

These tables for example
CREATE TABLE FOO2(
FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK PRIMARY KEY,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

CREATE TABLE FOO(
FOO_ID INTEGER NOT NULL CONSTRAINT FOO_PK PRIMARY KEY,
FOO2_ID INTEGER CONSTRAINT FOO_FK1 REFERENCES FOO2(FOO2_ID) ON DELETE
RESTRICT ON UPDATE RESTRICT,
STATUS CHAR(1) NOT NULL,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

when I create an access plan for the following insert into FOO it shows a
lookup for the FOO2_ID value even though it is NULL!
INSERT INTO FOO VALUES(
1,
NULL,
'A',
'This is Data',
CURRENT TIMESTAMP);

I thought maybee the explain plan showed the typical results for any data
values so I tested inserts with the FK applied and with the FK removed.

I inserted 70% faster with the FK removed.

Can someone explain why it is doing the key scan for a value that cannot be
thier FOO2.FOO2_ID is NOT NULL?

Even if FOO2.FOO2_ID was nullable would it be appropiate to 'link' them?

Can I keep the standard RI checks but not do the FK check when NULL?

I believe I could accomplish this with custom triggers but I would think the
DBM would optimize this shortcut.

thank you for your time,



Nov 12 '05 #1
9 3928
"collincm" <co******@thath otmail.comservi ce> wrote in message
news:vs******** ****@corp.super news.com...
Hi,

I am trying to optimize a table for inserts. Half of the timeron cost is in the FK lookup!

These tables for example
CREATE TABLE FOO2(
FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK PRIMARY KEY,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

CREATE TABLE FOO(
FOO_ID INTEGER NOT NULL CONSTRAINT FOO_PK PRIMARY KEY,
FOO2_ID INTEGER CONSTRAINT FOO_FK1 REFERENCES FOO2(FOO2_ID) ON DELETE
RESTRICT ON UPDATE RESTRICT,
STATUS CHAR(1) NOT NULL,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

when I create an access plan for the following insert into FOO it shows a
lookup for the FOO2_ID value even though it is NULL!
INSERT INTO FOO VALUES(
1,
NULL,
'A',
'This is Data',
CURRENT TIMESTAMP);

I thought maybee the explain plan showed the typical results for any data
values so I tested inserts with the FK applied and with the FK removed.

I inserted 70% faster with the FK removed.

Can someone explain why it is doing the key scan for a value that cannot be thier FOO2.FOO2_ID is NOT NULL?

Even if FOO2.FOO2_ID was nullable would it be appropiate to 'link' them?

Can I keep the standard RI checks but not do the FK check when NULL?

I believe I could accomplish this with custom triggers but I would think the DBM would optimize this shortcut.

thank you for your time,

Just out of curiosity, try explaining this statement:

INSERT into foo
(foo_id, status ,data , lasttime )
values( 1, 'A', 'This is Data', current timestamp)

Nov 12 '05 #2
Nice idea but the explain plan still shows a check on FOO2.

"Mark A" <ma@switchboard .net> wrote in message
news:pW******** *********@news. uswest.net...
"collincm" <co******@thath otmail.comservi ce> wrote in message
news:vs******** ****@corp.super news.com...
Hi,

I am trying to optimize a table for inserts. Half of the timeron cost is
in
the FK lookup!

These tables for example
CREATE TABLE FOO2(
FOO2_ID INTEGER NOT NULL CONSTRAINT FOO2_PK PRIMARY KEY,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

CREATE TABLE FOO(
FOO_ID INTEGER NOT NULL CONSTRAINT FOO_PK PRIMARY KEY,
FOO2_ID INTEGER CONSTRAINT FOO_FK1 REFERENCES FOO2(FOO2_ID) ON DELETE
RESTRICT ON UPDATE RESTRICT,
STATUS CHAR(1) NOT NULL,
DATA VARCHAR(200) NOT NULL,
LASTTIME TIMESTAMP NOT NULL);

when I create an access plan for the following insert into FOO it shows

a lookup for the FOO2_ID value even though it is NULL!
INSERT INTO FOO VALUES(
1,
NULL,
'A',
'This is Data',
CURRENT TIMESTAMP);

I thought maybee the explain plan showed the typical results for any data values so I tested inserts with the FK applied and with the FK removed.

I inserted 70% faster with the FK removed.

Can someone explain why it is doing the key scan for a value that cannot

be
thier FOO2.FOO2_ID is NOT NULL?

Even if FOO2.FOO2_ID was nullable would it be appropiate to 'link' them?

Can I keep the standard RI checks but not do the FK check when NULL?

I believe I could accomplish this with custom triggers but I would think

the
DBM would optimize this shortcut.

thank you for your time,

Just out of curiosity, try explaining this statement:

INSERT into foo
(foo_id, status ,data , lasttime )
values( 1, 'A', 'This is Data', current timestamp)


Nov 12 '05 #3
"collincm" <co******@thath otmail.comservi ce> wrote in message
news:vs******** ****@corp.super news.com...
Nice idea but the explain plan still shows a check on FOO2.

I tested this myself using explain and got similar results.

Just to clarify, are you saying that you measured actual insert performance
and it ran 70% faster without the constraint even when the FK column is
null?
Nov 12 '05 #4
That is true. The tables were populated. about 25 timerons for the actual
insert and another 25 timerons for the fk check.
"Mark A" <ma@switchboard .net> wrote in message
news:AC******** **********@news .uswest.net...
"collincm" <co******@thath otmail.comservi ce> wrote in message
news:vs******** ****@corp.super news.com...
Nice idea but the explain plan still shows a check on FOO2.
I tested this myself using explain and got similar results.

Just to clarify, are you saying that you measured actual insert

performance and it ran 70% faster without the constraint even when the FK column is
null?


Nov 12 '05 #5
"collincm" <co******@thath otmail.comservi ce> wrote in message
news:vs******** ****@corp.super news.com...
That is true. The tables were populated. about 25 timerons for the actual insert and another 25 timerons for the fk check.

I updated the card (in sysibm.systable s) for some tables in the sample
database and got the exact same numbers on the explain. Don't know why it is
doing that.
Nov 12 '05 #6
AK
timeron costs may be a little bit misleading.
I would try to compare real execution costs.
The reason: if the bufferpool is warm, then the index pages are
usually already in it. The optimizer never knows if the bufferpool is
warm or not
Nov 12 '05 #7
The 70% improvment was a real timings of around 1000 records, once with the
key, again without the key. I am purosly testing with a very small (default
1MB) buffer pool. I am sure I can increase it to help keep the index pages
of FOO2 in the pool, but that will make an uneccessary operation faster. I
don't want it to check at all. There is no logical reason why it should do
a key check for a NULL value. It is NULL.

"AK" <ak************ @yahoo.com> wrote in message
news:46******** *************** ***@posting.goo gle.com...
timeron costs may be a little bit misleading.
I would try to compare real execution costs.
The reason: if the bufferpool is warm, then the index pages are
usually already in it. The optimizer never knows if the bufferpool is
warm or not


Nov 12 '05 #8
AK
"collincm" <co******@thath otmail.comservi ce> wrote in message news:<vs******* *****@corp.supe rnews.com>...
The 70% improvment was a real timings of around 1000 records, once with the
key, again without the key. I am purosly testing with a very small (default
1MB) buffer pool. I am sure I can increase it to help keep the index pages
of FOO2 in the pool, but that will make an uneccessary operation faster. I
don't want it to check at all. There is no logical reason why it should do
a key check for a NULL value. It is NULL.


I would insert 1000 records with no NULLs at all,
then next 1000 records with only NULLs, but the FK constraint still
present, and compare real execution costs (db2batch output)
Nov 12 '05 #9
"collincm" <co******@thath otmail.comservi ce> wrote in message news:<vs******* *****@corp.supe rnews.com>...
The 70% improvment was a real timings of around 1000 records, once with the
key, again without the key. I am purosly testing with a very small (default
1MB) buffer pool. I am sure I can increase it to help keep the index pages
of FOO2 in the pool, but that will make an uneccessary operation faster. I
don't want it to check at all. There is no logical reason why it should do
a key check for a NULL value. It is NULL.

"AK" <ak************ @yahoo.com> wrote in message
news:46******** *************** ***@posting.goo gle.com...
timeron costs may be a little bit misleading.
I would try to compare real execution costs.
The reason: if the bufferpool is warm, then the index pages are
usually already in it. The optimizer never knows if the bufferpool is
warm or not


I just tried your example insert out with DB2 v8 for LUW (DDL as
provided, no statistics gathered). My access plan also shows a FK
lookup, but as I expected there are no FOO2 PK index accesses when I
run the insert with NULL (i.e. null = index lookups are ignored at
run-time).

Could you try using the snapshot monitor to see how many bufferpool
logical index reads you get (per insert) when using NULL versus when
using non-null? If you get the same result as me you'll see 1 and 2
logical index reads respectively, which again is what you would
expect.

I haven't tested the time differences. How did you do so, and did you
ensure there were no strange caching effects etc.?
Jeremy Rickard
Nov 12 '05 #10

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

Similar topics

0
4536
by: Ron | last post by:
Mandatories: Ver 7.3.4, Redhat Linux 8.0, P4, 2GB RAM I want to add a 'nullable' foreign key to a column in a table. I have tables "company" and "project" which may be related by company.companyID <-> project.companyID. project.companyID is allowed to be null. However, when someone tries to delete a company which is still referenced in "project" I want a constraint restricting deletion. I tried:
0
2635
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
1
8906
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
26
14147
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based on a column called RefId and one called Type, i.e. type 1 is a relationship to BidItem and type 2 is a relationship to BidAddendum. Is there any way to specify a foreign key that will allow for the different types indicating which table the...
1
3105
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a table that has a foreign key constraint that is linked to a lookup table. E.g. Take the following database structure: SQL-Server Database: Table 1:
3
2632
by: Alex | last post by:
Hi, I am using Arrays to store a list of values. Is it possible to a) index array fields b) to create a foreign key constraint from the array Thanks Alex >
0
1418
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A stored procedure updates a row in C, adds a row each in B & C. I get an integrity violation. All the foreign keys are deferrable, and the stored procedure is called from within a transaction with constraints deferred. (And the foreign keys do refer to...
1
2097
by: Thomas T. Thai | last post by:
I'm looking for a better way to make use of foreign keys. Here is a sample setup: -- TESTING Foreign Keys create table mod ( mod_id int not null primary key, name varchar(32) not null default '' );
2
43962
by: jarea | last post by:
I have read quite a bit about this error but I have yet to find the solution to my problem. I am trying to execute the following mysql statement: alter table line_items add constraint fk_line_item_products foreign key (product_id) references products(id) I have also tried the following statement with the same result:
2
7340
by: ksenthilbabu | last post by:
Hey All, I am using MSSQL -2005 with VB6. I have created a master table tblCompany and detail Table tblDetail having foreign key relationship. When i try to insert a value within a TRANSACTION I am getting Error No. -2147217873 at Line No. 0 (The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblDetail_tblCompany". The conflict occurred in database "DBTest", table "dbo.tblCompany", column RefID.) . please help me to solve...
0
9656
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
10366
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
9967
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
8993
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
7517
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
6750
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.