Subject | Re: [firebird-support] Strange SELECT FIRST 1 |
---|---|
Author | Ann Harrison |
Post date | 2012-09-12T21:21:57Z |
On Wed, Sep 12, 2012 at 3:25 PM, Mr. John <mr_johnmr@...> wrote:
assume you have a table of pets, and in it you've got descriptions of
hundreds of animals.
select first 1 Name from Pets where Species = 'cat'
would probably get the same name on three sequential tries because Firebird
is retrieving the results in storage order. But if some one deletes an
early 'dog' and someone else inserts a cat, that cat may well take the dog
slot and become the first cat in storage order. So suddenly instead of
'Methos', you get 'Amanda'.
select first 1 Name from Pets where Species = 'cat' order by DateOfBirth
If you've got an index on DateOfBirth, it's very quick.
Good luck,
Ann
[Non-text portions of this message have been removed]
> Hi Ann,thanks for your tips,so SELECT FIRST 1 is slower thanNot in general, but why complicate things?
> simple SELECT ?
>
> Why to add ORDER BY ?That gives you a chance of getting the same record every time. Lets
>
assume you have a table of pets, and in it you've got descriptions of
hundreds of animals.
select first 1 Name from Pets where Species = 'cat'
would probably get the same name on three sequential tries because Firebird
is retrieving the results in storage order. But if some one deletes an
early 'dog' and someone else inserts a cat, that cat may well take the dog
slot and become the first cat in storage order. So suddenly instead of
'Methos', you get 'Amanda'.
select first 1 Name from Pets where Species = 'cat' order by DateOfBirth
If you've got an index on DateOfBirth, it's very quick.
Good luck,
Ann
[Non-text portions of this message have been removed]