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 +4 -4
- data/README.md +16 -16
- data/lib/quick_random_records.rb +11 -11
- data/lib/quick_random_records/version.rb +1 -1
- data/quick_random_records.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5910d6e62796a12933405fd5cee3bd1825597157
|
4
|
+
data.tar.gz: b884224d92b4c84dddd74fd10edab5bac2010336
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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 `
|
34
|
-

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

|
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
|

|
39
39
|
|
40
40
|
|
41
|
-

|
42
42
|
|
43
43
|

|
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
|
53
|
-
And of course, it will truncate to
|
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.
|
56
|
-
EX:
|
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
|
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
|
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
|
-
|
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
|
```
|
data/lib/quick_random_records.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'quick_random_records/version'
|
2
2
|
require 'active_record'
|
3
3
|
|
4
|
-
class ActiveRecord::Base
|
5
|
-
def
|
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
|
-
|
8
|
+
sample_complement_records(quantity, multiply, loop_limit)
|
9
9
|
when 2
|
10
|
-
|
10
|
+
order_rand_limit_records(quantity)
|
11
11
|
when 3
|
12
|
-
|
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
|
21
|
-
min_max = self.pluck(
|
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
|
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
|
57
|
+
def pluck_sample_records(quantity)
|
58
58
|
ids = self.pluck(:id).sample(quantity)
|
59
59
|
self.where(id: ids)
|
60
60
|
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.
|
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:
|
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:
|
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:
|
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.
|
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
|