estimate_count 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a183b9931539602b197f560cb1e2f3c1a23735e0ac88f3a1bdfe1e7d3055420a
4
- data.tar.gz: 1399ce07b08369ab9a30e2a92f70598d8e502da7c7505bc1970c331a461d9ff8
3
+ metadata.gz: 5c13bfac3669b65175b85eac38f0f924e5fa0d98c05c540149094c82b5d03242
4
+ data.tar.gz: 8ad7b5421936626f9329f02f9a3c5b0a3686e481a3c67d5b0d5baf834cef1123
5
5
  SHA512:
6
- metadata.gz: 202a74c4860aede412634c0dd9962b24c31663db14970fedd1dde06ff1de9e5fc12fede2d59ca5ca9e54210094721f06f37a22e4a60b45fc3ef3eb0282917e8f
7
- data.tar.gz: e825b7766d5eb08d63fbd59039f995bb4fb15c82602de9b42f1f0db1d4ffe6327be7b864e47b5ec575f081ad1ac5f354cc36bae9aa5d1d15824c8649d093379d
6
+ metadata.gz: c48c201949258f037290590387047420a72f391c78013f0a91a11ced87c00d636cb98ed41c4d43defcb4c31d6130a6e1144d977651420ff65db43607d9ada5d6
7
+ data.tar.gz: 9ea0058f76b3d5e2fba446a8b88c87ea92899276ff4638569d8c90a47c5b436279bf34b5a252570ac5556cac24fb4ee5bd3d77aca049e3b826b36b52177dd928
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [Unreleased]
6
+
7
+ ## 0.4.0 - 2020-05-21
8
+
9
+ ### Removed
10
+ - No longer depends on `pry` gem for production
11
+
12
+ ## 0.3.0 - 2021-04-20
13
+
14
+ ### Added
15
+ - MySQL support
16
+ - Tests for PostgreSQL and MySQL
17
+
18
+ ## 0.1.0 - 2021-03-04
19
+
20
+ ### Added
21
+ - Initial release
22
+ - `#estimate_count` method for `ActiveRecord::Relation` class, PostgreSQL version
data/Gemfile CHANGED
@@ -8,5 +8,3 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
-
12
- gem "pry"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- estimate_count (0.1.0)
4
+ estimate_count (0.4.0)
5
5
  activerecord
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,11 +2,17 @@
2
2
 
3
3
  This gems help with a common pagination problem in which the calculation of total number of pages takes too long.
4
4
 
5
- Currently only PostgreSQL is supported.
5
+ Currently only PostgreSQL and MySQL are supported.
6
6
 
7
7
  ## Problem
8
8
 
9
- Let's say you have a table with 1 million records and you want to paginate it. You add filters and sorting. You can use the `count` method to get the total number of records in the table. However, this method will take a long time to execute. This is because the database has to count all the records in the table.
9
+ Let's say you have a table with 1 million records and you want to paginate it. You add filters and sorting.
10
+
11
+ Suddenly your performance drops even though you're only displaying a few records per page.
12
+
13
+ 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.
14
+
15
+ Be aware though that this rely on table statistics being refreshed from time to time.
10
16
 
11
17
  ## Example
12
18
  Given the following code:
@@ -31,17 +37,13 @@ In a view:
31
37
  # app/views/users/index.html.erb
32
38
  Total pages - <%= @users.total_pages %>
33
39
  ```
34
- The above code will work fine. However, if you have a table with 1 million records, the `count` method will take a long time to execute. This is because the database has to count all the records in the table.
40
+ If you want to use estimate number of pages change the above line to:
35
41
 
36
42
  ```ruby
37
43
  # app/views/users/index.html.erb
38
- Total pages - About <%= (@users.estimate_count / @users.per_page).ceil %>
44
+ Total pages - About <%= (@users.estimate_count / @users.per_page).ceil %>
39
45
  ```
40
46
 
41
- This extracts the estimation from PostgreSQL statistics and uses it to calculate the total number of pages. This is much faster than the `count` method.
42
-
43
- You can use it with any pagination library (or without one).
44
-
45
47
  ## Installation
46
48
 
47
49
  Install the gem and add to the application's Gemfile by executing:
@@ -56,7 +58,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
56
58
 
57
59
  This gem adds a method `#estimate_count` to the `ActiveRecord::Relation` class.
58
60
 
59
- You can use it for any scope
61
+ You can use it for any scope:
60
62
 
61
63
  ```ruby
62
64
  User.active.estimate_count
@@ -73,7 +75,7 @@ User.active.estimate_count(threshold: 1000)
73
75
 
74
76
  ## Development
75
77
 
76
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
79
 
78
80
  To install this gem onto your local machine, run `bundle exec rake install`.
79
81
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EstimateCount
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: estimate_count
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hasiński
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-20 00:00:00.000000000 Z
11
+ date: 2023-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Uses EXPLAIN to get an estimate count for an ActiveRecord::Relation
70
84
  email:
71
85
  - krzysztof.hasinski@gmail.com
@@ -74,6 +88,7 @@ extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
90
  - ".rspec"
91
+ - CHANGELOG.md
77
92
  - CODE_OF_CONDUCT.md
78
93
  - Gemfile
79
94
  - Gemfile.lock