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 +4 -4
- data/README.md +42 -9
- data/lib/quick_random_records/version.rb +1 -1
- data/lib/quick_random_records.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bb0588241c2d513e8bbb2ae843cc2b67f6c4bf0
|
4
|
+
data.tar.gz: cd5da0a147961a9c9ece4bd8a81610fcd21077e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37b792a2d9a89aa3c6441be805c8174e5a8c8311fafffde4a00f1e6aa3a6f3e3006da0a79e45d7ae8c2780538e7f0c4325b73d6a7c7592f2dcdf565e147806b3
|
7
|
+
data.tar.gz: 38ec1f3608797b7b6c699904212d967ac4bc618ad1fd06dd74d3ec2c90ae84072941398971ab3d819f4e9c7cafeee7b696d9d15a8bfc3531db1c805ff7c53628
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
|
1
|
+
## QuickRandomRecords
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
+

|
35
|
+
2. `Model.order("RAND()").limit(num)` costs `3314.1ms`.
|
36
|
+

|
37
|
+
3. `Model.where(id: Model.pluck(:id).sample(num))` costs `1659.4ms`.
|
38
|
+

|
39
|
+
|
40
|
+
|
41
|
+

|
42
|
+
|
43
|
+

|
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).
|
data/lib/quick_random_records.rb
CHANGED
@@ -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,
|
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,
|
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,
|
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 *
|
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 *
|
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 *
|
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
|