Giter VIP home page Giter VIP logo

bulk_insert.cr's Introduction

bulk_insert

Helper class to perform batched INSERTs on a SQL database.

When inserting many of rows at a time, including values for multiple rows in a single INSERT is faster than executing an INSERT for every row.

This class takes care of that for you, including preparing the necessary statements and executing them for any number of rows.

Installation

Add this to your application's shard.yml:

dependencies:
  bulk_insert:
    github: hinrik/bulk_insert.cr

Usage

require "db"
require "sqlite3"
require "bulk_insert"

DB.connect "sqlite3::memory:" do |conn|
  conn.exec "CREATE TABLE mytable (name string, count integer)"

  sql_prefix = "INSERT INTO mytable (name, count) VALUES"
  bulk = BulkInsert.new(2, sql_prefix)

  # fast bulk importing
  data = [["foo", 5], ["bar", 3], ["baz", 9]]
  conn.transaction do |tx|
    bulk.exec_many(conn, data) do |nr_rows, result|
      puts "Processed #{nr_rows} rows"
    end

    # blockless form
    bulk.exec_many(conn, data)
  end

  # also supports single-row insert
  result = bulk.exec conn, ["bla", 123]
end

How it works

Internally, each object prepares floor(log2(n)) statements where n is the greatest number <= max_args (which is 999 by default) divisible by the number of columns. This means that for a 2-column INSERT, 9 statements are prepared:

arguments rows
998 499
498 249
248 124
... ...
6 3
2 1

They are then tried in order, executing your batched insert using the fewest statements possible.

Contributing

  1. Fork it (https://github.com/hinrik/bulk_insert.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

bulk_insert.cr's People

Contributors

hinrik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.