Subject | Re: Opimize query with casted field |
---|---|
Author | Adam |
Post date | 2007-06-19T04:45:19Z |
--- In firebird-support@yahoogroups.com, Mitch Peek <mpeek@...> wrote:
operator being inclusive).
You could certainly do
where DyStart >= :StartDate and DyStart < (:StartDate + 1)
But
where DyStart between :StartDate and (:StartDate + 1)
will give the wrong result because it includes midnight the morning after.
Helen's first response is the correct way to do it, but it only works
in Firebird 2 or higher (new feature). The other suggestion is a
'hack' to achieve roughly the same thing in Firebird 1.5 or earlier
which don't have a clue about expression indices.
Adam
>notes.
> Helen Borrie wrote:
> > In Firebird 2.0.x you can have an expression index - see release
> >just
> > In Fb 1.5.x and below you'd need to add a trigger-populated proxy
> > column for DyStart and index that, e.g.
> >
> > Alter DY add DyStartdate DATE;
> >
> > create index ascDyStartdate on DY (DyStartdate);
> >
> > create trigger biu_DY for DY
> > active before insert or update
> > as
> > declare vtimestamp timestamp;
> > begin
> > if (new.DyStart is not null) then
> > new.DyStartdate = cast (new.DyStart as Date);
> > end
> > else
> > if (updating and old.DyStart is not null) then
> > new.DyStartdate = null;
> > end
> >
> >
> Helen,
> Would this approach be better then an index on the timestamp and
> querying between Adate and Adate+1? if so, why?For the exact reason you point out (confusion with the 'between'
> The only problem I see might be the inclusion of the record which could
> occur if an entry was exactly at midnight (00:00:00.0000)
operator being inclusive).
You could certainly do
where DyStart >= :StartDate and DyStart < (:StartDate + 1)
But
where DyStart between :StartDate and (:StartDate + 1)
will give the wrong result because it includes midnight the morning after.
Helen's first response is the correct way to do it, but it only works
in Firebird 2 or higher (new feature). The other suggestion is a
'hack' to achieve roughly the same thing in Firebird 1.5 or earlier
which don't have a clue about expression indices.
Adam