我数据源都是缓存的,所以可以不用考虑读取数据的时间。数据有几万条,因为是AJAX调用比较频繁linq, List, datatable, Dictionary 这几种方式中,那种方式模糊搜索效果较好?下面测试了linq的,要100毫秒,datatable要1秒多(循环500次也是这个时间,感觉循环有问题)这个搜索比较频繁,有没有快点的?
public DataTable GetHotledt(string[] arrkey)
{
DataTable dt = getHoteldata();
DataTable dt1 = new DataTable();
DateTime dts = DateTime.Now;
// for (int i = 0; i < 500; i++)
// {
dt.DefaultView.RowFilter = strFilterSQL(arrkey);
dt1 = dt.DefaultView.ToTable();
// }
Console.WriteLine("dt1: " + totalTime(dts, DateTime.Now));
return dt1;
}
public List<SearchData> GetHotel(string[] arrkey)
{
//List<SearchData> list = GetDistrictList();
List<SearchData> rList = new List<SearchData>();
DateTime dts = DateTime.Now;
// for (int i = 0; i < 500; i++)
// {
var linqData = (from c in list
where (c.CnName.Contains(arrkey[0])) || (c.EngName.Contains(arrkey[0]))
select c)
;
rList = linqData.ToList<SearchData>();
// }
Console.WriteLine("rList: " + totalTime(dts, DateTime.Now));
return rList;
}
private string strFilterSQL(string[] arrkey)
{
StringBuilder sbFilter =new StringBuilder ();
for (int i = 0; i < arrkey.Length; i++)
{
if (arrkey[i].Trim() != "")
{
if (sbFilter.ToString() != "")
{
sbFilter.Append(" and ");
}
sbFilter.Append("( CnName like '%" + arrkey[i] + "%' or EngName like '%" + arrkey[i] + "%' )");
}
}
return sbFilter.ToString ();
}
private static string totalTime(DateTime start, DateTime end)
{
TimeSpan ts = end.Subtract(start).Duration();
return ts.TotalMilliseconds.ToString();
}
可以分词 + 全文索引
或者 单字索引 + 单字位置表