Discussion:
[firebird-support] Exists function
thp_pkmi@yahoo.com [firebird-support]
2016-12-08 04:02:11 UTC
Permalink
Dear all,


I compare two SQL select statements via flamerobin:


tables definition:



create table Master

( Gen integer constraint PK_Master primary key,
F1 varchar(20),
F2 varchar(50),
F3 varchar(50),
Entry timestamp,
User varchar(10) );


create table Detail1
( Gen1 integer constraint PK_Detail1 primary key,
Gen integer constraint FK_Detail1 references Master(Gen) on update cascade,
TrDate date,
Qty integer,
Entry timestamp,

User varchar(10) );


create table Detail2
( Gen2 integer constraint PK_Detail2 primary key,
Gen integer constraint FK_Detail2 references Master(Gen) on update cascade,
TrDate date,
Qty integer,
Amount bigint,
Entry timestamp,

User varchar(10) );


FIRST STATEMENT:
select Master.*, iif(exists(select 1 from Detail1 where Gen=Master.Gen rows 1) or exists(select 1 from Detail2 where Gen=Master.Gen rows 1), 1, 0) Ref from Master;


SECOND STATEMENT:
select Master.*, iif(exists(select 1 from Detail1 where Gen=Master.Gen) or exists(select 1 from Detail2 where Gen=Master.Gen), 1, 0) Ref from Master;


I expect the first select with "rows 1" will be faster and consumes less fetches than the second, but surprisingly flamerobin reports no difference in statistics of both. I observe statistics after two times execution of each statement.


I guess firebird always optimizes the EXISTS function, am I right ?
I appreciate every comment on this, thank you.



Best Regards,
Tjioe Hian Pin
Köditz, Martin Martin.Koeditz@it-syn.de [firebird-support]
2016-12-09 14:13:35 UTC
Permalink
Hi,

I think the answer is simple. Since EXISTS just checks for existence the results are equal. So after EXISTS fetches (finds) one row the result is true and no further records will be checked. So there is internally no difference in your queries.

Please correct me if I'm wrong.

Regards,
Martin
thp_pkmi thp_pkmi@yahoo.com [firebird-support]
2016-12-12 14:19:01 UTC
Permalink
Hi Martin,
Thank you for yor concern. I don't explore the source code of firebird, but I guess your opinion is  correct.
Regards,Tjioe Hian Pin


