Re: [firebird-support] Plan problem en CTE

2020-01-01 Thread setysvar setys...@gmail.com [firebird-support]
Your query is most likely incorrect. The LIST function returns a 
VARCHAR, so if x returns, say, two rows with 5 and 7, then list will 
create the string '5,7' (or '7,5', your query does not specify any 
order) and IN ('5,7') is very different from IN('5', '7'). Your query 
would only return rows if tbl_2.pk_id actually was '5,7' and that would 
be a very unusual value to have in a pk field.


The optimizer of Firebird has improved for each version, but 2.5 may not 
be clever enough to understand that your particular subselect referring 
to the CTE in the IN part treats the CTE as if it was constant (Firebird 
has to be able to cope with more complex cases where the subselect can 
change for each row and those cases could never use an index). Myself, I 
stopped using IN () with Firebird last century, and have 
preferred using EXISTS ever since. However, in your case I think I would 
have simplified your query rather than used EXISTS, does this return 
what you want?


with x as (
SELECT *
  FROM tbl_x1
 JOIN tbl_fk1 ON cond_1
  WHERE cond_w1
UNION
SELECT *
  FROM tbl_x1
    JOIN tbl_fk1 ON cond_2
  WHERE cond_w2
)

select distinct tbl_2.pk_id
  from tbl_2
  join x on tbl_2.pk_id = x.tbl2_pk_id

HTH,
Set

Den 30.12.2019 17:57, skrev Germán Balbi bal...@yahoo.com 
[firebird-support]:



Hi.
I have this problem in FB 2.5

with x as (
SELECT *
  FROM tbl_x1
     JOIN tbl_fk1 ON cond_1
  WHERE cond_w1
UNION
SELECT *
  FROM tbl_x1
    JOIN tbl_fk1 ON cond_2
  WHERE cond_w2
)

select tbl_2.pk_id
  from tbl_2
  where tbl_2.pk_id in (select list(x.tbl2_pk_id) from x


The plan in the X table is OK, but in tbl_2 is natural

How can I specify the right plan?

Thank





 
	Virus-free. www.avg.com 
 



<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>





RE: [firebird-support] Plan problem en CTE

2019-12-31 Thread liviuslivius liviusliv...@poczta.onet.pl [firebird-support]
HiWhy "list(x.tbl2_pk_id)"?Result of list is a blob string with one single 
value like '1,2,3' not a 3 records '1', '2', '3'.And using "in" is not a good 
choice here. Change query to e.g. 'exists' or better some 'join'Regards,Karol 
Bieniaszewski
null

Re: [firebird-support] Plan problem en CTE

2019-12-31 Thread Mark Rotteveel m...@lawinegevaar.nl [firebird-support]
On 2019-12-30 17:57, Germán Balbi bal...@yahoo.com [firebird-support] 
wrote:
> Hi.
> I have this problem in FB 2.5
> 
> with x as (
> SELECT *
>   FROM tbl_x1
>  JOIN tbl_fk1 ON cond_1
>   WHERE cond_w1
> UNION
> SELECT *
>   FROM tbl_x1
> JOIN tbl_fk1 ON cond_2
>   WHERE cond_w2
> )
> 
> select tbl_2.pk_id
>   from tbl_2
>   where tbl_2.pk_id in (select list(x.tbl2_pk_id) from x
> 
> The plan in the X table is OK, but in tbl_2 is natural
> 
> How can I specify the right plan?

The condition `tbl_2.pk_id in (select list(x.tbl2_pk_id) from x` will be 
false unless there is only one row in x. As far as I'm aware, that 
condition cannot use an index, it can only use natural.

What are you trying to do?

Maybe using `where exists (select * from x where x.tbl2_pk_id = 
tbl2.pk_id)` will have the desired effect.


Re: [firebird-support] Plan problem en CTE

2019-12-30 Thread Dimitry Sibiryakov s...@ibphoenix.com [firebird-support]
30.12.2019 17:57, Germán Balbi bal...@yahoo.com [firebird-support] wrote:
> How can I specify the right plan?

   Your strange query cannot work at all. Natural scan is unavoidable with it.

-- 
   WBR, SD.