Skip to content

Conversation

@tiagopog
Copy link
Owner

@tiagopog tiagopog commented Jan 13, 2018

Closes #78

What's this PR do?

It brings some refactoring + fixes to the record count feature.

TODOs

  • Fix the execution of slow count queries when using AR objects with eager load (i.e. count with left joins);
    • If for some reason the effectiveness of the query is affected for skipping the left joins (e.g. where clauses) then rollback to the slow count;
  • Raise an error if no count strategy can be applied;
  • Apply memoization on the record count;
  • Write specs;
  • Bump up the gem's version.

Background context

At Beauty Date we have a production HTTP API fully based on JSON API using jsonapi-utils to parse request e build responses. While optimizing an endpoint that was suffering from "N+1 queries" problem, one of our developers (@giovannibonetti) noted that when using eager load on large tables it actually ends up with an extremely low count query because of the left join associations.

Manual test

# Explicit count
jsonapi_render json: User.all, options: { count: 42 }

# Array count
jsonapi_render json: User.all.to_a

# ActiveRecord count
jsonapi_render json: User.all

# ActiveRecord count with eager load
jsonapi_render json: User.all.includes(:posts)

# ActiveRecord count with eager load and where clause
jsonapi_render json: User.all.includes(:posts).where(posts: { id: Post.first.id }) 

@tiagopog tiagopog self-assigned this Jan 13, 2018
@tiagopog
Copy link
Owner Author

tiagopog commented Jan 13, 2018

@giovannibonetti I still need to write some unit tests but since it's +-90% done I think you can already have a 👀 on this PR if you want.

@tiagopog tiagopog merged commit 437319d into master Jan 15, 2018
@tiagopog tiagopog deleted the improvement/record-count-refactoring branch January 15, 2018 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rendering scopes with includes is slow for no reason

2 participants