Tsql Sybase ASE-性能选择到Vs插入选择

Tsql Sybase ASE-性能选择到Vs插入选择,tsql,database-performance,sap-ase,sql-tuning,Tsql,Database Performance,Sap Ase,Sql Tuning,我已经看到,select into的性能优于insert/select组合,因为它是未登录的。但今天我遇到了另一种情况。我正在从一个物理表向一个临时表执行select into操作,它在410秒内插入了2.4M记录,但当我将其拆分为select into语句以首先创建表,然后使用insert/select插入数据时,相同的查询在10.6秒内单击了2.4M记录 我很困惑,原因是什么 Query 1 ======= select a, b, c into #A from table1 where a

我已经看到,select into的性能优于insert/select组合,因为它是未登录的。但今天我遇到了另一种情况。我正在从一个物理表向一个临时表执行select into操作,它在410秒内插入了2.4M记录,但当我将其拆分为select into语句以首先创建表,然后使用insert/select插入数据时,相同的查询在10.6秒内单击了2.4M记录

我很困惑,原因是什么

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秒内插入2.4M记录

在Jean发表评论之后 现在它的行为很奇怪。如果我先执行查询1,然后执行查询2,那么结果如上所示。但是如果我切换顺序,查询1将在9秒内运行,查询2将在7秒内运行

Plans
问题1

问题2


数据可能在缓存中,第一次需要一些时间,第二次做类似的事情时速度会更快,因为它没有任何物理io。

你能同时提供两个查询计划吗?@Jean,问题中添加了计划。
-------
""
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.
-------
""
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.