Subject RE: [ib-support] HELP ! Adding a foreign key SLOWS down query
Author Amrita Chaudhury
Helen,

Thanks for the solution and explanation - I tried the option of using
triggers to achieve the linking between the
two tables, and heres a NEW problem :

The referencing tables, traf_org, traf_ter, etc can be created or dropped at
user request, so
I have to add and drop the triggers on the config table bscmap when I
create/drop the referencing
tables.

The problem is, when I am dropping a table - I first have to drop the
trigger for that table.
After I drop the trigger, when I try to drop the table, I get an error
saying the object <table_name> is
in use !!

Why cant I delete a table right after I delete the trigger referencing the
table ?

TIA,
Amrita .

-----Original Message-----
From: Helen Borrie [mailto:helebor@...]
Sent: Tuesday, August 06, 2002 6:58 PM
To: ib-support@yahoogroups.com
Subject: RE: [ib-support] HELP ! Adding a foreign key SLOWS down query



At 03:35 PM 06-08-02 -0500, you wrote:
>
>The tables are :
>CREATE TABLE "TRAF_ORG"
>(
> "PMDATE" VARCHAR(8) NOT NULL,
> "PMHOUR" INTEGER NOT NULL,
> "BSC" INTEGER NOT NULL,
> "BTS" INTEGER NOT NULL,
> "SECTOR" INTEGER NOT NULL,
> "FA" INTEGER NOT NULL,
> "ATT_VOC" INTEGER default 0,
> "ATT_DAT" INTEGER default 0,
>....
> "AVG_DLY" NUMERIC(15, 2) default 0,
>CONSTRAINT "TRAF_ORG_PRIMARY" PRIMARY KEY ("PMDATE", "PMHOUR", "BSC",
"BTS",
>"SECTOR", "FA")
>);
>
>
>CREATE TABLE "TRAF_TER"
>(
> "PMDATE" VARCHAR(8) NOT NULL,
> "PMHOUR" INTEGER NOT NULL,
> "BSC" INTEGER NOT NULL,
> "BTS" INTEGER NOT NULL,
> "SECTOR" INTEGER NOT NULL,
> "FA" INTEGER NOT NULL,
> "ATT_VOC" INTEGER default 0,
> "ATT_DAT" INTEGER default 0,
>....
> "AVG_DLY" NUMERIC(15, 2) default 0,
>CONSTRAINT "TRAF_TER_PRIMARY" PRIMARY KEY ("PMDATE", "PMHOUR", "BSC",
"BTS",
>"SECTOR", "FA")
>);
>
>
>I was trying to add :
>ALTER TABLE "TRAF_ORG" ADD FOREIGN KEY ("BSC") REFERENCES BSCMAP ("BSCID")
>ON UPDATE CASCADE ON DELETE CASCADE;
>
>where BSCMAP is a config table defined as :
>CREATE TABLE "BSCMAP"
>(
> "BSCID" INTEGER NOT NULL,
> "MSCID" INTEGER NOT NULL,
> "BSMID" INTEGER NOT NULL,
> "BSCNO" INTEGER NOT NULL,
>CONSTRAINT "BSCMAPUNIQUE" UNIQUE ("BSMID", "BSCNO"),
> PRIMARY KEY ("BSCID")
>);
>
>-----Original Message-----
>From: Amrita Chaudhury [mailto:achaudhu@...]
>Sent: Tuesday, August 06, 2002 3:30 PM
>To: 'ib-support@yahoogroups.com'
>Subject: [ib-support] HELP ! Adding a foreign key SLOWS down query
>
>
>HI,
>
>I had a set of stored procedures that were used to join and query certain
>data tables, and
>all was working fine, till I had to add foreign key reference on one of the
>columns on these
>tables to a column in another configuration table.
>
>If I take off the feriegn key reference, everything is ok, as soon as I put
>it back on, the
>query just never makes it through ..
>
>
>PLEASE help .. I do need to reference this to the other table - how can I
>get away with
>this ?

There are at least two potential factors here causing this slow-down:

1. Creating the foreign key on BSC creates a single-column index on that
column which "steps on" your primary key. If you apply an SQL monitor to
your query (one which displays the PLAN on preparing the query, such as the
one in IB_SQL) I think it will show that the optimizer is not using any
indexes for your query.

One way out of this one is to replace your PK with a surrogate (using a
generator). If all of your current PK columns require uniqueness, place a
unique index on them. You are still going to have indexes stepping on one
another but at least it will give the optimizer access to the primary key.

Better still would be to normalise some of the columns out to other tables
to avoid such massive duplication in the top table that requires six
elements in a PK or uniqueness constraint. This seems especially called
for, given that you have two tables both using the same complex PK
structure.

2. If there are very few possible values for BSC, or if most values for
BSC in the actual data are only one or two of the possible values, then the
FK index will have very low selectivity. This is a well-documented source
of query-constipation. Don't use single-column lookup keys as FKs in these
conditions - provide the integrity checking yourself using triggers.

3. You don't mention what the query is but, if there is any ordering or
grouping in it, this would compound the problems the optimizer would have
with these index structures.

heLen


All for Open and Open for All
Firebird Open SQL Database · http://firebirdsql.org <http://firebirdsql.org>
·
http://users.tpg.com.au/helebor/ <http://users.tpg.com.au/helebor/>
______________________________________________________________________



Yahoo! Groups Sponsor

ADVERTISEMENT

<http://rd.yahoo.com/M=228862.2128520.3581629.1829184/D=egroupweb/S=17051153
86:HM/A=1182729/R=0/*http://adfarm.mediaplex.com/ad/ck/990-1736-1039-334>


To unsubscribe from this group, send an email to:
ib-support-unsubscribe@egroups.com



Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .




[Non-text portions of this message have been removed]