Skip to content

Generating linkage data gets expensive with includes #1242

@st0012

Description

@st0012

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Bug reports:

This is not a feature bug, but a potential performance issue.

It's very common to eager load data in JR. But if we keep includes values when plucking for linkage data, it can make the query unnecessarily expensive.

Let's use Post model for example:

If we directly pluck all posts' created_at column, the query would be very simple

SELECT "posts"."created_at" FROM "posts"

But if we keep includes values in the previous relation object, like

Post.includes(:author).pluck(:created_at)

It generates unnecessarily complicated query

SELECT "posts"."created_at" FROM "posts" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id"

This is how we pluck polymorphic to_many linkage

assoc = source.public_send("records_for_#{relationship.name}", options)
......
assoc.pluck(:type, :id)

And we do apply includes in records_for_xxxx method

      define_on_resource associated_records_method_name do |options = {}|
        .....

        records = resource_klass.apply_includes(records, options)

This means our pluck query will be very expensive due to the previous includes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions