Skip to content

Querying an interface produces very slow query with a lot of UNION #5061

@Masadow

Description

@Masadow

Describe the bug
When querying an interface, neo4j generates a union for every subtypes of that interface resulting in very slow queries even if you don't request type conditions

Type definitions

interface Parent {
  id: ID
  label: String
}

type Child1 implements Parent {
  id: ID
  label: String
}

type Child2 implements Parent {
  id: ID
  label: String
}

...

type Child10 implements Parent {
  id: ID
  label: String
}

To Reproduce

Populate some nodes and relations and execute the graphql query :

query SlowQuery {
   parents(where: {id: "113"}) {
      id
      label
   }
}

Using

The generated query resembles to

CALL {
  MATCH (this0:Child1 {id: $param0})
  WITH this0 { .id, __resolveType: "Child1", __id: id(this0) } AS this0
  RETURN this0 AS this
  UNION
  MATCH (this1:Child2 {id: $param1})
  WITH this1 { .id, __resolveType: "Child2", __id: id(this1) } AS this1
  RETURN this1 AS this
  UNION
  ...
  MATCH (this9:Child10 {id: $param9})
  WITH this9 { .id, __resolveType: "Child10", __id: id(this9) } AS this9
  RETURN this9 AS this
}
WITH this
RETURN this as this

Execution plan with my real world schema for the generated query (structure is identical, just names changed) looks like :

plan

Expected behavior

This should be instantaneous since we're requesting a single node with a unique id filter however, it can takes up to several seconds to execute because of all the unions trying to successively clear for distinct values.

System (please complete the following information):

  • OS: reproduced on several kind of OS
  • Version: @neo4j/[email protected]
  • Node.js version: 20.x.x

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions