Subject Re: Slow select min()
Author danny_vdw
--- In firebird-support@yahoogroups.com, "Kirill Nesterenko" <work@...> wrote:
>
> Hello,
>
> I have a query which runs very slow:
> select min(id) from stat where user_id = 100
>
> There are indices on both fields: PK_STAT PRIMARY KEY (ID);
> and STAT_USER_ID ON STAT (USER_ID);
>
> there are about 3M records in the table, but only 2 records with this
> user_id (I don't think it should take 600ms to find out which id is the
> minimum anyway..);
> also if I run select max(id) - it runs immediately.
>
>
> Thank you,
> Kirill.
>

Like Dimitry said in a previous reply, the query used 2 indices PK_STAT and STAT_USER_ID. If the index STAT_USER_ID is selective you can better eliminate the second index.
select min(id+0) as id from stat where user_id = 100

I use this for order by/group by, mostly don't using an index in these situation are faster then using one.

Danny