I've got a big table (about 120 million rows) and want to get some min and max values.
CREATE TABLE TEST (
ID UUID /* UUID = CHAR(16) */,
RELATIONID UUID /* UUID = CHAR(16) */,
PARENTID UUID /* UUID = CHAR(16) */,
DATUM D_DATE NOT NULL /* D_DATE = DATE */,
SOORTMETING D_SMALLINT NOT NULL /* D_SMALLINT = SMALLINT */)
Indices:
ALTER TABLE TEST ADD CONSTRAINT UNQ1_TEST UNIQUE (DIERID, DATUM, SOORTMETING)
USING DESCENDING INDEX UNQ1_TEST;
ALTER TABLE TEST ADD CONSTRAINT PK_TEST PRIMARY KEY (ID);
CREATE DESCENDING INDEX TEST_IDX1 ON TEST (RELATIONID, DATUM, SOORTMETING);
CREATE DESCENDING INDEX TEST_IDX2 ON TEST (PARENTID, SOORTMETING, DATUM);
CREATE UNIQUE INDEX TEST_IDX3 ON TEST (DIERID, DATUM, SOORTMETING);
CREATE INDEX TEST_IDX4 ON TEST (RELATIONID, DATUM, SOORTMETING);
SQL 1: fast and correct:
select max(dm.DATUM)
from TEST dm
where dm.RELATIONID = char_to_uuid('9c295585-a307-49be-9014-43545ef79c2a')
and dm.SOORTMETING = 2
Uses PLAN (DM ORDER TEST_IDX1) good speed and has a few reads.
SQL 2: change max to min:
select min(dm.DATUM)
from TEST dm
where dm.RELATIONID = char_to_uuid('9c295585-a307-49be-9014-43545ef79c2a')
and dm.SOORTMETING = 2
Uses correctly plan PLAN (DM ORDER TEST_IDX4). But needs 540.489 fetches
SQL 3: (copy of SQL 1, but with SOORTMETING=3)
select max(dm.DATUM)
from TEST dm
where dm.RELATIONID = char_to_uuid('9c295585-a307-49be-9014-43545ef79c2a')
and dm.SOORTMETING = 3
Uses correctly plan PLAN (DM ORDER TEST_IDX1), but has 2.391.684 fetches
Regardless of empty resultset.
Above occurs in both FB4.0.4.3010 and FB5.0.0.1306.
Question is, it this expected behaviour from your point of view? I can send database. Compressed fbk is 4.3GB.
Please note:
When under FB 5 I rewrite indexes to several conditional/partial index (on soortmeting=2 and 3), all is good! Good speed and expected number of fetches.