Use binary COPY table FROM with psycopg2

Here is the binary equivalent of COPY FROM for Python 3: from io import BytesIO from struct import pack import psycopg2 # Two rows of data; “id” is not in the upstream data source # Columns: node, ts, val1, val2 data = [(23253, 342, -15.336734, 2494627.949375), (23256, 348, 43.23524, 2494827.949375)] conn = psycopg2.connect(“dbname=mydb user=postgres”) curs … Read more

Performance of bcp/BULK INSERT vs. Table-Valued Parameters

I don’t really have experience with TVP yet, however there is an nice performance comparison chart vs. BULK INSERT in MSDN here. They say that BULK INSERT has higher startup cost, but is faster thereafter. In a remote client scenario they draw the line at around 1000 rows (for “simple” server logic). Judging from their … Read more

How to insert multiple documents at once in MongoDB through Java

DBCollection.insert accepts a parameter of type DBObject, List<DBObject> or an array of DBObjects for inserting multiple documents at once. You are passing in a string array. You must manually populate documents(DBObjects), insert them to a List<DBObject> or an array of DBObjects and eventually insert them. DBObject document1 = new BasicDBObject(); document1.put(“name”, “Kiran”); document1.put(“age”, 20); DBObject … Read more

Bulk insert using stored procedure

There’s nothing wrong with your stored procedure code – the point is: the BULK INSERT command cannot accept a file name as a variable. This does work: BULK INSERT ZIPCodes FROM ‘e:\5-digit Commercial.csv’ WITH but this never works – within a stored proc or not: DECLARE @filename VARCHAR(255) SET @filename=”e:\5-digit Commercial.csv” BULK INSERT ZIPCodes FROM … Read more

Bulk Insert records into Active Record table

Use the activerecord-import gem. Let us say you are reading a CSV file and generating a Product catalogue and you want to insert records in batches of 1000: batch,batch_size = [], 1_000 CSV.foreach(“/data/new_products.csv”, :headers => true) do |row| batch << Product.new(row) if batch.size >= batch_size Product.import batch batch = [] end end Product.import batch

Bulk Insertion on Android device

Normally, each time db.insert() is used, SQLite creates a transaction (and resulting journal file in the filesystem), which slows things down. If you use db.beginTransaction() and db.endTransaction() SQLite creates only a single journal file on the filesystem and then commits all the inserts at the same time, dramatically speeding things up. Here is some pseudo … Read more

BULK INSERT with identity (auto-increment) column

Add an id column to the csv file and leave it blank: id,Name,Address ,name1,addr test 1 ,name2,addr test 2 Remove KEEPIDENTITY keyword from query: BULK INSERT Employee FROM ‘path\tempFile.csv ‘ WITH (FIRSTROW = 2,FIELDTERMINATOR = ‘,’ , ROWTERMINATOR = ‘\n’); The id identity field will be auto-incremented. If you assign values to the id field … Read more