Subject Re: [firebird-support] Re: SQL Performance problem
Author Alexandre Benson Smith
Adam,

Here is a sample:

Create Table Employee (
ID Integer,
Name varchar(40),
Address varchar(40));

commit;

create generator g_Employee;

set term ^;

create trigger tg_Employee for Employee before insert as begin
New.ID = Gen_Id(g_Employee, 1);
New.Name = 'a' || '_' || cast(New.ID as varchar(6));
end^

set term ;^

commit;


insert into Employee values (0, 'a', 'b');

insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;
insert into Employee select * from Employee where Id < 1000;

commit;

create index X_Employee on Employee(ID);

create view v_Employee (ID, Name) as
select
ID, Name
from
Employee;

commit;

select count(*) from Employee
19981 records

select * from Employee where Id = 5
the used plan was:
PLAN (EMPLOYEE INDEX (X_EMPLOYEE))

select * from Employee where Id = 5000
the used plan was:
PLAN (EMPLOYEE INDEX (X_EMPLOYEE))

select * from v_employee where id = 5
the used plan was:
PLAN (V_EMPLOYEE EMPLOYEE INDEX (X_EMPLOYEE))

select * from v_employee where id = 5000
the used plan was:
PLAN (V_EMPLOYEE EMPLOYEE INDEX (X_EMPLOYEE))

It uses the index ! :-)

in your case:
select * from Employee where ID < 5000

used the same plan as above both for using the table or the view...

Maybe your indices statistcs are out dated ? Then the optimizer just
think that will be cheaper to do not use it ?

see you !

--

Alexandre Benson Smith
Development
THOR Software e Comercial Ltda.
Santo Andre - Sao Paulo - Brazil
www.thorsoftware.com.br