JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Performance on selecting all columns instead of specified attributes

Open
#1,389 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

Is there a reason the _model_class queries all columns?

https://github.com/cerebris/jsonapi-resources/blob/bf4b4cd7d79da453372880068e167066f0d05f47/lib/jsonapi/active_relation_resource.rb#L286-L288

This is a huge performance impact on models with many columns that may not ever be used by the resource.

It seems for performance reasons it makes sense to only select the attributes defined in the JSONAPI::Resource class.

A workaround for now is to override each resource using .select to retrieve only the fields that are specifically defined.

attributes :name, :address

def self.records(_options = {})
  super.select(self.fields)
end

This should also take into account sparse fieldsets parameters, which would further limit which columns are selected.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/jsonapi/active_relation_resource.rb at the linked _model_class query and trace how Resource attributes and sparse fieldsets are represented. The work is done when those queries select only the fields needed by the resource and request fieldset, while preserving the existing records behavior and covering the relevant query paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend-api-design, databases, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.