捕获批量插入操作的事件信息(BCP.EXE
, SqlBulkCopy
,我假设BULK INSERT
, and OPENROWSET(BULK...
)是可能的,但您将无法看到单独的行和列。
批量插入操作显示为单个(每个批次一个,默认情况下是在单个批次中执行所有行)DML 语句:
INSERT BULK <destination_table_name> (
<column1_name> <column1_datatype> [ COLLATE <column1_collation> ], ...
) [ WITH (<1 or more hints>) ]
<hints> := KEEP_NULLS, TABLOCK, ORDER(...), ROWS_PER_BATCH=, etc
You can find the full list of "hints" on the MSDN page for the BCP Utility https://msdn.microsoft.com/en-us/library/ms162802.aspx. Please note that SqlBulkCopy only supports a subset of those hints (e.g. KEEP_NULLS
, TABLOCK
, and a few others) but does not support ORDER(...)
or ROWS_PER_BATCH=
** (which is quite unfortunate, actually, as the ORDER()
hint is needed in order to avoid a sort that happens in tempdb in order to allow the operation to be minimally logged (assuming the other conditions for such an operation have also been satisfied).
为了查看此语句,您需要在 SQL Server Profiler 中捕获以下任何事件:
SQL:批量启动
SQL:批量完成
SQL:Stmt启动
SQL:StmtCompleted
您还需要至少选择以下列(在 SQL Server Profiler 中):
TextData
CPU
Reads
写
期间
SPID
开始时间
时间结束
行数
并且,由于用户无法提交INSERT BULK
直接声明,如果您只想查看这些事件而不想查看其他内容,则可以在列过滤器中对其进行过滤。
如果您想查看官方通知BULK INSERT
操作正在开始和/或结束,那么您需要捕获以下事件:
SQL事务
然后添加以下 Profiler 列:
事件子类
对象名
For ObjectName
您将始终收到显示“BULK INSERT”的事件,并且该事件是开始还是结束由中的值决定EventSubClass
,它是“0 - 开始”或“1 - 提交”(我想如果失败,您应该看到“2 - 回滚”)。
If the ORDER()
未指定提示(同样,它cannot使用时需指定SqlBulkCopy
),那么你还会得到一个“SQLTransaction”事件,在ObjectName
柱子。该事件还有“0 - Begin”和“1 - Commit”事件(如EventSubClass
柱子)。
最后,即使您看不到特定行,如果您捕获以下事件,您仍然可以看到针对事务日志的操作(例如插入行、修改 IAM 行、修改 PFS 行等):
交易日志
并添加以下 Profiler 列:
ObjectID
感兴趣的主要信息将在EventSubClass
列,但不幸的是它只是 ID 值,我在 MSDN 文档中找不到这些值的任何翻译。然而,我确实找到了 Jonathan Kehayias 的以下博客文章:使用 SQL Server Denali CTP1 中的扩展事件来映射 TransactionLog SQL 跟踪事件 EventSubClass 值 http://sqlblog.com/blogs/jonathan_kehayias/archive/2011/01/03/using-extended-events-in-sql-server-denali-ctp1-to-map-out-the-transactionlog-sql-trace-event-eventsubclass-values.aspx.
@RBarryYoung 指出 EventSubClass 值和名称可以在sys.trace_subclass_values
目录视图,但查询该视图显示它没有行TransactionLog
event:
SELECT * FROM sys.trace_categories -- 12 = Transactions
SELECT * FROM sys.trace_events WHERE category_id = 12 -- 54 = TransactionLog
SELECT * FROM sys.trace_subclass_values WHERE trace_event_id = 54 -- nothing :(
** Please note that the SqlBulkCopy.BatchSize
property is equivalent to setting the -b
option for BCP.EXE, which is an operational setting that controls how each command will break up the rows into sets. This is not the same as the ROWS_PER_BATCH=
hint which does not physically control how the rows are broken up into sets, but instead allows SQL Server to better plan how it will allocate pages, and hence reduces the number of entries in the Transaction Log (sometimes by quite a bit). Still my testing showed that:
- 指定
-b
for BCP.EXE确实设置了ROWS_PER_BATCH=
暗示相同的值。
- 指定
SqlBulkCopy.BatchSize
财产做到了not设置ROWS_PER_BATCH=
提示,但是,减少事务日志活动的好处在某种程度上肯定是存在的(神奇吗?)。事实上,净效应仍然是获得利益,这就是为什么当我说不幸的是,我没有在顶部提到这一点。ORDER()
提示不受支持SqlBulkCopy
.