SearchUserTT方法,去数据库中用存储过程查询用户表,返回DataTable并且缓存起来,下次再取的时候如果有缓存就取缓存。 public DataTable SearchUserTT(int customerid, int exceptionid, bool islimit)
{ DataTable dtCache = new DataTable(); if (HttpContext.Current.Cache["dtSearchUserName"] != null)
{
dtCache = (DataTable)HttpContext.Current.Cache["dtSearchUserName"];
return dtCache;
}
else
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["learnways_main_dsn"]);
SqlCommand com = new SqlCommand("DL_UserSearch", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@customerid", SqlDbType.Int).Value = customerid;
com.Parameters.Add("@exceptionid", SqlDbType.Int).Value = exceptionid;
com.Parameters.Add("@islimit", SqlDbType.Bit).Value = islimit;
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dtCache); HttpContext.Current.Cache.Insert("dtSearchUserName", dtCache, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero); sda.Dispose();
com.Dispose();
con.Close(); return dtCache; }
}在这里来进行操作, 用DataView.RowFilter 属性来进行筛选。 取符合条件的前十条记录。 [AjaxMethod()]
public string SearchUser(string value)
{
StringBuilder sb = new StringBuilder();
int customerid = ((User)(HttpContext.Current.Session["User"])).CustomerId;
value = "%" + value.Trim() + "%";
string dvQuerystr = "strFirstname LIKE '" + value + "' OR strLastname LIKE '" + value
+ "' OR strFirstname+strLastname LIKE '" + value
+ "' OR strFirstname+' '+strLastname LIKE '" + value + "'"; DataView dv = new DataView(SearchUserTT(customerid, 0, true));
dv.RowFilter = dvQuerystr; int top = 10;
if (dv.Count <= 10 )
{
top = dv.Count;
}
for (int i = 0; i < top; i++)
{
sb.Append("<div onmouseout='RollOut(this)' onmouseover='RollOver(this)' class='lw__searchRes' id='" + dv[i].Row["intuserid"] + "' onClick='ChooseItem(this, " + dv[i].Row["intuserid"] + ")'>" + dv[i].Row["strfirstname"] + " " + dv[i].Row["strlastname"] + " " + dv[i].Row["stremail"] + "</div>");
} dv.Dispose();
return sb.ToString();
}
我现在的应用是:
在一个文本框里输入一个人的姓名,每输入一个字母就去选出符合条件的前十条。
比如我叫Fredrik,输入F查找显示在下面,再输入r就查找Fr,再输e就查找Fre。
现在我提出的问题是:
缓存起来的DataTable中数据有五万条,每次通过DataView.RowFilter去筛选都是新建的一个DataTable,
速度上比较慢,希望听听大家的想法,看有没有更好的实现方式。比如百度 谷歌,输入几个关键字查询出来会非常快。 希望大家说说自己的看法,贴一下代码。
{ DataTable dtCache = new DataTable(); if (HttpContext.Current.Cache["dtSearchUserName"] != null)
{
dtCache = (DataTable)HttpContext.Current.Cache["dtSearchUserName"];
return dtCache;
}
else
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["learnways_main_dsn"]);
SqlCommand com = new SqlCommand("DL_UserSearch", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@customerid", SqlDbType.Int).Value = customerid;
com.Parameters.Add("@exceptionid", SqlDbType.Int).Value = exceptionid;
com.Parameters.Add("@islimit", SqlDbType.Bit).Value = islimit;
SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(dtCache); HttpContext.Current.Cache.Insert("dtSearchUserName", dtCache, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero); sda.Dispose();
com.Dispose();
con.Close(); return dtCache; }
}在这里来进行操作, 用DataView.RowFilter 属性来进行筛选。 取符合条件的前十条记录。 [AjaxMethod()]
public string SearchUser(string value)
{
StringBuilder sb = new StringBuilder();
int customerid = ((User)(HttpContext.Current.Session["User"])).CustomerId;
value = "%" + value.Trim() + "%";
string dvQuerystr = "strFirstname LIKE '" + value + "' OR strLastname LIKE '" + value
+ "' OR strFirstname+strLastname LIKE '" + value
+ "' OR strFirstname+' '+strLastname LIKE '" + value + "'"; DataView dv = new DataView(SearchUserTT(customerid, 0, true));
dv.RowFilter = dvQuerystr; int top = 10;
if (dv.Count <= 10 )
{
top = dv.Count;
}
for (int i = 0; i < top; i++)
{
sb.Append("<div onmouseout='RollOut(this)' onmouseover='RollOver(this)' class='lw__searchRes' id='" + dv[i].Row["intuserid"] + "' onClick='ChooseItem(this, " + dv[i].Row["intuserid"] + ")'>" + dv[i].Row["strfirstname"] + " " + dv[i].Row["strlastname"] + " " + dv[i].Row["stremail"] + "</div>");
} dv.Dispose();
return sb.ToString();
}
我现在的应用是:
在一个文本框里输入一个人的姓名,每输入一个字母就去选出符合条件的前十条。
比如我叫Fredrik,输入F查找显示在下面,再输入r就查找Fr,再输e就查找Fre。
现在我提出的问题是:
缓存起来的DataTable中数据有五万条,每次通过DataView.RowFilter去筛选都是新建的一个DataTable,
速度上比较慢,希望听听大家的想法,看有没有更好的实现方式。比如百度 谷歌,输入几个关键字查询出来会非常快。 希望大家说说自己的看法,贴一下代码。
不知道还有没有别的方法。。
你想百度谷歌? 楼主 别想 你想不出来
看看它的查询速度是不是快一点.
asp.net2.0里暂时还没有出来LINQ,应该用不了。
然后如果是FE,那么把不是FE的全删了。。存下如此如此,这般这般。存当然可是是缓存,也可是其它的方式!不过,现在jQuery有个插件了,
基本上去他了,漂亮又开发速度快!
现在就是觉得建立了缓存后,用RowFilter来筛选,效果不好。
大家有什么思路没有,诚心学习。
dt。select()
试试B-tree。至于Ajax效果,楼主搜搜googleSuggest的JQuery插件,我用过很好用的,
而且那个插件可以进行客户端的缓存,减轻服务器压力。
还是尝试一下那个插件的效果吧,做客户端缓存,不太必要,因为这种筛选每次都要去服务端执行筛选代码,再返回结果。
[AjaxMethod()]
public string SearchUser(string value)
{
value = "%" + value.Trim() + "%";
string dvQuerystr = "strFirstname LIKE '" + value + "' OR strLastname LIKE '" + value
+ "' OR strFirstname+strLastname LIKE '" + value
+ "' OR strFirstname+' '+strLastname LIKE '" + value + "'";
int customerid = ((User)(HttpContext.Current.Session["User"])).CustomerId;
var key=string.Format("query is {0}, customerid is {1}",dvQuerystr,customerid);
var cache=HttpContext.Cache;
StringBuilder sb =cache[key];
if(sb==null)
{
sb= 去查什么DataView之类的随便什么算法(customerid, dvQuerystr);
cache.Insert(key, sb, null, //其实null写为SqlDependency或者自定义的数据库依赖项对象才准确
DateTime.Now.AddMinutes(60), TimeSpan.Zero);
}
return sb.ToString();
}
Cache应该放在高层次BLL层。如果搁在底层,肯定容易滥用,甚至想出一次读取几万条记录这么简单恐怖的做法。
你这样还不如直接去查数据库我想 你所理解的缓存应该是把数据库里的东西搬到内存里用RowFilter来筛选 不是与数据库查一样么?如果数据少内存会很快,数据一多 不仅内存不够用导致服务器崩溃,而且查的速度很慢(RowFilter 相当于是在内存表中进行where语句)缓存是什么?就是把经常查询到的,不易更新的数据放到内存里你那5W条记录是要经常查询显示的?想想看?
真正要查询显示的是多少?
存了多少垃圾在内存里?