简体   繁体   English

将C#类型Int64的参数传递到T-SQL bigint存储过程参数时,性能下降

[英]performance hit when passing argument of C# type Int64 into T-SQL bigint stored procedure parameter

I'm noticing serious performance issues with my application when I started using the data type [bigint] for my stored procedure parameters. 当我开始将数据类型[bigint]用于存储过程参数时,我注意到应用程序存在严重的性能问题。 The parameter data type for the fast code below is [nvarchar](50) . 以下快速代码的参数数据类型为[nvarchar](50) Below is some code that I changed, and this simple call went from < 1 second (fast code) to over 20 seconds (slow code). 下面是我更改的一些代码,此简单调用从<1秒(快速代码)更改为超过20秒(慢速代码)。 What could be causing this issue? 是什么导致此问题? How can I use [bigint] but maintain performance? 如何使用[bigint]保持性能? I'm using Enterprise Library 5 (Database Application Block) with .NET 4.0. 我在.NET 4.0中使用Enterprise Library 5(数据库应用程序块)。

Before (fast): 之前(快速):

            Database db = DatabaseFactory.CreateDatabase("APP");
            DbCommand cmd = db.GetStoredProcCommand("sp_test");
            db.AddInParameter(cmd, "@crud_command", DbType.String, "read");
            db.AddInParameter(cmd, "@network_login", DbType.String, "abc231");
            db.AddInParameter(cmd, "@id_filter", DbType.String, id_filter);
            DataSet ds = db.ExecuteDataSet(cmd);

After (slow): 之后(慢):

            Database db = DatabaseFactory.CreateDatabase("APP");
            DbCommand cmd = db.GetStoredProcCommand("sp_test");
            db.AddInParameter(cmd, "@crud_command", DbType.String, "read");
            db.AddInParameter(cmd, "@network_login", DbType.String, "abc231");
            db.AddInParameter(cmd, "@id_filter", DbType.Int64, Convert.ToInt64(id_filter));
            DataSet ds = db.ExecuteDataSet(cmd);

You have to check the type in the db, make sure the type of the parameter is the same as the column you are quering (I guess it is a varchar, not a bigint). 您必须检查db中的类型,确保参数的类型与您要查询的列相同(我想这是varchar,而不是bigint)。 If they are different, the compaison will do a convertion, and sql server will not use the indexes (can not optimize). 如果它们不同,则比较将进行转换,并且sql server将不使用索引(无法优化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM