quick_random_records 0.2.0 → 0.3.0

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: 8460dba37c8d16afde91b5f8ed2e85edb122c5ef
4
- data.tar.gz: 3ff4a0cdc2df474bfb3e11718e25669582b75797
3
+ metadata.gz: 2bb0588241c2d513e8bbb2ae843cc2b67f6c4bf0
4
+ data.tar.gz: cd5da0a147961a9c9ece4bd8a81610fcd21077e6
5
5
  SHA512:
6
- metadata.gz: 33fe51141b1fb07b2a0fdd77d7f188c69da47bcee9854de1513446a205d59b5e2fdad410764840211a8a38d573ad82f8bf13b9c83d656735cf0f2d0d2f8c24db
7
- data.tar.gz: 2019ea12883b3bc1c6e848709b5c9ba2c44e95a933242a8231e0886a9c91842113d20801b2764b94a55be4a1fef49973b65ef9f4f166c3d013e25596a7e9f360
6
+ metadata.gz: 37b792a2d9a89aa3c6441be805c8174e5a8c8311fafffde4a00f1e6aa3a6f3e3006da0a79e45d7ae8c2780538e7f0c4325b73d6a7c7592f2dcdf565e147806b3
7
+ data.tar.gz: 38ec1f3608797b7b6c699904212d967ac4bc618ad1fd06dd74d3ec2c90ae84072941398971ab3d819f4e9c7cafeee7b696d9d15a8bfc3531db1c805ff7c53628
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # QuickRandomRecords
1
+ ## QuickRandomRecords
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/quick_random_records`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ `quick_random_records` is a Ruby Gem that empowers ActiveRecord Models with the ability to return random records dramatically fast.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,46 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ```ruby
24
+ # return ActiveRecord::Relation contains 10 random model objects from User Table
25
+
26
+ users = User.random_records(10)
27
+ ```
28
+
29
+ ## Dramatically fast, compared to other random records strategies
30
+
31
+ Scenario: query 100 random records from table with 550,000 data rows.
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)
35
+ 2. `Model.order("RAND()").limit(num)` costs `3314.1ms`.
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`.
38
+ ![alt text](https://user-images.githubusercontent.com/19776127/40585123-b6d07f00-61df-11e8-9622-e4cd61100e37.png)
39
+
40
+
41
+ ![alt text](https://user-images.githubusercontent.com/19776127/40585160-59fe14bc-61e0-11e8-891f-ecd144d46905.png)
42
+
43
+ ![alt text](https://user-images.githubusercontent.com/19776127/40585161-5add98b2-61e0-11e8-9265-11bef7a1536d.png)
44
+
45
+ ## Fine-tuning
46
+
47
+ This strategy is fast because:
48
+ (1) Instead of plucking all ids in the table, it selects ids bewteen min_id and max_id.
49
+ Then make complements if any missing records (id between min_id and max_id, but not exist in db).
50
+ (2)
51
+
52
+ ## Drawback
53
+
54
+ This strategy works extremely well with table that has a lot of records and few deleted records.
55
+
56
+ But for tables with a lot of deleted records (ex: 8 deleted reocrds out of 10 records),
57
+ it may return fewer random records as expected since I limit the loop it searchs for complements.
58
+
59
+ The default `loop_limit` is `3`. You can configure your own `loop_limit` for searching complements.
60
+ ```ruby
61
+ users = User.random_records(100, loop_limit: 5)
62
+ ```
26
63
 
27
64
  ## Development
28
65
 
@@ -37,7 +74,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
37
74
  ## License
38
75
 
39
76
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the QuickRandomRecords project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/quick_random_records/blob/master/CODE_OF_CONDUCT.md).
@@ -1,3 +1,3 @@
1
1
  module QuickRandomRecords
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -2,10 +2,10 @@ require 'quick_random_records/version'
2
2
  require 'active_record'
3
3
 
4
4
  class ActiveRecord::Base
5
- def self.random_records(quantity, strategy: 1, multiple: 1.25, loop_limit: 3)
5
+ def self.random_records(quantity, strategy: 1, multiply: 1.25, loop_limit: 3)
6
6
  case strategy
7
7
  when 1
8
- self.sample_complement_records(quantity, multiple, loop_limit)
8
+ self.sample_complement_records(quantity, multiply, loop_limit)
9
9
  when 2
10
10
  self.order_rand_limit_records(quantity)
11
11
  when 3
@@ -17,20 +17,20 @@ class ActiveRecord::Base
17
17
 
18
18
  private
19
19
 
20
- def self.sample_complement_records(quantity, multiple, loop_limit)
20
+ def self.sample_complement_records(quantity, multiply, loop_limit)
21
21
  min_max = self.pluck('MIN(id), MAX(id)').first
22
22
  id_range = (min_max[0]..min_max[1])
23
23
 
24
- samples = [*id_range].sample(quantity * multiple)
24
+ samples = [*id_range].sample(quantity * multiply)
25
25
  exist_samples = self.where(id: samples).pluck(:id)
26
26
  exist_samples_size = exist_samples.size
27
27
  deficit = quantity - exist_samples_size
28
28
  exist_samples_size = 1 if exist_samples_size.zero?
29
- deficit_weight = quantity * multiple / exist_samples_size
29
+ deficit_weight = quantity * multiply / exist_samples_size
30
30
  n = 1
31
31
 
32
32
  while deficit > 0 && n <= loop_limit
33
- complements = ([*id_range] - samples).sample(deficit * multiple * deficit_weight * n)
33
+ complements = ([*id_range] - samples).sample(deficit * multiply * deficit_weight * n)
34
34
  exist_complements = self.where(id: complements).pluck(:id)
35
35
 
36
36
  deficit -= exist_complements.size
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick_random_records
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - derekfan