我有两张表如下:
ParentUpdate
name varchar(255)
value int not null
主键:名称
ChildUpdate
parentName varchar(255)
name varchar(255)
value int
data varchar(1000)
主键:名称 外键:parentName到ParentUpdate.name
当我运行语句"更新ChildUpdate set parentName =' Parent 2'其中parentName ='父母1'"在ChildUpdate表中有2500条记录,在ParentUpdate表中有1条记录,在ChildUpdate表中数据大小只有一个字节差异,性能下降了15倍。
当ChildUpdate数据列恰好具有相同字符的14个字节时,上述查询的运行时间约为500毫秒。当我向ChildUpdate的数据列添加一个字节时,性能突然变为大约7500毫秒。
如果我然后将数据大小从15减少到14,它再次快速。当我把它放回到15时它又慢了。这是每次都可以重复的。
请你帮我弄清楚如何在没有看似随意的行为的情况下获得相同的快速性能。
两种情况下的查询计划都在下面。
Statement Name:
null
Statement Text:
update ChildUpdate set parentName = 'Parent 2' where parentName = 'Parent 1'
Parse Time: 16
Bind Time: 0
Optimize Time: 0
Generate Time: 0
Compile Time: 16
Execute Time: -1453199411351
Begin Compilation Timestamp : 2016-01-19 05:30:11.32
End Compilation Timestamp : 2016-01-19 05:30:11.336
Begin Execution Timestamp : 2016-01-19 05:30:11.351
End Execution Timestamp : 2016-01-19 05:30:11.773
Statement Execution Plan Text:
Update ResultSet using row locking:
deferred: false
Rows updated = 2500
Indexes updated = 2
Execute Time = -1453199411383
Normalize ResultSet:
Number of opens = 1
Rows seen = 2500
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 16
close time (milliseconds) = 16
optimizer estimated row count: 51.50
optimizer estimated cost: 796.12
Source result set:
Project-Restrict ResultSet (2):
Number of opens = 1
Rows seen = 2500
Rows filtered = 0
restriction = false
projection = true
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 16
close time (milliseconds) = 16
restriction time (milliseconds) = 0
projection time (milliseconds) = 0
optimizer estimated row count: 51.50
optimizer estimated cost: 796.12
Source result set:
Table Scan ResultSet for CHILDUPDATE at read committed isolation level using exclusive row locking chosen by the optimizer
Number of opens = 1
Rows seen = 2500
Rows filtered = 0
Fetch Size = 1
constructor time (milliseconds) = 0
open time (milliseconds) = 15
next time (milliseconds) = 16
close time (milliseconds) = 16
next time in milliseconds/row = 0
scan information:
Bit set of columns fetched={0, 1}
Number of columns fetched=2
Number of pages visited=41
Number of rows qualified=2500
Number of rows visited=2500
Scan type=heap
start position:
null
stop position:
null
qualifiers:
Column[0][0] Id: 0
Operator: =
Ordered nulls: false
Unknown return value: false
Negate comparison result: false
optimizer estimated row count: 51.50
optimizer estimated cost: 796.12
total time: ~500 milliseconds
和慢版
Statement Name:
null
Statement Text:
update ChildUpdate set parentName = 'Parent 2' where parentName = 'Parent 1'
Parse Time: 0
Bind Time: 0
Optimize Time: 0
Generate Time: 0
Compile Time: 0
Execute Time: -1453199485700
Begin Compilation Timestamp : 2016-01-19 05:31:25.684
End Compilation Timestamp : 2016-01-19 05:31:25.684
Begin Execution Timestamp : 2016-01-19 05:31:25.7
End Execution Timestamp : 2016-01-19 05:31:33.141
Statement Execution Plan Text:
Update ResultSet using row locking:
deferred: true
Rows updated = 2500
Indexes updated = 2
Execute Time = -1453199485747
Normalize ResultSet:
Number of opens = 1
Rows seen = 2500
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 47
close time (milliseconds) = 0
optimizer estimated row count: 51.50
optimizer estimated cost: 810.94
Source result set:
Project-Restrict ResultSet (3):
Number of opens = 1
Rows seen = 2500
Rows filtered = 0
restriction = false
projection = true
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 32
close time (milliseconds) = 0
restriction time (milliseconds) = 0
projection time (milliseconds) = 0
optimizer estimated row count: 51.50
optimizer estimated cost: 810.94
Source result set:
Project-Restrict ResultSet (2):
Number of opens = 1
Rows seen = 2500
Rows filtered = 0
restriction = false
projection = true
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 32
close time (milliseconds) = 0
restriction time (milliseconds) = 0
projection time (milliseconds) = 0
optimizer estimated row count: 51.50
optimizer estimated cost: 810.94
Source result set:
Index Scan ResultSet for CHILDUPDATE using index TESTINDEX at read committed isolation level using exclusive row locking chosen by the optimizer
Number of opens = 1
Rows seen = 2500
Rows filtered = 0
Fetch Size = 1
constructor time (milliseconds) = 0
open time (milliseconds) = 0
next time (milliseconds) = 32
close time (milliseconds) = 0
next time in milliseconds/row = 0
scan information:
Bit set of columns fetched={0, 1, 2}
Number of columns fetched=3
Number of deleted rows visited=0
Number of pages visited=42
Number of rows qualified=2500
Number of rows visited=2500
Scan type=btree
Tree height=2
start position:
None
stop position:
None
qualifiers:
Column[0][0] Id: 1
Operator: =
Ordered nulls: false
Unknown return value: false
Negate comparison result: false
optimizer estimated row count: 51.50
optimizer estimated cost: 810.94
total time: ~7 seconds 500 milliseconds