quick_random_records 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a73a46ab4437ecf9169db037cc018ba179bc36ba
4
- data.tar.gz: 9b068b8a67f71f9b88d91e5112937fc23118b6bb
3
+ metadata.gz: 5910d6e62796a12933405fd5cee3bd1825597157
4
+ data.tar.gz: b884224d92b4c84dddd74fd10edab5bac2010336
5
5
  SHA512:
6
- metadata.gz: b2a2f477ee383c8fc66fc6557899d9d3849215428b4098af9ad25489d332e4cd842390b4074e9e81a345a69b2d030733d52ea7ac46654728396592472b45cc84
7
- data.tar.gz: 34e3a955bedd78e1393a23a54626b07093663da1a78bb8dc2796c91a984388ff801f0986a2663c48aa286a010e0aacf87dc9b606435f3aa6f7d94f5f99dbbd03
6
+ metadata.gz: c5cae62bb52b53b2f64fe6662f16aa6428a91fe9c5efc9ea1a8586542846b15eb0e363c891b3c1f601e61c177589c935565e5ca05e54dd7e55246ad0157a5701
7
+ data.tar.gz: 1c05891165bc08c1e22a972b534e3872b4b9a05e6db8fe25a6e0c0ad8a3812b71c8d709625d4d9a5416fa64e48a465ec29263000803de580afa55c34f5f06395
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## QuickRandomRecords
2
2
 
3
- `quick_random_records` is a Ruby Gem that empowers ActiveRecord Models with the ability to return random records dramatically fast, even with table that has a lot of data rows.
3
+ `quick_random_records` is a Ruby Gem that empowers ActiveRecord Model to return random records dramatically fast, even with tables that have lots of data rows.
4
4
 
5
5
  ## Installation
6
6
 
@@ -26,19 +26,19 @@ Or install it yourself as:
26
26
  users = User.random_records(10)
27
27
  ```
28
28
 
29
- ## Dramatically fast, compared to other random records strategies
29
+ ## Dramatically fast, compared to common random records strategies
30
30
 
31
- Scenario: query 100 random records from table with 550,000 data rows.
31
+ Scenario: query 100 random records from table with 550,000 data rows in localhost.
32
32
 
33
- 1. `quick_random_records` costs `25.0ms`.
34
- ![alt text](https://user-images.githubusercontent.com/19776127/40585122-b6a90cae-61df-11e8-8b54-96f238a370f2.png)
33
+ 1. `quick_random_records` costs only `6.2ms` totally.
34
+ ![alt text](https://user-images.githubusercontent.com/19776127/40586675-137b0f5a-61f8-11e8-85e3-4df7a96ed343.png)
35
35
  2. `Model.order("RAND()").limit(num)` costs `3314.1ms`.
36
36
  ![alt text](https://user-images.githubusercontent.com/19776127/40585124-b6f7b0a2-61df-11e8-9884-86f96354efbc.png)
37
- 3. `Model.where(id: Model.pluck(:id).sample(num))` costs `1659.4ms`.
37
+ 3. `Model.where(id: Model.pluck(:id).sample(num))` costs `1659.4ms` totally.
38
38
  ![alt text](https://user-images.githubusercontent.com/19776127/40585123-b6d07f00-61df-11e8-9622-e4cd61100e37.png)
39
39
 
40
40
 
41
- ![alt text](https://user-images.githubusercontent.com/19776127/40585160-59fe14bc-61e0-11e8-891f-ecd144d46905.png)
41
+ ![alt text](https://user-images.githubusercontent.com/19776127/40586737-e07cb99a-61f8-11e8-8d02-2a3dd4a832b5.png)
42
42
 
43
43
  ![alt text](https://user-images.githubusercontent.com/19776127/40585161-5add98b2-61e0-11e8-9265-11bef7a1536d.png)
44
44
 
@@ -49,14 +49,14 @@ This strategy is fast because:
49
49
  (1) Instead of plucking all id in the table, it selects id bewteen min_id and max_id.
50
50
  Then make complements if any missing records (id between min_id and max_id, but not exist in db).
51
51
 
52
- (2) It select id 1.25 times more than required. So that it doesn't need to perform another query to make complements.
53
- And of course, it will truncate to the required number before method return.
52
+ (2) It selects records 1.05 times more than required. So that it doesn't need to perform another query to make complements.
53
+ And of course, it will truncate to required number of records before method return.
54
54
 
55
- You can configure your own multiply, which is 1.25 by default.
56
- EX: My table has 10% deleted records, so multiply 1.1 will maximum the speed of random_records.
55
+ You can configure your own multiply, which is 1.05 by default.
56
+ EX: If table has 10% deleted records, multiply 1.1 will maximize the speed of random_records.
57
57
 
58
58
  ```ruby
59
- # select 1.1 times more than required, that is 110 here.
59
+ # select 1.1 times more than required, that is 110 in this case.
60
60
  # And it will truncate to 100 before method return.
