Search code examples
firebirdfastreportfirebird-3.0

Why can't Firebird 3 perform where in?


When the code below is executed, it keeps thinking forever and ends up crashing fastreport, why?

SELECT TREQDEVMAT.COD_PRODUTO,
       TPRODUTO.DES_PRODUTO,
       TPRODUTO.COD_SUBFAMILIA,
       TPRODUTOIDIOMA.DES_PRODUTO AS DES_PRODUTO_IDIOMA,
       TREQDEVMAT.NUM_CHAVEOP,
       (TREQDEVMAT.QTD_REQUISITADA / TORDEMPROD.QTD_PRODUZIR) AS QTD_POREQUIP,
       TPRODUTO.FLG_ASSISTTEC,
       TPRODUTO.VAL_ULTCUSTOMOV,
       TPRODUTO.DAT_ULTCOMPRA
FROM TREQDEVMAT
LEFT JOIN TPRODUTO ON TPRODUTO.COD_PRODUTO = TREQDEVMAT.COD_PRODUTO
LEFT JOIN TORDEMPROD ON TORDEMPROD.NUM_CHAVE = TREQDEVMAT.NUM_CHAVEOP
LEFT JOIN TPRODUTOIDIOMA ON TPRODUTOIDIOMA.COD_PRODUTO = TREQDEVMAT.COD_PRODUTO
WHERE TREQDEVMAT.NUM_CHAVEOP IN (
    SELECT tordemprod.NUM_CHAVE
    FROM tmrp
    LEFT JOIN tordemprod ON tordemprod.NUM_CHMRP = tmrp.NUM_CHAVE
    WHERE TRIM(NUM_MRP) = '1354'
)
AND TPRODUTO.FLG_ASSISTTEC = 1
AND TPRODUTOIDIOMA.COD_IDIOMA = 1
AND TREQDEVMAT.COD_EMPRESA = 1
AND (TPRODUTO.COD_SUBFAMILIA NOT LIKE 'CE-UM' OR TPRODUTO.COD_SUBFAMILIA = 'CM-FP')
ORDER BY TREQDEVMAT.COD_PRODUTO ASC

Solution

  • Your subquery includes condition that cannot use indexes. It makes nested loop used for this query extremely ineffective. As the result time of execution is very big.

    Your query also contain logic error: condition in subquery duplicates condition in join which makes situation even worse.

    Crash of Fast Report is not related to all this and most likely is result of big number of records returned by the query.