Plots2: Replace all instances of .length and .count with .size

Created on 25 Jul 2020  路  9Comments  路  Source: publiclab/plots2

Please describe the desired behavior.
In case of length, it aims to fetch all the entries of the db till the length is reached and then calculate the count. This leads us to unnecessarily fetching all the records.

Book.all.length
Book Load (1.3ms) SELECT "books".* FROM "books"
=> 100
books = Book.all
Book Load (1.3ms) SELECT "books".* FROM "books"
books.length
=> 100

While .count only calculates the count but if the entries are already fetched then it still makes an extra query.

books = Book.all
Book Load (1.3ms) SELECT "books".* FROM "books"
books.count
(0.2ms) SELECT COUNT(*) FROM "books"
=> 100

So we need to vary between these 2 methods keeping in mind where our records have been fetched already and where they have not. Thankfully we have the .size method that determine which method between count and length to call. If you have already loaded the entries it will use length. If you haven鈥檛 it will use count. Size can therefore be used in most cases to save you having to worry about any of the performance considerations.

Additional context (optional)
Reference: https://medium.com/@craigsheen/count-vs-length-in-rails-4308e83f6292

feature outreachy

All 9 comments

@jywarren @cesswairimu @icarito what do you think about this? I think replacing all instances of this with .size may solve some existing sql repitions flagged by skylight. :sweat_smile: and hopefully it should not break anything :joy:

Wow @Tlazypanda 馃挴
The time reduction from 1.3ms to 0.3 ms is really cool!!!
You can make many FTO's from this issue as well. Let's see what Jeff has to say regarding this. Thanks 馃槃

I think this is very important work and might be urgently needed. I've enabled Slow SQL query logs on our Google Cloud SQL instance in order for us to know for sure which queries are the ones that are slowing things down. Thank you for this work.

Is anyone already working on this refact? or can i try to do that?

This is great :+1: , I agree with the ftos idea @sagarpreet-chadha ...I will make some and assign them to the folks in the FTO waiting list...I will try to start with the queries that seem to be slowing queries so that we can escalate as @icarito mentioned

@gabrielbaldao I will ping you on the first issue that I create for this in the next few

Thanks everyone

Hey everyone 馃憢 Just noting here that sometimes test might fail for moving from .length to .size so whenever we are creating ftos we might need to take this into consideration and check 馃槄

Also for the slow routes I will try to mark the ones which we can get started with in a day or two or the ones in which tests don't fail 鉁岋笍

Hi :smile:, this issue has been automatically marked as stale because it has not had recent activity. Don't worry you can continue to work on this and ask @publiclab/reviewers to add "work in progress" label :tada: . Otherwise, it will be closed if no further activity occurs in 5 days -- but you can always re-open it if you like! :100: Thank you for your contributions :raised_hands: :balloon:.

@Tlazypanda I think we finished replacing all of these. Closing this. Thanks all

Was this page helpful?
0 / 5 - 0 ratings

Related issues

divyabaid16 picture divyabaid16  路  3Comments

keshavsethi picture keshavsethi  路  3Comments

jywarren picture jywarren  路  3Comments

milaaraujo picture milaaraujo  路  3Comments

grvsachdeva picture grvsachdeva  路  3Comments