On Friday, 9 December 2016, 21:13, "Köditz, Martin ***@it-syn.de [firebird-support]" <firebird-***@yahoogroups.com> wrote:


  Hi,   I think the answer is simple. Since EXISTS just checks for existence the results are equal. So after EXISTS fetches (finds) one row the result is true and no further records will be checked. So there is internally no difference in your queries.   Please correct me if I’m wrong.   Regards, Martin #yiv8488335541 #yiv8488335541 -- #yiv8488335541ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv8488335541 #yiv8488335541ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv8488335541 #yiv8488335541ygrp-mkp #yiv8488335541hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv8488335541 #yiv8488335541ygrp-mkp #yiv8488335541ads {margin-bottom:10px;}#yiv8488335541 #yiv8488335541ygrp-mkp .yiv8488335541ad {padding:0 0;}#yiv8488335541 #yiv8488335541ygrp-mkp .yiv8488335541ad p {margin:0;}#yiv8488335541 #yiv8488335541ygrp-mkp .yiv8488335541ad a {color:#0000ff;text-decoration:none;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ygrp-lc {font-family:Arial;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ygrp-lc #yiv8488335541hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ygrp-lc .yiv8488335541ad {margin-bottom:10px;padding:0 0;}#yiv8488335541 #yiv8488335541actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv8488335541 #yiv8488335541activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv8488335541 #yiv8488335541activity span {font-weight:700;}#yiv8488335541 #yiv8488335541activity span:first-child {text-transform:uppercase;}#yiv8488335541 #yiv8488335541activity span a {color:#5085b6;text-decoration:none;}#yiv8488335541 #yiv8488335541activity span span {color:#ff7900;}#yiv8488335541 #yiv8488335541activity span .yiv8488335541underline {text-decoration:underline;}#yiv8488335541 .yiv8488335541attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv8488335541 .yiv8488335541attach div a {text-decoration:none;}#yiv8488335541 .yiv8488335541attach img {border:none;padding-right:5px;}#yiv8488335541 .yiv8488335541attach label {display:block;margin-bottom:5px;}#yiv8488335541 .yiv8488335541attach label a {text-decoration:none;}#yiv8488335541 blockquote {margin:0 0 0 4px;}#yiv8488335541 .yiv8488335541bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv8488335541 .yiv8488335541bold a {text-decoration:none;}#yiv8488335541 dd.yiv8488335541last p a {font-family:Verdana;font-weight:700;}#yiv8488335541 dd.yiv8488335541last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv8488335541 dd.yiv8488335541last p span.yiv8488335541yshortcuts {margin-right:0;}#yiv8488335541 div.yiv8488335541attach-table div div a {text-decoration:none;}#yiv8488335541 div.yiv8488335541attach-table {width:400px;}#yiv8488335541 div.yiv8488335541file-title a, #yiv8488335541 div.yiv8488335541file-title a:active, #yiv8488335541 div.yiv8488335541file-title a:hover, #yiv8488335541 div.yiv8488335541file-title a:visited {text-decoration:none;}#yiv8488335541 div.yiv8488335541photo-title a, #yiv8488335541 div.yiv8488335541photo-title a:active, #yiv8488335541 div.yiv8488335541photo-title a:hover, #yiv8488335541 div.yiv8488335541photo-title a:visited {text-decoration:none;}#yiv8488335541 div#yiv8488335541ygrp-mlmsg #yiv8488335541ygrp-msg p a span.yiv8488335541yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv8488335541 .yiv8488335541green {color:#628c2a;}#yiv8488335541 .yiv8488335541MsoNormal {margin:0 0 0 0;}#yiv8488335541 o {font-size:0;}#yiv8488335541 #yiv8488335541photos div {float:left;width:72px;}#yiv8488335541 #yiv8488335541photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv8488335541 #yiv8488335541photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv8488335541 #yiv8488335541reco-category {font-size:77%;}#yiv8488335541 #yiv8488335541reco-desc {font-size:77%;}#yiv8488335541 .yiv8488335541replbq {margin:4px;}#yiv8488335541 #yiv8488335541ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv8488335541 #yiv8488335541ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv8488335541 #yiv8488335541ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv8488335541 #yiv8488335541ygrp-mlmsg select, #yiv8488335541 input, #yiv8488335541 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv8488335541 #yiv8488335541ygrp-mlmsg pre, #yiv8488335541 code {font:115% monospace;}#yiv8488335541 #yiv8488335541ygrp-mlmsg * {line-height:1.22em;}#yiv8488335541 #yiv8488335541ygrp-mlmsg #yiv8488335541logo {padding-bottom:10px;}#yiv8488335541 #yiv8488335541ygrp-msg p a {font-family:Verdana;}#yiv8488335541 #yiv8488335541ygrp-msg p#yiv8488335541attach-count span {color:#1E66AE;font-weight:700;}#yiv8488335541 #yiv8488335541ygrp-reco #yiv8488335541reco-head {color:#ff7900;font-weight:700;}#yiv8488335541 #yiv8488335541ygrp-reco {margin-bottom:20px;padding:0px;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ov li a {font-size:130%;text-decoration:none;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv8488335541 #yiv8488335541ygrp-sponsor #yiv8488335541ov ul {margin:0;padding:0 0 0 8px;}#yiv8488335541 #yiv8488335541ygrp-text {font-family:Georgia;}#yiv8488335541 #yiv8488335541ygrp-text p {margin:0 0 1em 0;}#yiv8488335541 #yiv8488335541ygrp-text tt {font-size:120%;}#yiv8488335541 #yiv8488335541ygrp-vital ul li:last-child {border-right:none !important;}#yiv8488335541
Loading...