-
Notifications
You must be signed in to change notification settings - Fork 0
IMPRESS
smith11235 edited this page Nov 2, 2013
·
4 revisions
A Bulk Data Loading Gem For Rails and Active Record
- Purpose
- Usage
- How It Works
Any Rails/ActiveRecord lovers with a need to efficiently ingest a lot of data
<script src="https://gist.github.com/smith11235/7281452.js"></script> <script src="https://gist.github.com/smith11235/7281410.js"></script>Each call to save or create methods cause a transaction with the database.
Each trasaction has opening and closing costs.
When commiting many model instances to a database transaction costs become non-trivial.
Deep Import can improve a load
- of 27,930 model instances
- from 90 minutes to 90 seconds
- by eliminating transaction costs
- is this the path of least resistance?
- aren't other tools built for bulk data?
- are super easy to use
- provide helper logic like validations
- are database and developer agnostic
- fast to prototype and deliver
- why would you slow down by switching to non-rails
When invoked
- enhances the active record api on the Model classes
- track all models instantiated and associations
- executes the user provided block of code
- commits all instantiated models to the database
- within a single transaction
[gist](example deep import load of yaml file)
- added DeepImport.import call
- removed save calls [vimdiff image](of code difference) app/assets/images
-
27,930 models
- to a remote mysql instance
- across 3 associated model classes
- 30 Parents
- 900 Children
- 27,000 Grand Children
Standard 90 minutes Deep Import 90 seconds
- tells Deep Import which models to track
- tells Deep Import which belongs_to associations to track
[gist]
- generates a migration based on config file
- executes migration to prepare database for deep import
- if a config file exists and setup has been run
- just pass it a block that creates models
- a couple of options exist to tweak how active record is modified
- generally to allow non import code to run as import code
- makes development and debugging against subsets easy
- :on_save => :noop - allows code with 'save' to be executed
- link a gist
- extra model instances are used to support the bulk import
- for each ModelClass in the config file
- setup creates a DeepImportModelClass
- any time a ModelClass is instantiated
- deep import creates a DeepImportModelClass
- whenever a belongs_to association method is called
- the related DeepImportModelClass instance is updated
- basically a shadow index of record associations
- after load of models to the database
- associations on source models are set from deep import models
- deep import models are then deleted
Each model created with: Model.new.save
Causes 1 transaction with the database.
- 1 instance per transation
- Parent.new.save
- @parent.children.create
- X instances per transaction for 1 model class
- supports flat record formats
- doesnt support 'nested data' well
- helps power Deep Import, so much Thanks
Load X instances of M model classes
- where B is the number of belongs to relationships between the M classes
- in M + B database statements M and B must be neglible compared to X for benefit