我最近不得不更改应用程序的数据访问层,以使用参数化SQL而不是动态SQL。对于SQL Server,这意味着将值作为命令参数发送,而不是在代码中执行字符串替换。
对于需要插入27k行的进程,将这些命令分解为每个命令有96个插入,总计2016个输入变量。每个命令大约需要2.5秒。在此之前,每个命令大约有1000次插入,平均花费0.5秒。
我该怎么做才能优化此过程?
c#
foreach (ParameterObject po in sqlParam.ColumnParameters)
{
SqlParameter spParam = new SqlParameter();
spParam.Direction = po.Direction;
spParam.SqlDbType = ConvertToDBType(po.Type);
spParam.ParameterName = po.ParameterName;
if (po.FieldSize != null)
spParam.Size = po.FieldSize;
spParam.Value = po.Value;
command.Parameters.Add(spParam);
}
// execute command and get return value
oiRowsAffected = command.ExecuteNonQuery();
T-SQL插入代码段:
INSERT INTO MYTABLE (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7, COLUMN8, COLUMN9, COLUMN10, COLUMN11, COLUMN12, COLUMN13, COLUMN14, COLUMN15, COLUMN16, COLUMN17, COLUMN17, COLUMN18, COLUMN19, COLUMN20)
VALUES (@MYTABLE_COLUMN1_0D1BC2AB84884349A6912AB92FF60CE3, @MYTABLE_COLUMN2_06C42777A52A424192C7440D9F81E2ED, @MYTABLE_COLUMN3_D758A1B77F274CE8A69C830BD10B9A23, @MYTABLE_COLUMN4_A235ED1E13D34155B0A6291E88DF2E4D, @MYTABLE_COLUMN5_71413333777143A4AD9AFA3B8067433B, @MYTABLE_COLUMN6_E14252A4AD6E48B281B255D6814EB5E3, @MYTABLE_COLUMN7_7B5B12B20BBE43BFBC6C7924E2CFE08F, @MYTABLE_COLUMN8_74F9FB0AB9B54F4B8A874DE739089DF2, @MYTABLE_COLUMN9_9441985F2B464523A75081AD74EA2F18, @MYTABLE_COLUMN10_6F17E6806F2A4E32B20E1915AF2A09B8, @MYTABLE_COLUMN11_B91E1549AA0B4FD7B3E1395D8D9CA0A2, @MYTABLE_COLUMN12_E9B2DE8B17D24725B866E2A8505C0E9A, @MYTABLE_COLUMN13_814D51588A73478790D8B7C16D60A736, @MYTABLE_COLUMN14_3CE557074F784B378DE983D01FA221FF, @MYTABLE_COLUMN15_5CE50C3C97C0493A92CA411C2635C3AF, @MYTABLE_COLUMN16_9C143DBFAA2C4517A275DE1845760A5C, @MYTABLE_COLUMN17_B6E6D88C228D419FBEDD9EA866670A09, @MYTABLE_COLUMN17_E2479C7F52464CE3994DC75EBABD0B36, @MYTABLE_COLUMN18_23444536201A4E64898BFBD724FFD0A0, @MYTABLE_COLUMN19_35936A60BD294FDB97A62EE32C4DDA6D, @MYTABLE_COLUMN20_E87315891E8E4AE58754595C4C825288);
IF @@ROWCOUNT <> 1
BEGIN
RAISERROR('-20001 Table: MYTABLE Function Type: Insert', 16, 1)
RETURN
END;
INSERT INTO MYTABLE (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7, COLUMN8, COLUMN9, COLUMN10, COLUMN11, COLUMN12, COLUMN13, COLUMN14, COLUMN15, COLUMN16, COLUMN17, COLUMN17, COLUMN18, COLUMN19, COLUMN20)
VALUES (@MYTABLE_COLUMN1_BF6CCBB453A444838F7ED12CB48916E7, @MYTABLE_COLUMN2_E3159D1012C3414595C36DCC9D93CA40, @MYTABLE_COLUMN3_A17320C11DB040AA9EED31B2CCA3CAEE, @MYTABLE_COLUMN4_C8BA776010674AE1BFB3606AD3B80F27, @MYTABLE_COLUMN5_447E68BF44E34AF4B392B85E9F5C8CE2, @MYTABLE_COLUMN6_4D66B26D024C447D8EDB45B619C9ADEA, @MYTABLE_COLUMN7_F341652C63714031923B9C06BB9379DB, @MYTABLE_COLUMN8_E329040EE254465CB44B75A1C01BC0E9, @MYTABLE_COLUMN9_DBEB6C97DD55473D9D818A6A640A11DF, @MYTABLE_COLUMN10_27BB9F25659244BA8CB0F9E7B6B16CE3, @MYTABLE_COLUMN11_87E43C9094BB403695B224C9FC7AA27A, @MYTABLE_COLUMN12_4F958FE87BF34F62BC38BE87BD7A209D, @MYTABLE_COLUMN13_158AF0196D6D4B8FB1AA92287A34ED2E, @MYTABLE_COLUMN14_FF1EFDFA067C4CD998A52B96F9DEBF43, @MYTABLE_COLUMN15_A3FCF4F6943B4D41986AD226A403A420, @MYTABLE_COLUMN16_04E3D88ED6214A8B97E357E2626248A0, @MYTABLE_COLUMN17_3CB9F2328D304212B58930FAA55081DF, @MYTABLE_COLUMN17_0CF8E3BB79424EA389FCEEC46BDAEB1C, @MYTABLE_COLUMN18_7092C63513F0465CB430181E68B92BD6, @MYTABLE_COLUMN19_96D96C8F70BD4B1191FA37B0D75048BA, @MYTABLE_COLUMN20_7AB75F13809B48F8A37A9BBBF5A02DF5);
IF @@ROWCOUNT <> 1
BEGIN
RAISERROR('-20001 Table: MYTABLE Function Type: Insert', 16, 1)
RETURN
END;