README.md in upsert-0.1.2 vs README.md in upsert-0.2.0
- old
+ new
@@ -16,22 +16,31 @@
upsert = Upsert.new Pet.connection, Pet.table_name
selector = {:name => 'Jerry'}
document = {:breed => 'beagle'}
upsert.row selector, document
-### Multiple upserts bundled together for speed
+### Streaming upserts (fastest)
+Rows are buffered in memory until it's efficient to send them to the database.
+
Upsert.stream(Pet.connection, Pet.table_name) do |upsert|
# [...]
upsert.row({:name => 'Jerry'}, :breed => 'beagle')
# [...]
upsert.row({:name => 'Pierre'}, :breed => 'tabby')
# [...]
end
-Rows are buffered in memory until it's efficient to send them to the database.
+### With a helper method
+For bulk upserts, you probably still want to use `Upsert.stream`.
+
+ # be sure to require 'upsert/active_record_upsert' - it's not required by default
+ selector = {:name => 'Jerry'}
+ document = {:breed => 'beagle'}
+ Pet.upsert selector, document
+
## Real-world usage
<p><a href="http://brighterplanet.com"><img src="https://s3.amazonaws.com/static.brighterplanet.com/assets/logos/flush-left/inline/green/rasterized/brighter_planet-160-transparent.png" alt="Brighter Planet logo"/></a></p>
We use `upsert` for [big data processing at Brighter Planet](http://brighterplanet.com/research) and in production at
@@ -52,13 +61,13 @@
#### Speed
From the tests:
Upsert was 77% faster than find + new/set/save
- Upsert was 84% faster than create + rescue/find/update
- Upsert was 82% faster than find_or_create + update_attributes
- Upsert was 47% faster than faking upserts with activerecord-import
+ Upsert was 58% faster than create + rescue/find/update
+ Upsert was 80% faster than find_or_create + update_attributes
+ Upsert was 39% faster than faking upserts with activerecord-import
#### SQL MERGE trick
"ON DUPLICATE KEY UPDATE" where we just set everything to the value of the insert.
@@ -196,14 +205,9 @@
This, however, only works on MySQL and requires ActiveRecord—and if all you are doing is upserts, `upsert` is tested to be 40% faster. And you don't have to put all of the rows to be upserted into a single huge array - you can stream them using `Upsert.stream`.
### Loosely based on mongo-ruby-driver's upsert functionality
The `selector` and `document` arguments are inspired by the upsert functionality of the [mongo-ruby-driver's update method](http://api.mongodb.org/ruby/1.6.4/Mongo/Collection.html#update-instance_method).
-
-## Wishlist
-
-1. `Pet.upsert`... duh
-2. Don't need a separate buffer class... just extend an instance of Upsert with the appropriate database driver module.
## Copyright
Copyright 2012 Brighter Planet, Inc.