问题
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