delete_in_batches 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/delete_in_batches.gemspec +23 -0
- data/lib/delete_in_batches.rb +26 -0
- data/lib/delete_in_batches/version.rb +3 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a63cc69d5539bdab7a0cbc60bf015049ca8ce729
|
4
|
+
data.tar.gz: 6f213288c1f9a52666af9d9cb01b5ec615ad02ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9a00e37f6272dff4674fcbbbaea290b8c047265a51718c07fce8bf2ca5584288341db59b377fe5be3132b3fd0aeef85a1ad05487f86a2f4fcd4071dc6db29d2
|
7
|
+
data.tar.gz: a02e0da81efae6cb1d4782a4401b83d9133bb3253e3d96e4f817e34834e4168ad4ac67f5eb54bfc28673433b25e01cbd91da406a20e3727d9258ecd502be0df0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# delete_in_batches
|
2
|
+
|
3
|
+
:fire: The fastest way to delete millions of rows with ActiveRecord
|
4
|
+
|
5
|
+
## Slow
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Tweet.where(user_id: 1).delete_all
|
9
|
+
# DELETE FROM tweets WHERE user_id = 1
|
10
|
+
```
|
11
|
+
|
12
|
+
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).
|
13
|
+
|
14
|
+
## Fast
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
Tweet.where(user_id: 1).delete_in_batches
|
18
|
+
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
|
19
|
+
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
|
20
|
+
# ...
|
21
|
+
```
|
22
|
+
|
23
|
+
**Important:** Be sure to test your query before running it in production
|
24
|
+
|
25
|
+
Change the batch size
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Tweet.where(user_id: 1).delete_in_batches(batch_size: 50000) # defaults to 10000
|
29
|
+
```
|
30
|
+
|
31
|
+
Show progress
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Tweet.where(user_id: 1).delete_in_batches do
|
35
|
+
puts "Another batch deleted"
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Works with associations
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
user.tweets.delete_in_batches
|
43
|
+
```
|
44
|
+
|
45
|
+
To delete all rows in a table, `TRUNCATE` is fastest.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
ActiveRecord::Base.connection.execute("TRUNCATE tweets")
|
49
|
+
```
|
50
|
+
|
51
|
+
## Installation
|
52
|
+
|
53
|
+
Add this line to your application’s Gemfile:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
gem "delete_in_batches"
|
57
|
+
```
|
58
|
+
|
59
|
+
And then execute:
|
60
|
+
|
61
|
+
```sh
|
62
|
+
bundle
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
68
|
+
|
69
|
+
- [Report bugs](https://github.com/ankane/delete_in_batches/issues)
|
70
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/delete_in_batches/pulls)
|
71
|
+
- Write, clarify, or fix documentation
|
72
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'delete_in_batches/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "delete_in_batches"
|
8
|
+
spec.version = DeleteInBatches::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.summary = %q{The fastest way to delete millions of rows with ActiveRecord}
|
12
|
+
spec.description = %q{The fastest way to delete millions of rows with ActiveRecord}
|
13
|
+
spec.homepage = "https://github.com/ankane/delete_in_batches"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "delete_in_batches/version"
|
2
|
+
|
3
|
+
module DeleteInBatches
|
4
|
+
|
5
|
+
def delete_in_batches(options = {}, &block)
|
6
|
+
batch_size = options[:batch_size] || 10000
|
7
|
+
|
8
|
+
# TODO dry
|
9
|
+
sql =
|
10
|
+
if connection.respond_to?(:unprepared_statement)
|
11
|
+
# ActiveRecord 4
|
12
|
+
connection.unprepared_statement do
|
13
|
+
select(:id).limit(batch_size).to_sql
|
14
|
+
end
|
15
|
+
else
|
16
|
+
select(:id).limit(batch_size).to_sql
|
17
|
+
end
|
18
|
+
|
19
|
+
while connection.delete("DELETE FROM #{table_name} WHERE id IN (#{sql})") == batch_size
|
20
|
+
yield if block_given?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveRecord::Base.send :extend, DeleteInBatches
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delete_in_batches
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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
|
+
description: The fastest way to delete millions of rows with ActiveRecord
|
42
|
+
email:
|
43
|
+
- andrew@chartkick.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- delete_in_batches.gemspec
|
54
|
+
- lib/delete_in_batches.rb
|
55
|
+
- lib/delete_in_batches/version.rb
|
56
|
+
homepage: https://github.com/ankane/delete_in_batches
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.0
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: The fastest way to delete millions of rows with ActiveRecord
|
80
|
+
test_files: []
|