Sql filter and or statement within C# application -
i have c# application in have problem : when run snippet:
first way
public void getlist(list<string> liste, list<int> outliste) { foreach( string s in liste){ outliste.add(sqlfunction(s)); } } public int sqlfunction(string str) { string query = "select id user name="+str; ................... // homecoming id } the execution time 51 sec
second way
public void sqlsecondwayfunction(list<string> liste, list<int> outliste) { string query ="select id user ("; foreach(string str in liste){ query += "name=" str + "or "; } query += " 1=0 )"; ................... // fill outliste result of query } the execution time 1m:19sec!!!!!!!!! ( count of liste 11000).
so need know why first way faster?
the first faster because it's doing fewer things. , it's selecting subset of records.
the sec slower because you're concatenating tons of strings (which slow do) performing thousands of unnecessary comparisons on database every record, homecoming every record anyway.
basically, you're asking database compare name column against 11,000 strings every record in table. if table contains, example, 100,000 records you're making 1,100,000,000 string comparisons. , homecoming of records anyway because 1 of conditions "1 = 1" always true.
c# sql .net collections ado.net
No comments:
Post a Comment