{"repo":"khasinski/estimate_count","free":true,"listed":false,"github":"https://github.com/khasinski/estimate_count","clone":"git clone https://github.com/khasinski/estimate_count.git","description":"Quickly estimate the number of rows for a given query","language":"Ruby","stars":22,"topics":["activerecord","mysql","performance","postgresql","rails","ruby","sql"],"license":"MIT","category":"databases-storage","readme_excerpt":"Estimate Count This gems help with a common pagination problem in which the calculation of total number of pages takes too long. Currently only PostgreSQL and MySQL are supported. Problem Let's say you have a table with millions of records and you want to paginate it. You also add filters and sorting. Suddenly your performance drops even though you're only displaying a few records per page. The problematic part is #count , which causes the entire scope to be calculated and then counted. This is slow. However you can use table statistics to estimate the number of records in the table (same as rows in EXPLAIN ). This is much faster. Be aware though that this rely on table statistics being refreshed from time to time. Installation Install the gem and add to the application's Gemfile by executing: $ bundle add estimate count Or add it to your Gemfile yourself and run bundle install . Usage This gem adds a method #estimate count to ActiveRecord::Relation which uses table statistics to estimate the number of records in the table. You can use it for any scope: You can pass threshold named argument to determine when estimate should fall back to a regular count. Default is 1000, use nil to always use estimate. Pagination example Given the following code: In a view: If you want to use estimate number of pages change the above line to: Paginator integrations estimate count ships with drop-in helpers for the three most popular Ruby paginators. Each adapter is loaded automatically when th","default_branch":null,"files":null,"tree":[],"storefront":"/r/khasinski","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/khasinski/estimate_count/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}