After searching for a solution on the web for a slow query. I notice that converting the query to a dynamic query would make it execute faster. It seems to do with the wrong execution plan being used when different parameters are passed.
This is slow
SELECT ProductID, ProductName FROM TransactionHistory
WHERE (ProductID = pProductId OR pProductId IS NULL)
AND (ReferenceOrderID = pOrderId OR pOrderId IS NULL)
AND (TransactionType = pTransactionType OR pTransactionType IS NULL)
This is fast (please ignore the bad query and injection)
vSql := 'SELECT ProductID, ProductName FROM TransactionHistory WHERE 1=1 '
IF pProductId IS NOT NULL THEN
vSql := vSql || ' AND ProductID = ' || pProductId
END IF;
IF pOrderId IS NOT NULL THEN
vSql := vSql || ' AND ReferenceOrderID = ' || pOrderId
END IF;
IF pTransactionType IS NOT NULL THEN
vSql := vSql || ' AND TransactionType = ' || pTransactionType
END IF;
OPEN pCursor FOR vSql;
I'm currently working on 10g but will be moving to 11g soon. With 11g, is there a way to have the query run fast without the dynamic query?