Subject | Re: [firebird-support] using indices |
---|---|
Author | Sergio H. Gonzalez |
Post date | 2008-09-19T23:45:31Z |
From: "Alexandre Benson Smith"
CREATE TABLE FAC_COM (
ID ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_EMPRESA ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_FAC_TIPO ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_FAC_COM_RUBRO ID /* ID = NUMERIC(9,0) NOT NULL */,
LETRA CHAR(2),
SUCURSAL SMALLINT,
NUMERO ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_PROVEEDOR ID /* ID = NUMERIC(9,0) NOT NULL */,
IMPORTE MONEY /* MONEY = NUMERIC(18,3) DEFAULT 0 */,
SALDO MONEY /* MONEY = NUMERIC(18,3) DEFAULT 0 */,
ANULADA DATE,
ID_CONDICION_VTA ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_OPERADOR ID /* ID = NUMERIC(9,0) NOT NULL */,
EMISION_FECHA DATE DEFAULT 'TODAY',
EMISION_HORA TIME DEFAULT 'NOW',
FECHA_CONTABLE DATE,
FECHA DATE,
ID_PROVINCIAS ID /* ID = NUMERIC(9,0) NOT NULL */
);
/* Check constraints definition */
ALTER TABLE FAC_COM ADD CONSTRAINT SALDO_MAYOR_IMPORTE CHECK(SALDO <= IMPORTE);
/*Primary Keys*/
ALTER TABLE FAC_COM ADD CONSTRAINT PK_FAC_COM PRIMARY KEY (ID);
/*Foreign Keys*/
ALTER TABLE FAC_COM ADD CONSTRAINT FK_FAC_COM_1 FOREIGN KEY (ID_EMPRESA)
REFERENCES EMPRESAS (ID);
ALTER TABLE FAC_COM ADD CONSTRAINT FK_FAC_COM_2 FOREIGN KEY (ID_CONDICION_VTA)
REFERENCES CONDICION_VENTA (ID);
/*Indices*/
CREATE INDEX FAC_COM_IDX1 ON FAC_COM (FECHA);
CREATE INDEX FAC_COM_IDX2 ON FAC_COM (FECHA_CONTABLE);
CREATE INDEX FAC_COM_IDX3 ON FAC_COM (ID_PROVEEDOR);
CREATE INDEX FAC_COM_IDX4 ON FAC_COM COMPUTED BY (ABS(SALDO));
---
When I do:
select sum(saldo) from fac_com where (ABS(SALDO)>0) and (id_proveedor =
:id_proveedor)
I get:
Plan
PLAN (FAC_COM INDEX (FAC_COM_IDX3))
Adapted Plan
PLAN (FAC_COM INDEX (FAC_COM_IDX3))
Shouldn't FB use FAC_COM_IDX4 also? (I'm using FB 2.1.1)
Thanks!
> So, multiple indices could and will be used by FB when the optimizerHi Alenxandre!, This is the actual table
> find it useful, this feature makes virtually not necessary the need of
> composite index (of course there is exceptions to the rule).
CREATE TABLE FAC_COM (
ID ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_EMPRESA ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_FAC_TIPO ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_FAC_COM_RUBRO ID /* ID = NUMERIC(9,0) NOT NULL */,
LETRA CHAR(2),
SUCURSAL SMALLINT,
NUMERO ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_PROVEEDOR ID /* ID = NUMERIC(9,0) NOT NULL */,
IMPORTE MONEY /* MONEY = NUMERIC(18,3) DEFAULT 0 */,
SALDO MONEY /* MONEY = NUMERIC(18,3) DEFAULT 0 */,
ANULADA DATE,
ID_CONDICION_VTA ID /* ID = NUMERIC(9,0) NOT NULL */,
ID_OPERADOR ID /* ID = NUMERIC(9,0) NOT NULL */,
EMISION_FECHA DATE DEFAULT 'TODAY',
EMISION_HORA TIME DEFAULT 'NOW',
FECHA_CONTABLE DATE,
FECHA DATE,
ID_PROVINCIAS ID /* ID = NUMERIC(9,0) NOT NULL */
);
/* Check constraints definition */
ALTER TABLE FAC_COM ADD CONSTRAINT SALDO_MAYOR_IMPORTE CHECK(SALDO <= IMPORTE);
/*Primary Keys*/
ALTER TABLE FAC_COM ADD CONSTRAINT PK_FAC_COM PRIMARY KEY (ID);
/*Foreign Keys*/
ALTER TABLE FAC_COM ADD CONSTRAINT FK_FAC_COM_1 FOREIGN KEY (ID_EMPRESA)
REFERENCES EMPRESAS (ID);
ALTER TABLE FAC_COM ADD CONSTRAINT FK_FAC_COM_2 FOREIGN KEY (ID_CONDICION_VTA)
REFERENCES CONDICION_VENTA (ID);
/*Indices*/
CREATE INDEX FAC_COM_IDX1 ON FAC_COM (FECHA);
CREATE INDEX FAC_COM_IDX2 ON FAC_COM (FECHA_CONTABLE);
CREATE INDEX FAC_COM_IDX3 ON FAC_COM (ID_PROVEEDOR);
CREATE INDEX FAC_COM_IDX4 ON FAC_COM COMPUTED BY (ABS(SALDO));
---
When I do:
select sum(saldo) from fac_com where (ABS(SALDO)>0) and (id_proveedor =
:id_proveedor)
I get:
Plan
PLAN (FAC_COM INDEX (FAC_COM_IDX3))
Adapted Plan
PLAN (FAC_COM INDEX (FAC_COM_IDX3))
Shouldn't FB use FAC_COM_IDX4 also? (I'm using FB 2.1.1)
Thanks!