SQLServer 不执行的条件分支却消耗时间!求解…

来源:互联网 发布:显示器淘宝 编辑:程序博客网 时间:2024/06/06 04:24

测试脚本:

--  创建测试库--  drop database TestDBuse mastergocreate database TestDBgo--  创建证书--  drop certificate  Mycertificate;use TestDBgocreate certificate Mycertificate  encryption by password = N'Hello@Mycertificate'with subject = N'EnryptData certificate',  start_date = N'20150101',  expiry_date = N'20160101';  go  --  创建以证书加密的对称密钥--  drop symmetric key MySymmetric;use TestDBgocreate symmetric key MySymmetricwith       algorithm=aes_128       encryption by certificate Mycertificatego  --  测试加密和解密--  close symmetric key MySymmetric; use TestDBgoopen symmetric key MySymmetricdecryption by certificate Mycertificate with password = N'Hello@Mycertificate';goselect encryptbykey(key_guid('MySymmetric'),cast('123456' as varchar(20)))goselect convert(varchar(20),decryptbykeyautocert(cert_id('Mycertificate'),N'Hello@Mycertificate',0x001E60848B02184E9106B2BDF6F612470100000023BE0228F35192CC39EE810A0B6D31B4EC12F68EAFC2DA8FB4F6C688F869D7EF))go--  创建分表--  drop table objects_Part1,objects_Part2use TestDBgoselect object_id,name,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_dateinto objects_Part1from sys.objectsgoselect object_id,is_ms_shipped,is_published,is_schema_publishedinto objects_Part2from sys.objects where object_id<>object_id('objects_Part1')gocreate clustered index ix_objects_Part1 on [dbo].[objects_Part1]([object_id])gocreate clustered index ix_objects_Part2 on [dbo].[objects_Part2]([object_id])go--  创建联合视图--  drop view objectsAlluse TestDBgocreate view [dbo].[VobjectsAll]  as select t1.object_id,t1.name,t1.principal_id,t1.schema_id,t1.parent_object_id,t1.type,t1.type_desc,t1.create_date,t1.modify_date/*,t2.object_id*/,t2.is_ms_shipped,t2.is_published,t2.is_schema_publishedfrom [dbo].[objects_Part1] t1 left join [dbo].[objects_Part2] t2 on t1.object_id=t2.object_idgo--  创建更新触发器--  drop trigger [dbo].[tgr_objectsall_update] use TestDBgocreate trigger [dbo].[tgr_objectsall_update]  on [dbo].[VobjectsAll]   instead of update  asbegin declare @COLUMNS_UPDATED varbinary(100) declare @IsUpdateTab01 int = 0 declare @IsUpdateTab02 int = 0 declare @T1 int declare @T2 int set @T1 = 65281   set @T2 = 254 /*表1: 11111111 00000001*/ /*表2: 00000000 11111110*/ set @COLUMNS_UPDATED = COLUMNS_UPDATED() set @IsUpdateTab01 = SUBSTRING(@COLUMNS_UPDATED,1,2) & @T1 set @IsUpdateTab02 = SUBSTRING(@COLUMNS_UPDATED,2,1) & @T2 if ( @IsUpdateTab01 > 0 AND @IsUpdateTab02 = 0 ) begin    print 'update [objects_Part1]'    update t1 set      t1.name = t2.name    ,t1.principal_id = t2.principal_id    ,t1.schema_id = t2.schema_id    ,t1.parent_object_id = t2.parent_object_id    ,t1.type = t2.type    ,t1.type_desc = t2.type_desc    ,t1.create_date = t2.create_date    ,t1.modify_date = t2.modify_date    from [dbo].[objects_Part1] t1,inserted t2 where t1.object_id = t2.object_id end else if ( @IsUpdateTab01 = 0 AND @IsUpdateTab02 > 0) begin    print 'update [objects_Part2]'    open symmetric key MySymmetric    decryption by certificate Mycertificate with password = N'Hello@Mycertificate';        update t1 set      t1.is_ms_shipped = t2.is_ms_shipped    ,t1.is_published = t2.is_published    ,t1.is_schema_published = t2.is_schema_published    from [dbo].[objects_Part2] t1,inserted t2 where t1.object_id = t2.object_id end else begin    print 'update [objects_Part1] and [objects_Part2]'    update t1 set      t1.name = t2.name    ,t1.principal_id = t2.principal_id    ,t1.schema_id = t2.schema_id    ,t1.parent_object_id = t2.parent_object_id    ,t1.type = t2.type    ,t1.type_desc = t2.type_desc    ,t1.create_date = t2.create_date    ,t1.modify_date = t2.modify_date    from [dbo].[objects_Part1] t1,inserted t2 where t1.object_id = t2.object_id        update t1 set      t1.is_ms_shipped = t2.is_ms_shipped    ,t1.is_published = t2.is_published    ,t1.is_schema_published = t2.is_schema_published    from [dbo].[objects_Part2] t1,inserted t2 where t1.object_id = t2.object_id endendgo--  测试!use TestDBgoselect * from [vobjectsall]update [vobjectsall] set principal_id = 0 where object_id = 3


更新视图字段 principal_id ,将更新触发器中的第一个分支。执行一次更新耗时不明显,现在单线程更新30次。对比耗时如下图。

第二次更新时,去掉触发器中第二个条件分支的加密语句:

--  第二次更新去掉触发器中的打开密钥语句open symmetric key MySymmetricdecryption by certificate Mycertificate with password = N'Hello@Mycertificate';


这就是奇怪的地方,更新的只是第一个分支中的表。第二个分支是没有执行的,但是第二个分支的 “打开密钥” 却影响到总体时间!

为什么?为什么?为什么?……



0 0