quick_random_records 0.3.0 → 0.3.1
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 +31 -4
- data/lib/quick_random_records/version.rb +1 -1
- data/lib/quick_random_records.rb +1 -1
- 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: a73a46ab4437ecf9169db037cc018ba179bc36ba
|
4
|
+
data.tar.gz: 9b068b8a67f71f9b88d91e5112937fc23118b6bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2a2f477ee383c8fc66fc6557899d9d3849215428b4098af9ad25489d332e4cd842390b4074e9e81a345a69b2d030733d52ea7ac46654728396592472b45cc84
|
7
|
+
data.tar.gz: 34e3a955bedd78e1393a23a54626b07093663da1a78bb8dc2796c91a984388ff801f0986a2663c48aa286a010e0aacf87dc9b606435f3aa6f7d94f5f99dbbd03
|
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.
|
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.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -45,22 +45,49 @@ Scenario: query 100 random records from table with 550,000 data rows.
|
|
45
45
|
## Fine-tuning
|
46
46
|
|
47
47
|
This strategy is fast because:
|
48
|
-
|
48
|
+
|
49
|
+
(1) Instead of plucking all id in the table, it selects id bewteen min_id and max_id.
|
49
50
|
Then make complements if any missing records (id between min_id and max_id, but not exist in db).
|
50
|
-
(2)
|
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.
|
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.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# select 1.1 times more than required, that is 110 here.
|
60
|
+
# And it will truncate to 100 before method return.
|
61
|
+
|
62
|
+
users = User.random_records(100, multiply: 1.1)
|
63
|
+
```
|
64
|
+
|
52
65
|
## Drawback
|
53
66
|
|
54
67
|
This strategy works extremely well with table that has a lot of records and few deleted records.
|
55
68
|
|
56
69
|
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
|
70
|
+
it may return fewer random records as required since I limit the loop searching for complements.
|
58
71
|
|
59
72
|
The default `loop_limit` is `3`. You can configure your own `loop_limit` for searching complements.
|
60
73
|
```ruby
|
61
74
|
users = User.random_records(100, loop_limit: 5)
|
62
75
|
```
|
63
76
|
|
77
|
+
or
|
78
|
+
|
79
|
+
You can use other strategy for tables with a lot of deleted records.
|
80
|
+
|
81
|
+
`Model.order("RAND()").limit(num)` is strategy 2
|
82
|
+
```ruby
|
83
|
+
users = User.random_records(100, strategy: 2)
|
84
|
+
```
|
85
|
+
|
86
|
+
`Model.where(id: Model.pluck(:id).sample(num))` is strategy 3
|
87
|
+
```ruby
|
88
|
+
users = User.random_records(100, strategy: 3)
|
89
|
+
```
|
90
|
+
|
64
91
|
## Development
|
65
92
|
|
66
93
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/quick_random_records.rb
CHANGED
@@ -2,7 +2,7 @@ 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, multiply: 1.
|
5
|
+
def self.random_records(quantity, strategy: 1, multiply: 1.05, loop_limit: 3)
|
6
6
|
case strategy
|
7
7
|
when 1
|
8
8
|
self.sample_complement_records(quantity, multiply, loop_limit)
|