要获得报告,我必须编写一个递归存储过程GET_RECIPE_STEPS_ID(recipe_id)
。它返回配方类型的步骤的ID。 I.E。
SELECT GET_RECIPE_STEPS_ID.ID FROM GET_RECIPE_STEPS_ID(3189)
It Returns
3189
3190
3191
3192
当我自己运行它时,它很快(例如0.031秒的执行时间)。但是,如果将其与查询中的IN子句一起使用,则会花费一些时间。像下面的查询一样,花费了将近12分钟。
SELECT rs.RECIPEID
FROM RECIPESTEPS rs
WHERE rs.RECIPEID IN (select GET_RECIPE_STEPS_ID.ID from GET_RECIPE_STEPS_ID(3189))
与以下查询等效,并且几乎与存储过程本身一样快(0.038sec)
Select rs.RECIPEID
FROM RECIPESTEPS rs
WHERE rs.RECIPEID IN (3189, 3190, 3191, 3192)
存储过程
CREATE OR ALTER PROCEDURE GET_RECIPE_STEPS_ID
(recipe_id integer)
RETURNS
(id integer)
AS
declare variable coType integer;
BEGIN
/* Recursive Procedure
* For Passed Recipe 'Recipe_id', it Returns the step's which are of type Recipe again.
*
* If any step is of type Recipe(i.e COTYPE = 1)
* Then it calls itself again for that step(Recipe)
*/
id =: recipe_id;
SUSPEND;
FOR SELECT rs.COMMODITYID, c.COTYPE
FROM RECIPESTEPS rs
LEFT JOIN COMMODITIES c ON c.COMMODITYID = rs.COMMODITYID
WHERE rs.RECIPEID =: recipe_id INTO :id, :coType
Do
BEGIN
IF(coType = 1)
THEN
FOR SELECT r.RECIPEID FROM RECIPES r WHERE r.LATEST = 1 AND r.COMMODITYID =:id into :id
DO
BEGIN
FOR SELECT GET_RECIPE_STEPS_ID.ID
FROM GET_RECIPE_STEPS_ID(:id) INTO :id
DO
BEGIN
SUSPEND;
END
END
END
END^
答案 0 :(得分:2)
问题有两个:
IN
开头和如果将查询转换为使用INNER JOIN
而不是IN
,它可能会更好:
select rs.RECIPEID
from GET_RECIPE_STEPS_ID(3189) grs
inner join RECIPESTEPS rs
on rs.RECIPEID = grs.ID
我认为您的真实查询可能会更复杂,因为否则select ID from GET_RECIPE_STEPS_ID(3189)
就足够了。
以上查询的行为与IN
略有不同,例如,如果ID
在存储过程输出中多次出现,则现在也会产生多行。您可能需要进行相应的调整。