INSERT SELECT没有最小化记录

时间:2017-04-30 22:56:42

标签: sql-server tsql

以下查询记录最少:

INSERT [HIST_DB].[DBO].[HIST_TABLE] WITH (TABLOCK)
SELECT [Hash] FROM [DSA_DB].[DBO].[DSA_TABLE] SRC

锁定(sp_whoisactive):

<Database name="DSA_DB">
  <Locks>
    <Lock request_mode="S" request_status="GRANT" request_count="1" />
  </Locks>
  <Objects>
    <Object name="DSA_TABLE" schema_name="dbo">
      <Locks>
        <Lock resource_type="OBJECT" request_mode="IS" request_status="GRANT" request_count="9" />
        <Lock resource_type="PAGE" page_type="*" index_name="PK_DSA_TABLE" request_mode="S" request_status="GRANT" request_count="5" />
      </Locks>
    </Object>
  </Objects>
</Database>
<Database name="HIST_DB">
  <Locks>
    <Lock request_mode="S" request_status="GRANT" request_count="1" />
  </Locks>
  <Objects>
    <Object name="(null)">
      <Locks>
        <Lock resource_type="DATABASE.BULKOP_BACKUP_DB" request_mode="NULL" request_status="GRANT" request_count="9" />
        <Lock resource_type="DATABASE.BULKOP_BACKUP_LOG" request_mode="NULL" request_status="GRANT" request_count="9" />
        <Lock resource_type="DATABASE.ENCRYPTION_SCAN" request_mode="S" request_status="GRANT" request_count="9" />
        <Lock resource_type="PAGE" page_type="*" request_mode="X" request_status="GRANT" request_count="1" />
      </Locks>
    </Object>
    <Object name="HIST_TABLE" schema_name="dbo">
      <Locks>
        <Lock resource_type="ALLOCATION_UNIT.BULK_OPERATION_PAGE" request_mode="S" request_status="GRANT" request_count="9" />
        <Lock resource_type="HOBT.BULK_OPERATION" request_mode="IX" request_status="GRANT" request_count="9" />
        <Lock resource_type="OBJECT" request_mode="BU" request_status="GRANT" request_count="8" />
        <Lock resource_type="OBJECT" request_mode="X" request_status="GRANT" request_count="1" />
      </Locks>
    </Object>
  </Objects>
</Database>

以下查询最低限度记录:

INSERT [HIST_DB].[DBO].[HIST_TABLE] WITH (TABLOCK)
SELECT [Hash] FROM [DSA_DB].[DBO].[DSA_TABLE] SRC
WHERE NOT EXISTS (
    SELECT [Hash] FROM [HIST_DB].[DBO].[HIST_TABLE] TGT WITH
    WHERE TGT.[Hash] = SRC.[Hash]
)

锁定(sp_whoisactive):

<Database name="DSA_DB">
  <Locks>
    <Lock request_mode="S" request_status="GRANT" request_count="1" />
  </Locks>
  <Objects>
    <Object name="DSA_TABLE" schema_name="dbo">
      <Locks>
        <Lock resource_type="OBJECT" request_mode="IS" request_status="GRANT" request_count="1" />
        <Lock resource_type="PAGE" page_type="*" index_name="PK_DSA_TABLE" request_mode="S" request_status="GRANT" request_count="1" />
      </Locks>
    </Object>
  </Objects>
</Database>
<Database name="HIST_DB">
  <Locks>
    <Lock request_mode="S" request_status="GRANT" request_count="1" />
  </Locks>
  <Objects>
    <Object name="(null)">
      <Locks>
        <Lock resource_type="DATABASE.BULKOP_BACKUP_DB" request_mode="NULL" request_status="GRANT" request_count="1" />
        <Lock resource_type="DATABASE.BULKOP_BACKUP_LOG" request_mode="NULL" request_status="GRANT" request_count="1" />
      </Locks>
    </Object>
    <Object name="HIST_TABLE" schema_name="dbo">
      <Locks>
        <Lock resource_type="OBJECT" request_mode="X" request_status="GRANT" request_count="1" />
      </Locks>
    </Object>
  </Objects>
</Database>

为什么第二个查询没有最小化记录?

0 个答案:

没有答案