项目作者: uzbekdev1

项目描述 :
Npgsql Bulk Copy
高级语言: C
项目地址: git://github.com/uzbekdev1/NpgsqlBulkCopy.git
创建时间: 2014-08-21T06:41:54Z
项目社区:https://github.com/uzbekdev1/NpgsqlBulkCopy

开源协议:Apache License 2.0

下载


NpgsqlBulkCopy

Npgsql Bulk Copy

I simple demostration:

Scripts:

  1. -- Database: northwind
  2. -- DROP DATABASE northwind;
  3. CREATE DATABASE northwind
  4. WITH OWNER = postgres
  5. ENCODING = 'UTF8'
  6. TABLESPACE = pg_default
  7. LC_COLLATE = 'English_United States.1252'
  8. LC_CTYPE = 'English_United States.1252'
  9. CONNECTION LIMIT = -1;
  10. -- Table: employees
  11. -- DROP TABLE employees;
  12. CREATE TABLE employees
  13. (
  14. "EmployeeID" smallint NOT NULL,
  15. "LastName" character varying(20) NOT NULL,
  16. "FirstName" character varying(10) NOT NULL,
  17. "Title" character varying(30),
  18. "TitleOfCourtesy" character varying(25),
  19. "BirthDate" date,
  20. "HireDate" date,
  21. "Address" character varying(60),
  22. "City" character varying(15),
  23. "Region" character varying(15),
  24. "PostalCode" character varying(10),
  25. "Country" character varying(15),
  26. "HomePhone" character varying(24),
  27. "Extension" character varying(4),
  28. "Photo" bytea,
  29. "Notes" text,
  30. "ReportsTo" smallint,
  31. "PhotoPath" character varying(255),
  32. CONSTRAINT pk_employees PRIMARY KEY ("EmployeeID")
  33. )
  34. WITH (
  35. OIDS=FALSE
  36. );
  37. ALTER TABLE employees
  38. OWNER TO postgres;

Config file:

BulkCopyConfig.xml

  1. <BulkCopy>
  2. <MajorVersion>9</MajorVersion>
  3. <Connection>
  4. <Host>127.0.0.1</Host>
  5. <Port>5432</Port>
  6. <UserName>northwind</UserName>
  7. <UserName>postgres</UserName>
  8. <Password>web@1234</Password>
  9. </Connection>
  10. </BulkCopy>

Source:

``` static unsafe void Main(string[] args)
{
var model = BulkCopyFactory.GetModel();
var columnData = String.Join(“,”, new[]
{
“EmployeeID”,
“LastName”,
“FirstName”,
“Title”,
“BirthDate”,
“Address”
}.Select(s => String.Format(“\”{0}\””, s)));
var tableName = “\”public\”.\”employees\””;
var batchSize = 10000;
var recordSize = 10000000;

  1. PQNativeApi.LoadDLLDirectory(model.MajorVersion);
  2. switch (model.MajorVersion)
  3. {
  4. case PgVersions.PG8x:
  5. {
  6. PQ8xNativeApi.openLocaleConn(model.Connection.Database.AsPointer(), model.Connection.UserName.AsPointer(), model.Connection.Password.AsPointer());
  7. PQ8xNativeApi.setColumns(columnData.AsPointer());
  8. PQ8xNativeApi.setTableName(tableName.AsPointer());
  9. PQ8xNativeApi.setBatchSize(batchSize);
  10. for (var i = 0; i < recordSize; i++)
  11. {
  12. var row = String.Join(",", new object[]
  13. {
  14. i + 1,
  15. "'Elyor'",
  16. "'Laipov'",
  17. "'Software Developer'",
  18. "now()",
  19. "'Uzbekistan,Bukhara,Shofirkan'"
  20. });
  21. PQ8xNativeApi.addRow(row.AsPointer());
  22. }
  23. PQ8xNativeApi.runBulkCopy();
  24. PQ8xNativeApi.closeConn();
  25. PQ8xNativeApi.cleanUp();
  26. }
  27. break;
  28. case PgVersions.PQ9x:
  29. {
  30. PQ9xNativeApi.openLocaleConn(model.Connection.Database.AsPointer(), model.Connection.UserName.AsPointer(), model.Connection.Password.AsPointer());
  31. PQ9xNativeApi.setColumns(columnData.AsPointer());
  32. PQ9xNativeApi.setTableName(tableName.AsPointer());
  33. PQ9xNativeApi.setBatchSize(batchSize);
  34. for (var i = 0; i < recordSize; i++)
  35. {
  36. var row = String.Join(",", new object[]
  37. {
  38. i + 1,
  39. "'Elyor'",
  40. "'Laipov'",
  41. "'Software Developer'",
  42. "now()",
  43. "'Uzbekistan,Bukhara,Shofirkan'"
  44. });
  45. PQ9xNativeApi.addRow(row.AsPointer());
  46. }
  47. PQ9xNativeApi.runBulkCopy();
  48. PQ9xNativeApi.closeConn();
  49. PQ9xNativeApi.cleanUp();
  50. }
  51. break;
  52. }
  53. Console.WriteLine("Bulk copy successfully");
  54. Console.ReadKey();
  55. }
  56. ```

Performance:

Batch size:10.000 , Rows -> 10.000.000 records to load time ~5.8 min
Batch size: 100.000 ,Rows-> 10.000.000 records to load time ~5.9 min