Subject | slow to execute sub-query when "select first x" is used |
---|---|
Author | Charles McAllister |
Post date | 2008-07-24T04:26:16Z |
i'm using FB version 2.1.1.17910; classic version
i have two simple tables defined, "master" and "detail"
each has a primary key on column "recno", and table "detail" has an
index on a column named "master"
i've loaded the "master" table with about 5000 rows, and the "detail"
table with 100 rows per each master -- so a total of 500k rows
this query executes within milliseconds:
select * from detail where detail.master in
(select recno from master where master.recno > 1 and master.recno <
500 order by recno)
whereas this query executes in over 10 seconds
select * from detail where detail.master in
(select first 10 recno from master where master.recno > 1 and
master.recno < 500 order by recno)
i need the second query to execute much faster.
i know i can accomplish something similar using join, but i really
need this to work for sub-select as i have a lot of source code
already written for sub-selects.
help is greatly appreciated!
here's the ddl to create the tables:
CREATE TABLE MASTER (
RECNO INTEGER NOT NULL);
ALTER TABLE MASTER ADD PRIMARY KEY (RECNO);
CREATE TABLE DETAIL (
RECNO INTEGER NOT NULL,
MASTER INTEGER);
ALTER TABLE DETAIL ADD PRIMARY KEY (RECNO);
CREATE INDEX IDX_DETAIL ON DETAIL(MASTER);
i have two simple tables defined, "master" and "detail"
each has a primary key on column "recno", and table "detail" has an
index on a column named "master"
i've loaded the "master" table with about 5000 rows, and the "detail"
table with 100 rows per each master -- so a total of 500k rows
this query executes within milliseconds:
select * from detail where detail.master in
(select recno from master where master.recno > 1 and master.recno <
500 order by recno)
whereas this query executes in over 10 seconds
select * from detail where detail.master in
(select first 10 recno from master where master.recno > 1 and
master.recno < 500 order by recno)
i need the second query to execute much faster.
i know i can accomplish something similar using join, but i really
need this to work for sub-select as i have a lot of source code
already written for sub-selects.
help is greatly appreciated!
here's the ddl to create the tables:
CREATE TABLE MASTER (
RECNO INTEGER NOT NULL);
ALTER TABLE MASTER ADD PRIMARY KEY (RECNO);
CREATE TABLE DETAIL (
RECNO INTEGER NOT NULL,
MASTER INTEGER);
ALTER TABLE DETAIL ADD PRIMARY KEY (RECNO);
CREATE INDEX IDX_DETAIL ON DETAIL(MASTER);