我已经看到 select into 的性能优于 insert/select 组合,因为它没有记录。但是今天我遇到了不同的情况。我正在从物理表中选择进入临时表,它在 410 秒内插入 240 万条记录,但是当我将其拆分为选择进入语句以首先创建表,然后使用插入/选择插入数据时同样的查询在 10.6 秒内点击了 240 万条记录。
我很困惑,可能是什么原因?
Query 1
=======
select a, b, c
into #A
from table1
where a = 'XYZ'
and c = 'ABC'
410 秒内插入 240 万条记录
Query 2
=======
select a, b, c
into #A
from table1
where 1=2
insert #A
select a, b, c
from table1
where a = 'XYZ'
and c = 'ABC'
10.6 秒内插入 240 万条记录
珍发表评论后
现在它的表现很奇怪。如果我先执行查询1,然后执行查询2,那么结果如上。但是如果我切换顺序,查询 1 会在 ~9 秒内运行,而查询 2 会在 ~7 秒内运行。
Plans
查询 1
-------
""
QUERY PLAN FOR STATEMENT 1 (at line 1).
""
""
STEP 1
The type of query is DECLARE.
""
""
QUERY PLAN FOR STATEMENT 2 (at line 2).
""
""
STEP 1
The type of query is SELECT.
""
1 operator(s) under root
""
|ROOT:EMIT Operator (VA = 1)
|
| |SCALAR Operator (VA = 0)
""
""
""
QUERY PLAN FOR STATEMENT 3 (at line 3).
""
""
STEP 1
The type of query is CREATE TABLE.
""
STEP 2
The type of query is INSERT.
""
5 operator(s) under root
""
|ROOT:EMIT Operator (VA = 5)
|
| |INSERT Operator (VA = 4)
| | The update mode is direct.
| |
| | |NESTED LOOP JOIN Operator (VA = 3) (Join Type: Inner Join)
| | |
| | | |SCAN Operator (VA = 0)
| | | | FROM OR List
| | | | OR List has up to 3 rows of OR/IN values.
| | |
| | | |RESTRICT Operator (VA = 2)(0)(0)(0)(10)(0)
| | | |
| | | | |SCAN Operator (VA = 1)
| | | | | FROM TABLE
| | | | | ABCD.dbo.ITR
| | | | | a
| | | | | Using Clustered Index.
| | | | | Index : ITR_typ_imnt_cl_optn
| | | | | Forward Scan.
| | | | | Positioning by key.
| | | | | Keys are:
| | | | | id_typ_imnt ASC
| | | | | Using I/O Size 16 Kbytes for data pages.
| | | | | With LRU Buffer Replacement Strategy for data pages.
| |
| | TO TABLE
| | #A
| | Using I/O Size 16 Kbytes for data pages.
""
""
""
QUERY PLAN FOR STATEMENT 4 (at line 9).
""
""
STEP 1
The type of query is SELECT.
""
1 operator(s) under root
""
|ROOT:EMIT Operator (VA = 1)
|
| |SCALAR Operator (VA = 0)
""
""
Parse and Compile Time 0.
Adaptive Server cpu time: 0 ms.
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.
Table: #A scan count 0, logical reads: (regular=2452707 apf=0 total=2452707), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: ABCD.dbo.ITR (a) scan count 3, logical reads: (regular=581980 apf=0 total=581980), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Execution Time 75.
Adaptive Server cpu time: 7500 ms. Adaptive Server elapsed time: 7546 ms.
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.
查询 2
-------
""
QUERY PLAN FOR STATEMENT 1 (at line 1).
""
""
STEP 1
The type of query is DECLARE.
""
""
QUERY PLAN FOR STATEMENT 2 (at line 2).
""
""
STEP 1
The type of query is SELECT.
""
1 operator(s) under root
""
|ROOT:EMIT Operator (VA = 1)
|
| |SCALAR Operator (VA = 0)
""
""
""
QUERY PLAN FOR STATEMENT 3 (at line 3).
""
""
STEP 1
The type of query is CREATE TABLE.
""
STEP 2
The type of query is INSERT.
""
3 operator(s) under root
""
|ROOT:EMIT Operator (VA = 3)
|
| |INSERT Operator (VA = 2)
| | The update mode is direct.
| |
| | |RESTRICT Operator (VA = 1)(4)(0)(0)(0)(0)
| | |
| | | |SCAN Operator (VA = 0)
| | | | FROM TABLE
| | | | CERD_CORPORATE..INSTRUMENT
| | | | Table Scan.
| | | | Forward Scan.
| | | | Positioning at start of table.
| | | | Using I/O Size 2 Kbytes for data pages.
| | | | With LRU Buffer Replacement Strategy for data pages.
| |
| | TO TABLE
| | #A
| | Using I/O Size 16 Kbytes for data pages.
""
""
""
QUERY PLAN FOR STATEMENT 4 (at line 5).
""
""
STEP 1
The type of query is INSERT.
""
5 operator(s) under root
""
|ROOT:EMIT Operator (VA = 5)
|
| |INSERT Operator (VA = 4)
| | The update mode is direct.
| |
| | |NESTED LOOP JOIN Operator (VA = 3) (Join Type: Inner Join)
| | |
| | | |SCAN Operator (VA = 0)
| | | | FROM OR List
| | | | OR List has up to 3 rows of OR/IN values.
| | |
| | | |RESTRICT Operator (VA = 2)(0)(0)(0)(10)(0)
| | | |
| | | | |SCAN Operator (VA = 1)
| | | | | FROM TABLE
| | | | | CERD_CORPORATE.dbo.INSTRUMENT
| | | | | a
| | | | | Using Clustered Index.
| | | | | Index : INSTRUMENT_typ_imnt_cl_optn
| | | | | Forward Scan.
| | | | | Positioning by key.
| | | | | Keys are:
| | | | | id_typ_imnt ASC
| | | | | Using I/O Size 16 Kbytes for data pages.
| | | | | With LRU Buffer Replacement Strategy for data pages.
| |
| | TO TABLE
| | #A
| | Using I/O Size 2 Kbytes for data pages.
""
""
""
QUERY PLAN FOR STATEMENT 5 (at line 11).
""
""
STEP 1
The type of query is SELECT.
""
1 operator(s) under root
""
|ROOT:EMIT Operator (VA = 1)
|
| |SCALAR Operator (VA = 0)
""
""
Parse and Compile Time 0.
Adaptive Server cpu time: 0 ms.
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.
Table: #A scan count 0, logical reads: (regular=1 apf=0 total=1), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: CERD_CORPORATE..INSTRUMENT scan count 0, logical reads: (regular=0 apf=0 total=0), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.
Table: #A scan count 0, logical reads: (regular=2493221 apf=0 total=2493221), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: CERD_CORPORATE.dbo.INSTRUMENT (a) scan count 3, logical reads: (regular=581980 apf=0 total=581980), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Execution Time 96.
Adaptive Server cpu time: 9600 ms. Adaptive Server elapsed time: 9580 ms.
Adaptive Server cpu time: 0 ms. Adaptive Server elapsed time: 0 ms.