Subject | Re: Why "SELECT FIRST 1 ..." and "EXISTS ( SELECT * FROM ... )" are so slow in a big database? |
---|---|
Author | Ali Gökçen |
Post date | 2006-07-15T10:45:31Z |
Hi Antti,
i have no big db file at home, i couldn't test very well.
but,
this command selects different PLAN and does less fetch for me.
DELETE FROM PV_TYPE
WHERE
PV = 2 AND
( SELECT min(PV) FROM DV_PV WHERE PV = 2 ) IS NULL
Subselect will use PLAN ( DV_PV ORDER MY_PV_INDEX).
Otherwise it will use PLAN(DV_PV INDEX MY_PV_INDEX).
and work with different fetchs for me.
i have no idea about detailed internals and traversing strategy,
i never analysed FB sources.
Regards.
Ali
--- In firebird-support@yahoogroups.com, "Antti Nivala"
<antti.nivala@...> wrote:
i have no big db file at home, i couldn't test very well.
but,
this command selects different PLAN and does less fetch for me.
DELETE FROM PV_TYPE
WHERE
PV = 2 AND
( SELECT min(PV) FROM DV_PV WHERE PV = 2 ) IS NULL
Subselect will use PLAN ( DV_PV ORDER MY_PV_INDEX).
Otherwise it will use PLAN(DV_PV INDEX MY_PV_INDEX).
and work with different fetchs for me.
i have no idea about detailed internals and traversing strategy,
i never analysed FB sources.
Regards.
Ali
--- In firebird-support@yahoogroups.com, "Antti Nivala"
<antti.nivala@...> wrote:
>the
> Dmitry Yemanov wrote:
> > Records are fetched from a table based on the bitmap filled by
> > index scan. I.e. no record can be fetched until the index scanthere a
> > completes for all existing matches. For a non-selective index,
> > the scan may take quite a long time.
>
> Thank you. This fully explains the problem I am experiencing. Is
> workaround, i.e. is there a way to make a pure index-based existencecurrently do
> check based on the "PV = 2" condition when there are lots of (e.g.
> 1.000.000) records for which this is true? The table has 26.000.000
> records so a natural scan would be much worse in many cases.
>
> In practice, I am looking for a way to delete a record from table
> PV_TYPE if that record is not referenced by any record in the DV_PV
> table. For example, when deleting a record from PV_TYPE, I
> DELETE FROM PV_TYPEbut 400
> WHERE
> PV = <x> AND
> NOT EXISTS ( SELECT 1 FROM DV_PV WHERE PV = <x> )
>
> This takes 0 ms if there are not many matching records in DV_PV,
> ms if there are 1.000.000 matching records in DV_PV. My database hasthat the
> just been backed up and restored. The point is, as Dmitry said,
> EXISTS check reads lots of data from the index in that case. Isthere a
> way to avoid that, i.e. utilize the index for a pure existencecheck? I
> would want FB to stop reading the index as soon as it sees that atleast
> one record matches.but I
>
> The workaround of having a manual count field is one possibility,
> am still looking for a more efficient and elegant solution. Is there
> one?
>
> Antti
>