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

纵饮孤独 提交于 2019-12-24 09:18:27

问题


I'm noticing serious performance issues with my application when I started using the data type [bigint] for my stored procedure parameters. The parameter data type for the fast code below is [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). What could be causing this issue? How can I use [bigint] but maintain performance? I'm using Enterprise Library 5 (Database Application Block) with .NET 4.0.

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);

回答1:


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). If they are different, the compaison will do a convertion, and sql server will not use the indexes (can not optimize).



来源:https://stackoverflow.com/questions/22008319/performance-hit-when-passing-argument-of-c-sharp-type-int64-into-t-sql-bigint-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!