random_order 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44c17e681b84961986ec40faa61117db2eebe889be510822c905bbed642cffa1
4
- data.tar.gz: 6c180250b563a70cff7aaca0aca869cc845e06a67a9e8f99217b8c18cec99d06
3
+ metadata.gz: 10e667214867ab310d3fc617f281ab0ee66fda471f88ef29a3852ddb0df82fb2
4
+ data.tar.gz: fac5acfe2fc876b7ea100be7a5aedbd1d39fc09074cbe35d518b741959571912
5
5
  SHA512:
6
- metadata.gz: 4284befb9765296e151379f4ab765bcfbae71168e2625f2367af371d862f3673c829dbbe33cfb0bd11f94816a4e86afc9ffca468326c7f958efbc2bcf22be672
7
- data.tar.gz: a40069226504d3f50a353d146735a6fe00c1e71e49d683146fca0538463e5ed713a9f7e5e3fc0e8bc8aa5e220719a411c0adff87fcb7a35627cd9b6ffad72f22
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` , `find_random` and `find_many_random(...)`, `fast_random(n = 1)`.
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 `88000 records` shows that it's `20` times faster. See screenshot with logs below:
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 = 10
23
+ n = 1000
24
+
26
25
  Benchmark.bm do |benchmark|
27
- benchmark.report("find_many_random(100)") do
28
- n.times do
29
- Business.find_many_random(100).to_a
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
- benchmark.report("fast_random(100)") do
33
- n.times do
34
- Business.fast_random(100).to_a
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.find_random.friends.random # return ActiveRecord::Relation with random user`s random friends
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
 
@@ -1,13 +1,5 @@
1
1
  module RandomOrder
2
2
  module Extension
3
- def find_many_random(n)
4
- random.limit(n)
5
- end
6
-
7
- def find_random
8
- random.first
9
- end
10
-
11
3
  def random
12
4
  self.order(RandomOrder.order_func)
13
5
  end
@@ -1,3 +1,3 @@
1
1
  module RandomOrder
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.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-21 00:00:00.000000000 Z
11
+ date: 2020-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails