BulkCopy in mssql-python - API Spec & Discussion
#414
Replies: 2 comments 1 reply
-
|
Re
I think we dont have a usecase for |
Beta Was this translation helpful? Give feedback.
-
|
I'd like to understand the reasoning behind hiding so much behind the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Discussion for Bulk Copy in
mssql-python(Issue #18)We’re adding a BulkCopy API (
cursor.bulkcopy()) tomssql-pythonfor fast, high-throughput inserts into SQL Server.Due to all the interest, this discussion thread is here for us to understand from you, about your usage scenarios - feel free to comment your suggestions and propose improvements!
If you’re using (or planning to use) bulk copy, feel free to comment with:
batch_size,table_locketc.)BulkCopy API Spec
The
cursor.bulkcopy()method provides bulk data loading to SQL Server, similar to ADO.NET’sSqlBulkCopy.It follows the standard DB-API cursor pattern and uses keyword arguments for configuration.
Method Signature
Full Signature with All Available Keyword Arguments
Parameters
Required Positional Parameters
table_name(str)Name of the destination table.
data_source(Iterator[Tuple])Iterator yielding tuples of row data.
Keyword Arguments
All parameters beyond
table_nameanddata_sourcemust be passed as keyword arguments.Performance Settings
batch_size(int, default:1000)Number of rows per batch.
1000–10000timeout_in_seconds(int, default:30)Operation timeout in seconds.
0for no timeoutColumn Mapping
column_mappings(List[Tuple], optional)Maps source columns to destination columns.
Format:
source: column name (str) or column index (int)destination: destination column name (str)Example:
Bulk Copy Options
keep_identity(bool, default:False)Preserve source identity values.
Equivalent to
SET IDENTITY_INSERT ON.check_constraints(bool, default:False)Enforces table constraints during bulk copy.
Disabled by default for performance.
table_lock(bool, default:False)Acquires a table-level lock to reduce locking overhead.
Recommended when exclusive access is available.
keep_nulls(bool, default:False)Preserves explicit
NULLvalues instead of applying column defaults.fire_triggers(bool, default:False)Fires
INSERTtriggers during bulk copy.Disabled by default for performance.
use_internal_transaction(bool, default:False)Wraps the bulk copy operation in an internal transaction.
Automatically rolls back on error.
Return Value
Returns a dictionary containing operation statistics:
{ 'rows_copied': 10000, # Number of rows successfully copied 'batch_count': 10, # Number of batches processed 'elapsed_time': 1.5, # Total elapsed time (seconds) 'rows_per_second': 6666 # Throughput }Looking forward to your feedback!
cc: @dlevy-msft-sql @saurabh500 @sumitmsft
Beta Was this translation helpful? Give feedback.
All reactions