Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate plan?? with sort [CORE2085] #2519

Open
firebird-automations opened this issue Sep 17, 2008 · 3 comments
Open

Duplicate plan?? with sort [CORE2085] #2519

firebird-automations opened this issue Sep 17, 2008 · 3 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: @livius2

I do not known if this is some issue or as designed
but
if i have query like this

SELECT
A.OBIEKT ,
(SELECT COUNT(*) FROM ELEMENTY_EX EE1 INNER JOIN ELEMENTY_DB E ON E.ID=EE1.ID_ELEM WHERE EE1.ID_OBJ=http://A.ID AND EE1.DATAP1=EE1.DATAP2 AND EE1.CZASP1=EE1.CZASP2 AND E.RODZAJ='Status') AS ILE_STAN1
FROM ADRESY_DB A

without order by i get single plan for subquery (EE1):

PLAN JOIN (EE1 INDEX (IXA__ELEMENTY_EX__ID_OBJ),E INDEX (PK_ELEMENTY_DB__ID))
PLAN (A NATURAL)

but with order by i get 2 plans for subquery (EE1):

SELECT
A.OBIEKT ,
(SELECT COUNT(*) FROM ELEMENTY_EX EE1 INNER JOIN ELEMENTY_DB E ON E.ID=EE1.ID_ELEM WHERE EE1.ID_OBJ=http://A.ID AND EE1.DATAP1=EE1.DATAP2 AND EE1.CZASP1=EE1.CZASP2 AND E.RODZAJ='Status') AS ILE_STAN1
FROM ADRESY_DB A ORDER BY 2 DESC

PLAN JOIN (EE1 INDEX (IXA__ELEMENTY_EX__ID_OBJ),E INDEX (PK_ELEMENTY_DB__ID))
PLAN JOIN (EE1 INDEX (IXA__ELEMENTY_EX__ID_OBJ),E INDEX (PK_ELEMENTY_DB__ID))
PLAN SORT ((A NATURAL))

@firebird-automations
Copy link
Collaborator Author

Commented by: Sean Leyne (seanleyne)

Where these values reported by ISQL or another database tool?

@firebird-automations
Copy link
Collaborator Author

Commented by: @dyemanov

This is a known issue, caused by the lack of common sub-expression elimination in Firebird. The sub-query will be executed twice (in the ORDER BY clause and in the select list), hence two plans reported.

@firebird-automations
Copy link
Collaborator Author

Commented by: Maxim Filatov (mnf)

In some case this lead to unpredictably results. Example:

CREATE TABLE CALC (
ID INTEGER NOT NULL,
RESULT DOUBLE PRECISION DEFAULT 0
);

COMMIT WORK;

INSERT INTO calc (id, result) VALUES (1, -1);
INSERT INTO calc (id, result) VALUES (2, 2);
INSERT INTO calc (id, result) VALUES (3, -3);
INSERT INTO calc (id, result) VALUES (0, 0);

COMMIT WORK;

SET TERM ^ ;

CREATE OR ALTER PROCEDURE REVERSE_CALC (
ID INTEGER)
RETURNS (
RESULT DOUBLE PRECISION)
AS
BEGIN
SELECT result FROM calc WHERE id = :id INTO :result;
UPDATE calc SET result = - :result WHERE id = :id;
result = COALESCE(result, 0);
SUSPEND;
END
^

SET TERM ; ^

COMMIT WORK;

SELECT (SELECT result FROM reverse_calc(http://c.id)) AS get_result,
c.*
FROM calc c
ORDER BY 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant