asp.net - Performance of SqlParameter in C# -
i have stored procedure this:
create proc wsp_test ( @age int , @username varchar(30) ) select userid, username users username '%' + @username + '%' , age > @age
and here c# code:
case 1:
sqlcommand _cmd = new sqlcommand(); _cmd.parameters.add("age", sqldbtype.int, 4); _cmd.parameters.add("username", sqldbtype.varchar, 30);
case 2:
sqlcommand _cmd = new sqlcommand(); _cmd.parameters.add("age", sqldbtype.int); _cmd.parameters.add("username", sqldbtype.varchar);
case 3:
sqlcommand _cmd = new sqlcommand(); _cmd.parameters.add("age"); _cmd.parameters.add("username");
case 4:
sqlcommand _cmd = new sqlcommand(); _cmd.commandtext = "exec wsp_test 20, 'john'";
now question has got performance specify parameter's datatype or length?
which 1 has best performance? there documents microsoft's web site can rely on?
or there security reasons specify parameter's datatype , length?
the performance of datatype irrelevant compared this:
where username '%' + @username + '%'
this particular part of query going take exponentially more time dealing parameters.
i recommend don't care performance @ stage. it's early. not micro-benchmark of whether or not specify type , length of parameters. @ other factors. yes, should specify type, makes code more maintainable , can make runtime casting issues easier identify , solve.
if later determine particular code bottleneck application, using full text username field. can @ statistics code's usage , identify if need it. majority of queries match username or @ least starting with? use case search first on exact matches, faster, , search partial matches of no exact matches found?
Comments
Post a Comment