Oracle XE 21c query from ORM (EF core) giving wrong result

34 Views Asked by At

Table "Relationships" with (among the others) 3 not-nullable number columns:

  • "Id" being the PK
  • "LeftAccountId" with an index on it, being a FK
  • "RightAccountId" with an index on it, being a FK too

This table contains only one row:

Id LeftAccountId RightAccountId
1 2 1

If I run the query

SELECT "r"."Id", "r"."LeftAccountId", "r"."RightAccountId"
FROM "Relationships" "r"
WHERE
    "r"."Id" <> :Id
;

and give Id the value 1 it (correctly) returns an empty set.

If I instead run the query

SELECT "r"."Id", "r"."LeftAccountId", "r"."RightAccountId"
FROM "Relationships" "r"
WHERE
    "r"."Id" <> :Id
    AND
    (
        :Id = "r"."LeftAccountId"
        OR
        :Id = "r"."RightAccountId"
    )
;

and pass the value 1 to Id it returns the only row in the table. Also, if I give it the value 2 it returns an empty set.

That's the SQL plan I get from Oracle SQL Developer

SQL Plan with parameters

I'm kinda new to Oracle development, but it seems like it should work.

If I replace the :Id parameters in the query directly with the 1 or 2 value then proper results are returned, but the SQL plan changes a little bit:

SQL Plan without parameters

The main two changes I see are the predicate on the PK being inverted with respect to the query (it changes from a <> to an =) and the combination of the bitmaps changing from a BITMAP AND to a BITMAP MINUS

If I run the same queries on an identical database on Oracle 19c, there everything works.

Someone may shed some light on me?

0

There are 0 best solutions below