BulkDeleteByKey
This method deletes rows from the database using a list of primary keys in bulk. It is supported for MySqlConnector.
This page documents the MySqlConnector-specific arguments and examples. For the SQL Server implementation, see BulkDeleteByKey (SQL Server); for Oracle, see BulkDeleteByKey (Oracle).
Call Flow Diagram
The diagram below shows the flow when calling this operation.
flowchart TD
Client["Client<br/>(RepoDB)"] -->|BulkDeleteByKey| Keys["Primary Keys<br/>IEnumerable<TPrimaryKey>"]
Keys --> Pseudo["Create Pseudo Table<br/>(Physical, key column only)"]
Pseudo --> BulkCopy["MySqlBulkCopy"]
BulkCopy -->|Write| PseudoTable[("Pseudo Table<br/>(key column only)")]
PseudoTable -->|"DELETE ... INNER JOIN<br/>ON key"| Table[("Target Table")]
PseudoTable -->|Drop| Cleanup(["Pseudo Table Dropped"])
Use Case
Use this method to delete rows by primary key at high speed. It leverages the native bulk operation from MySqlConnector via the MySqlBulkCopy class.
Prefer this method over BulkDelete when you only have the primary keys of the rows to delete (not the full entities). The pseudo table used internally only ever stages the one qualifier column, making this the lightest of the bulk operations.
Special Arguments
The bulkCopyTimeout, batchSize and pseudoTableType arguments are available for this operation.
bulkCopyTimeout overrides the command timeout, in seconds.
batchSize overrides the number of rows sent to the server per batch. When not set, all items are sent at once.
pseudoTableType (via MySqlConnectorBulkImportPseudoTableType) controls the kind of staging table used internally.
Every
pseudoTableTypevalue currently resolves toPhysicalat runtime — see Operations (MySqlConnector) for details.
Caveats
This operation creates a pseudo-temporary table internally under a transaction context. The database user must have permission to create tables, or a MySqlException will be thrown.
Usability
Pass the target table name and the list of primary keys to the operation.
using (var connection = new MySqlConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = connection.BulkDeleteByKey("Person", primaryKeys);
}
It returns the number of rows deleted from the underlying table.
To specify a batch size:
using (var connection = new MySqlConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = connection.BulkDeleteByKey("Person",
primaryKeys,
batchSize: 100);
}
If
batchSizeis not set, all items in the collection are sent at once.
DataReader
using (var sourceConnection = new MySqlConnection(sourceConnectionString))
{
using (var reader = sourceConnection.ExecuteReader("SELECT Id FROM Person WHERE (IsActive = 0)"))
{
var primaryKeys = new List<int>();
while (reader.Read())
{
primaryKeys.Add(reader.GetInt32(0));
}
using (var destinationConnection = new MySqlConnection(destinationConnectionString))
{
var deletedRows = destinationConnection.BulkDeleteByKey("Person", primaryKeys);
}
}
}
Async Method
An equivalent BulkDeleteByKeyAsync method is also available.
using (var connection = new MySqlConnection(connectionString))
{
var primaryKeys = connection.Query<Person>(p => p.IsActive == false).Select(p => p.Id);
var deletedRows = await connection.BulkDeleteByKeyAsync("Person", primaryKeys);
}