我正在使用MVC
在JSON
中使用jquery datatable服务器端处理。我能够绑定1000万个数据并且分页工作正常但是当我进行搜索时它运行缓慢它正在50 secs
我需要15 secs
。因为它正在搜索多个列,所以我花了一些时间来显示5 columns
。我正在使用SQL
服务器作为后端并使用实体框架。
控制器代码
MVCEntities1 db = new MVCEntities1();
IObjectContextAdapter dbcontextadapter = (IObjectContextAdapter)db;
dbcontextadapter.ObjectContext.CommandTimeout = 180;
int pageNo = 1;
if(param.iDisplayStart >= param.iDisplayLength)
{
pageNo = (param.iDisplayStart / param.iDisplayLength) + 1;
}
if (param.sSearch!=null)
{
var result = (from r in db.MyTable
where ( (r.CompanyName.Contains(param.sSearch)
|| r.City.Contains(param.sSearch) || r.Website.Contains(param.sSearch) ||
r.Contact_Name.Contains(param.sSearch) ))
orderby r.id
select new { r.CompanyName, r.Website, r.ContactName, r.Country, r.City, r.Phone_Number }).
Skip((pageNo - 1) * param.iDisplayLength).Take(param.iDisplayLength);
var resultcount = (from r in db.MyTable
where ((r.Company_Name.Contains(param.sSearch) ||
r.City.Contains(param.sSearch) || r.Website.Contains(param.sSearch) ||
r.Contact_Name.Contains(param.sSearch)
))
select r).Count();
JsonResult result2 = Json(new
{
aaData = result,
sEcho = param.sEcho,
iTotalDisplayRecords = resultcount,
iTotalRecords = resultcount
}
, JsonRequestBehavior.AllowGet);
result2.MaxJsonLength = 8675309;
return result2;
}
Index view code :
var BindDataTable = function (response) {
$.noConflict();
var tbl= $("#EmpInfo").DataTable({
"bServerSide": true,
"sAjaxSource": "/Main/CustomerList",
"Processing": true,
"deferRender": true,
"fnServerData": function (sSource, aoData, fnCallback) {
var countryname = $('#Country').val();
aoData.push(
{ "name": "Id", "value": countryname }
);
$.ajax({
type: "GET",
url: sSource,
data: aoData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: fnCallback
})
},
"aoColumns": [
{ "mData": "CompanyName" },
{ "mData": "Website" },
{ "mData": "ContactName" },
{ "mData": "Country" },
{ "mData": "City" },
{ "mData": "PhoneNumber" }
]
});
<div class="form-horizontal">
<table id="EmpInfo" width="90%" align="center" class="table table-bordered table-hover">
<thead>
<tr><td colspan="2"> <input type="button" onclick="closetable()" value="close x" id="closetable" /></td></tr>
<tr>
<th>Company Name</th>
<th>Website</th>
<th>Contact Name</th>
<th>Country</th>
<th>City</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>