Subject | Faster select - exists |
---|---|
Author | Tiberiu Horvath |
Post date | 2008-08-14T09:59:51Z |
Firebird 2.1
this query is executed in 30 seconds
select distinct
id_cont_debit as id_repartitor
from articol_doc
inner join document on document.id_document+0=articol_doc.id_document
inner join cont on cont.id_cont=articol_doc.id_cont_debit
where
(cont.repartitor is not null) and
(cont.repartitor = 1) and
(document.id_agent_economic = 200)
this one, that does the same, and is executed in 0.9 seconds
select distinct
id_cont as id_repartitor
from cont
where
(cont.repartitor is not null) and
(cont.repartitor = 1) and
exists(
select articol_doc.id_cont_debit
from articol_doc
where
articol_doc.id_cont_debit = cont.id_cont and
exists(
select id_document
from document
where
(document.id_agent_economic = 200) and
(document.id_document = articol_doc.id_document)
)
)
If this is true, why ever using inner joins ?
This way any inner join can be replaced. Am I right ?
Tiberiu
[Non-text portions of this message have been removed]
this query is executed in 30 seconds
select distinct
id_cont_debit as id_repartitor
from articol_doc
inner join document on document.id_document+0=articol_doc.id_document
inner join cont on cont.id_cont=articol_doc.id_cont_debit
where
(cont.repartitor is not null) and
(cont.repartitor = 1) and
(document.id_agent_economic = 200)
this one, that does the same, and is executed in 0.9 seconds
select distinct
id_cont as id_repartitor
from cont
where
(cont.repartitor is not null) and
(cont.repartitor = 1) and
exists(
select articol_doc.id_cont_debit
from articol_doc
where
articol_doc.id_cont_debit = cont.id_cont and
exists(
select id_document
from document
where
(document.id_agent_economic = 200) and
(document.id_document = articol_doc.id_document)
)
)
If this is true, why ever using inner joins ?
This way any inner join can be replaced. Am I right ?
Tiberiu
[Non-text portions of this message have been removed]