61
61
 
62
62
  users = User.random_records(100, multiply: 1.1)
@@ -66,7 +66,7 @@ This strategy is fast because:
66
66
 
67
67
  This strategy works extremely well with table that has a lot of records and few deleted records.
68
68
 
69
- But for tables with a lot of deleted records (ex: 8 deleted reocrds out of 10 records),
69
+ But for tables with a lot of deleted records (ex: 8 deleted records out of 10 records),
70
70
  it may return fewer random records as required since I limit the loop searching for complements.
71
71
 
72
72
  The default `loop_limit` is `3`. You can configure your own `loop_limit` for searching complements.
@@ -74,16 +74,16 @@ The default `loop_limit` is `3`. You can configure your own `loop_limit` for sea
74
74
  users = User.random_records(100, loop_limit: 5)
75
75
  ```
76
76
 
77
- or
77
+ OR
78
78
 
79
79
  You can use other strategy for tables with a lot of deleted records.
80
80
 
81
- `Model.order("RAND()").limit(num)` is strategy 2
81
+ `Model.order("RAND()").limit(num)` is strategy 2 supported by this GEM
82
82
  ```ruby
83
83
  users = User.random_records(100, strategy: 2)
84
84
  ```
85
85
 
86
- `Model.where(id: Model.pluck(:id).sample(num))` is strategy 3
86
+ `Model.where(id: Model.pluck(:id).sample(num))` is strategy 3 supported by this GEM
87
87
  ```ruby
88
88
  users = User.random_records(100, strategy: 3)
89
89
  ```
@@ -1,15 +1,15 @@
1
1
  require 'quick_random_records/version'
2
2
  require 'active_record'
3
3
 
4
- class ActiveRecord::Base
5
- def self.random_records(quantity, strategy: 1, multiply: 1.05, loop_limit: 3)
4
+ class << ActiveRecord::Base
5
+ def random_records(quantity, strategy: 1, multiply: 1.05, loop_limit: 3)
6
6
  case strategy
7
7
  when 1
8
- self.sample_complement_records(quantity, multiply, loop_limit)
8
+ sample_complement_records(quantity, multiply, loop_limit)
9
9
  when 2
10
- self.order_rand_limit_records(quantity)
10
+ order_rand_limit_records(quantity)
11
11
  when 3
12
- self.pluck_sample_records(quantity)
12
+ pluck_sample_records(quantity)
13
13
  else
14
14
  "this gem doesn't support strategy other than 1, 2, 3"
15
15
  end
@@ -17,8 +17,8 @@ class ActiveRecord::Base
17
17
 
18
18
  private
19
19
 
20
- def self.sample_complement_records(quantity, multiply, loop_limit)
21
- min_max = self.pluck('MIN(id), MAX(id)').first
20
+ def sample_complement_records(quantity, multiply, loop_limit)
21
+ min_max = self.pluck(Arel.sql("MIN(#{self.table_name}.id), MAX(#{self.table_name}.id)")).first
22
22
  id_range = (min_max[0]..min_max[1])
23
23
 
24
24
  samples = [*id_range].sample(quantity * multiply)
@@ -42,19 +42,19 @@ class ActiveRecord::Base
42
42
  self.where(id: exist_samples[0...quantity])
43
43
  end
44
44
 
45
- def self.order_rand_limit_records(quantity)
45
+ def order_rand_limit_records(quantity)
46
46
  adapter_type = connection.adapter_name.downcase.to_sym
47
47
  case adapter_type
48
48
  when :mysql, :mysql2
49
- self.order("RAND()").limit(quantity)
49
+ self.order(Arel.sql("RAND()")).limit(quantity)
50
50
  when :sqlite, :postgresql
51
- self.order("RANDOM()").limit(quantity)
51
+ self.order(Arel.sql("RANDOM()")).limit(quantity)
52
52
  else
53
53
  raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
54
54
  end
55
55
  end
56
56
 
57
- def self.pluck_sample_records(quantity)
57
+ def pluck_sample_records(quantity)
58
58
  ids = self.pluck(:id).sample(quantity)
59
59
  self.where(id: ids)
60
60
  end
@@ -1,3 +1,3 @@
1
1
  module QuickRandomRecords
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.11"
23
23
  spec.add_development_dependency "rake", "~> 12.0"
24
- spec.add_development_dependency "sqlite3", "~> 1.3"
24
+ spec.add_development_dependency "sqlite3", "~> 1.3.6"
25
25
  spec.add_development_dependency "minitest", "~> 5.0"
26
26
 
27
27
  spec.add_dependency "activerecord", ">= 3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick_random_records
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - derekfan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-27 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.3'
47
+ version: 1.3.6
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: 1.3.6
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.6.13
122
+ rubygems_version: 2.5.2.3
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Returns random records for Ruby Models fast