Subject Slow query using stored procedure
Author Mr. John
Hi !

I have one selectable stored procedure (PR_GET_SOLD) that works and this query :

SELECT DISTINCT  X.NUMB  FROM  PR_GET_SOLD(2013,1)  X ) 


is executed in 0.219s with result of  77 rows :


 FlameRobin :
 Executing statement...

Statement executed (elapsed time: 0.000s).
35972 fetches, 0 marks, 599 reads, 2 writes.
0 inserts, 0 updates, 0 deletes, 0 index, 16841 seq.
Delta memory: 53332 bytes.
Total execution time: 0.219s
Script execution finished.
   
also this query :

SELECT a.date,a.id  FROM  mytable1 a   WHERE a.year=2013 
     
is executed in  0.235s with result of  19593 rows :


   Executing statement...
Statement executed (elapsed time: 0.000s).
35972 fetches, 0 marks, 599 reads, 2 writes.
0 inserts, 0 updates, 0 deletes, 0 index, 16841 seq.
Delta memory: 53332 bytes.
Total execution time: 0.235s
Script execution finished.

Put putting together :

 SELECT a.date,a.id  FROM  mytable1 a  WHERE a.year=2013 
     AND (  A.NUMB
      IN (SELECT DISTINCT  X.NUMB  FROM  PR_GET_SOLD(2013,1)  X ) ) 
   

it takes  >2 minutes and returns 1693 rows  :

Executing statement...

Statement executed (elapsed time: 0.000s).
61441654 fetches, 55808 marks, 3379 reads, 475 writes.
17900 inserts, 0 updates, 0 deletes, 30304700 index, 99576 seq.
Delta memory: 1943904 bytes.
GN_DEBUG: 17900 inserts. 
Total execution time: 0:02:15 (hh:mm:ss)
Script execution finished.


There is something wrong, PR_GET_SOLD is executed to many times.

  I've created one table for debugging  and put one insert  into this table called from this procedure and after the script above is executed I found 17900 inserts (also seen in the log above)

so why procedure is executed for so may times,procedure have to get data only one time,calling parameters are constants

What I'm doing wrong ? 
 Thanks for any help!

[Non-text portions of this message have been removed]