random_order 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 +34 -16
- data/lib/random_order/random_order/extension.rb +0 -8
- data/lib/random_order/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10e667214867ab310d3fc617f281ab0ee66fda471f88ef29a3852ddb0df82fb2
|
4
|
+
data.tar.gz: fac5acfe2fc876b7ea100be7a5aedbd1d39fc09074cbe35d518b741959571912
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86c24d152df40b67170ddec0626dc90e467178edd7155efc47ce885ff05e8a228ad669fc6780af7f58e5007259b47b0a52b8a177947d8dc073a7cbf57422f1b6
|
7
|
+
data.tar.gz: e7d7d1d94617c711d0994da133353e2fed7095fcaa0b788ec5430f4cde63f409de8100ab1252a6da39635068ff8ebbcc3b3e1bb231d4c2f374c91e67171fc2f3
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ With this gem, you can return a random record from the database using SQL or use
|
|
4
4
|
## Usage
|
5
5
|
Works with `MySQL`, `SQLite`, `PostgreSQL`.
|
6
6
|
|
7
|
-
This gem is adding three methods to ActiveRecord Relations - `random`
|
7
|
+
This gem is adding three methods to ActiveRecord Relations - `random` and `fast_random(n = 1)`.
|
8
8
|
|
9
9
|
You can do the following:
|
10
10
|
```ruby
|
@@ -13,40 +13,58 @@ u2 = User.create(name: 'Metamorph')
|
|
13
13
|
u3 = User.create(name: 'Diablo')
|
14
14
|
|
15
15
|
assert_equal User.random.is_a?(ActiveRecord::Relation), true
|
16
|
-
assert_equal User.find_many_random(10).is_a?(ActiveRecord::Relation), true
|
17
16
|
assert_equal User.fast_random.is_a?(ActiveRecord::Relation), true
|
18
|
-
assert_equal User.find_random.is_a?(User), true
|
19
17
|
```
|
20
18
|
## Fast Random method
|
21
19
|
As you may know `order by RANDOM()` might be slow in case you have many records in DB. I think the case I suggest to use another method which is bundled with this gem fast_random which is using another SQL to return random records from DB. It has some limitations like your table must have a primary key column.
|
22
|
-
Simple benchmark on the table with `
|
20
|
+
Simple benchmark on the table with `113000 records` shows that it's faster. See screenshot with logs below:
|
23
21
|
|
24
22
|
```ruby
|
25
|
-
n =
|
23
|
+
n = 1000
|
24
|
+
|
26
25
|
Benchmark.bm do |benchmark|
|
27
|
-
benchmark.report("
|
28
|
-
n.times do
|
29
|
-
|
26
|
+
benchmark.report("random.limit(10)") do
|
27
|
+
n.times do |nn|
|
28
|
+
Events.random.limit(10).to_a
|
30
29
|
end
|
31
30
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
|
32
|
+
benchmark.report("fast_random(10)") do
|
33
|
+
n.times do |nn|
|
34
|
+
Events.fast_random(10).to_a
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
|
+
user system total real
|
39
|
+
random.limit(10) 2.612499 0.193492 2.805991 (1162.501909)
|
40
|
+
fast_random(10) 2.234706 0.140195 2.374901 ( 37.173482)
|
41
|
+
|
42
|
+
# Find one random record
|
43
|
+
Benchmark.bm do |benchmark|
|
44
|
+
benchmark.report("random.first.id") do
|
45
|
+
n.times do |nn|
|
46
|
+
Events.random.first.id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
benchmark.report("fast_random.first.id") do
|
51
|
+
n.times do |nn|
|
52
|
+
Events.fast_random.first.id
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
#
|
57
|
+
user system total real
|
58
|
+
random.first.id 2.014658 0.134942 2.149600 (1151.981899)
|
59
|
+
fast_random.first.id 1.661777 0.067484 1.729261 ( 35.350424)
|
38
60
|
#
|
39
|
-
user system total real
|
40
|
-
find_many_random(100) 0.088990 0.025837 0.114827 ( 13.836576)
|
41
|
-
fast_random(100) 0.060209 0.013933 0.074142 ( 0.699557)
|
42
61
|
```
|
43
62
|
|
44
63
|
#### Examples:
|
45
64
|
```ruby
|
46
65
|
User.friends.random # return ActiveRecord::Relation with user`s random friends
|
47
|
-
User.
|
66
|
+
User.random.first.friends.random # return ActiveRecord::Relation with random user`s random friends
|
48
67
|
Post.published.random.limit(3) # return ActiveRecord::Relation with 3 published posts
|
49
|
-
Post.published.find_many_random(3) # --//--
|
50
68
|
Post.published.fast_random(3) # --//-- and faster
|
51
69
|
```
|
52
70
|
|
data/lib/random_order/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitalii Kasianchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|