delete_in_batches 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +1 -1
- data/README.md +25 -23
- data/lib/delete_in_batches.rb +7 -0
- data/lib/delete_in_batches/version.rb +1 -1
- metadata +9 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a9e277e2aab5a09816e7df3578eb2a585d44e2fd1fe28a81c9f7606e9e6a6b9
|
4
|
+
data.tar.gz: 6df5995b465ef126c9f745d1a1553a7c90f24e65f04263fe673bde2c6593286e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2cc30e03280311704551c681d57dac4badf6bb483834396bf8d7e4a0034cef9d5d910aab93c449936f1be6332ff49addba4f39d56c2fb769cd53f9f60691051
|
7
|
+
data.tar.gz: '08085780689fef205afe684d4d82a2deccb7325bac761bf47e0d7164e57d7a7965195469ea1b3396832bacd981284e290bbe58320107edd8f88ceb0099b0c109'
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# delete_in_batches
|
2
2
|
|
3
|
-
:fire:
|
3
|
+
:fire: Fast batch deletes for Active Record and Postgres
|
4
4
|
|
5
|
-
[![Build Status](https://
|
5
|
+
[![Build Status](https://github.com/ankane/delete_in_batches/workflows/build/badge.svg?branch=master)](https://github.com/ankane/delete_in_batches/actions)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -12,31 +12,12 @@ Add this line to your application’s Gemfile:
|
|
12
12
|
gem 'delete_in_batches'
|
13
13
|
```
|
14
14
|
|
15
|
-
##
|
15
|
+
## How to Use
|
16
16
|
|
17
|
-
|
18
|
-
Tweet.where(user_id: 1).delete_all
|
19
|
-
# DELETE FROM tweets WHERE user_id = 1
|
20
|
-
```
|
21
|
-
|
22
|
-
The database performs the delete in a transaction - either all the records are deleted (query completes) or none are, due to [multiversion concurrency control](http://en.wikipedia.org/wiki/Multiversion_concurrency_control).
|
23
|
-
|
24
|
-
## Faster
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
Tweet.where(user_id: 1).in_batches(of: 10000).delete_all
|
28
|
-
# SELECT tweets.id FROM tweets WHERE user_id = 1 ORDER BY id LIMIT 1000
|
29
|
-
# DELETE FROM tweets WHERE user_id = 1 AND id IN (1, 2, 3, ...)
|
30
|
-
# ...
|
31
|
-
```
|
32
|
-
|
33
|
-
## Fastest
|
17
|
+
Delete rows in batches
|
34
18
|
|
35
19
|
```ruby
|
36
20
|
Tweet.where(user_id: 1).delete_in_batches
|
37
|
-
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
|
38
|
-
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
|
39
|
-
# ...
|
40
21
|
```
|
41
22
|
|
42
23
|
**Important:** Be sure to test your query before running it in production
|
@@ -47,6 +28,12 @@ Change the batch size
|
|
47
28
|
Tweet.where(user_id: 1).delete_in_batches(batch_size: 50000) # defaults to 10000
|
48
29
|
```
|
49
30
|
|
31
|
+
Sleep between batches
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Tweet.where(user_id: 1).delete_in_batches(sleep: 0.01)
|
35
|
+
```
|
36
|
+
|
50
37
|
Show progress
|
51
38
|
|
52
39
|
```ruby
|
@@ -67,6 +54,12 @@ To delete all rows in a table, `TRUNCATE` is fastest.
|
|
67
54
|
ActiveRecord::Base.connection.execute("TRUNCATE tweets")
|
68
55
|
```
|
69
56
|
|
57
|
+
## History
|
58
|
+
|
59
|
+
View the [changelog](https://github.com/ankane/delete_in_batches/blob/master/CHANGELOG.md)
|
60
|
+
|
61
|
+
**Note:** This project originally had the description “the fastest way to delete 100k+ rows with ActiveRecord” but a single `DELETE` statement will likely be faster. See [this discussion](https://github.com/ankane/delete_in_batches/issues/4) for more details.
|
62
|
+
|
70
63
|
## Contributing
|
71
64
|
|
72
65
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
@@ -75,3 +68,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
75
68
|
- Fix bugs and [submit pull requests](https://github.com/ankane/delete_in_batches/pulls)
|
76
69
|
- Write, clarify, or fix documentation
|
77
70
|
- Suggest or add new features
|
71
|
+
|
72
|
+
To get started with development:
|
73
|
+
|
74
|
+
```sh
|
75
|
+
git clone https://github.com/ankane/delete_in_batches.git
|
76
|
+
cd delete_in_batches
|
77
|
+
bundle install
|
78
|
+
bundle exec rake test
|
79
|
+
```
|
data/lib/delete_in_batches.rb
CHANGED
@@ -5,6 +5,7 @@ require "active_support"
|
|
5
5
|
require "delete_in_batches/version"
|
6
6
|
|
7
7
|
module DeleteInBatches
|
8
|
+
# TODO use keyword arguments
|
8
9
|
def delete_in_batches(options = {})
|
9
10
|
batch_size = options[:batch_size] || 10000
|
10
11
|
|
@@ -16,8 +17,14 @@ module DeleteInBatches
|
|
16
17
|
sql = "SELECT * FROM (#{sql}) AS t"
|
17
18
|
end
|
18
19
|
|
20
|
+
unless connection.adapter_name =~ /postg/i
|
21
|
+
# TODO raise error
|
22
|
+
warn "[delete_in_batches] Use in_batches(of: #{batch_size.to_i}).delete_all instead of this gem for non-Postgres databases"
|
23
|
+
end
|
24
|
+
|
19
25
|
while connection.delete("DELETE FROM #{quoted_table_name} WHERE #{pk} IN (#{sql})") == batch_size
|
20
26
|
yield if block_given?
|
27
|
+
sleep(options[:sleep]) if options[:sleep]
|
21
28
|
end
|
22
29
|
end
|
23
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_in_batches
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,64 +24,8 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5'
|
27
|
-
|
28
|
-
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: minitest
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: sqlite3
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
description:
|
84
|
-
email: andrew@chartkick.com
|
27
|
+
description:
|
28
|
+
email: andrew@ankane.org
|
85
29
|
executables: []
|
86
30
|
extensions: []
|
87
31
|
extra_rdoc_files: []
|
@@ -95,7 +39,7 @@ homepage: https://github.com/ankane/delete_in_batches
|
|
95
39
|
licenses:
|
96
40
|
- MIT
|
97
41
|
metadata: {}
|
98
|
-
post_install_message:
|
42
|
+
post_install_message:
|
99
43
|
rdoc_options: []
|
100
44
|
require_paths:
|
101
45
|
- lib
|
@@ -110,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
54
|
- !ruby/object:Gem::Version
|
111
55
|
version: '0'
|
112
56
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
114
|
-
signing_key:
|
57
|
+
rubygems_version: 3.2.3
|
58
|
+
signing_key:
|
115
59
|
specification_version: 4
|
116
|
-
summary:
|
60
|
+
summary: Fast batch deletes for Active Record and Postgres
|
117
61
|
test_files: []
|