ASP.NET Ajax Get花费的时间太长
我有一个asp.NETMVC项目。我通过ajax get从数据库中获得了一些问题和答案。我的查询时间太长了。如何编辑代码以加快工作速度 我将问题和答案存储在不同的表格中。每个答案都是由ids提供的相关问题 谢谢你的回答 我的看法是:ASP.NET Ajax Get花费的时间太长,ajax,model-view-controller,Ajax,Model View Controller,我有一个asp.NETMVC项目。我通过ajax get从数据库中获得了一些问题和答案。我的查询时间太长了。如何编辑代码以加快工作速度 我将问题和答案存储在不同的表格中。每个答案都是由ids提供的相关问题 谢谢你的回答 我的看法是: $.ajax({//GET QUESTIONS url: '@Url.Action("GetQuestionsBySubCategory", "Order")', type: "GET",
$.ajax({//GET QUESTIONS
url: '@Url.Action("GetQuestionsBySubCategory", "Order")',
type: "GET",
data: { subcattext : selectedSubCategory },
success: function (result) {
$('<div class=\"form-group\" id=\"sorularform\" ><input name=\"altcat\" value=\"' + selectedSubCategory + '\" type=\"hidden\">').prependTo("#sorular");
// loop each question
for (var i = 0; i < result.length; i++) {
//IF QUESTION 1 START
if (result[i].QuestionType == 1) {
$('<label for=\"exampleFormControlSelect' + i + '\" > ' + result[i].Description + '</label ><select name=\"' + result[i].Description + ' \" class=\"form-control\" id=\"exampleFormControlSelect' + result[i].Id + '\"></select>').appendTo("#sorularform");
var questionid;
$.ajax({//GET ANSWERS
url: '@Url.Action("GetAnswersByQuestionId", "Order")',
type: "GET",
data: { questionid: result[i].Id },
success: function (answerresult) {
for (var a = 0; a < answerresult.length; a++) {
$('<option>' + answerresult[a].Description + '</option>').prependTo("#exampleFormControlSelect" + answerresult[a].Question_Id);
}
},
error: function (err) {
// the call thrown an error
console.log("Hata Oluştu")
},
complete: function () {
//$(".loading").hide();
}
});
};
}
},
error: function (err) {
// the call thrown an error
console.log("Hata Oluştu")
},
complete: function () {
//$(".loading").hide();
$("</div>").appendTo(".form-group");
$('#yersec').insertBefore('#sorularform');
//$('#sorular').html(table);
}
});
$.ajax({//获取问题
url:'@url.Action(“GetQuestionsBySubCategory”,“Order”),
键入:“获取”,
数据:{subcattext:selectedSubCategory},
成功:功能(结果){
$('').prependTo(“#sorular”);
//循环每个问题
对于(变量i=0;i
控制器
public ActionResult GetQuestionsBySubCategory(string subcattext)
{
var subcatid = subCategoryServices.GetAll().Where(x => x.Title == subcattext).FirstOrDefault().Id;
IEnumerable<QuestionVM> questionList = questionServices.GetAll().Where(x => x.SubCategory_Id == subcatid);
return Json(questionList, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult GetAnswersByQuestionId(int questionid)
{
IEnumerable<AnswerVM> answerList = answerServices.GetAll().Where(x => x.Question_Id == questionid);
return Json(answerList, JsonRequestBehavior.AllowGet);
}
public ActionResult GetQuestionsBySubCategory(字符串子文本)
{
var subcategid=subCategoryServices.GetAll(),其中(x=>x.Title==subcatext.FirstOrDefault().Id;
IEnumerable questionList=questionServices.GetAll(),其中(x=>x.SubCategory\u Id==subcatid);
返回Json(questionList,JsonRequestBehavior.AllowGet);
}
[HttpGet]
公共行动结果GetAnswersByQuestionId(int questionid)
{
IEnumerable answerList=answerServices.GetAll(),其中(x=>x.Question\u Id==questionid);
返回Json(answerList,JsonRequestBehavior.AllowGet);
}
如果您想加快查询速度,我建议您使用简洁的方式。Dapper是一种轻量级ORM,其运行速度几乎与本机SQL一样快。这里有一个很好的教程,我建议您阅读:
基本上,您将运行这样的查询(这并不确切,但非常接近简洁的语法,而没有看到数据库结构或其他代码):
public ActionResult GetQuestionsBySubCategory(字符串子文本)
{
var subcatid=db.Query(“从SubcategoryServices中选择Id,其中Title=@Title”,new{Title=subcatext});
IEnumerable questionList=db.Query(“从QuestionServices中选择*,其中SubCategory_Id=@SubCategory Id”,new{SubCategory Id=subcatid});
返回Json(questionList,JsonRequestBehavior.AllowGet);
}
撇开不谈-DOM操作也非常慢,因此这也可能会使应用程序速度变慢。技术原因是,一旦向DOM添加元素,它就必须重新呈现自身。渲染需要时间
在这里创建整个应用程序超出了范围,但我强烈建议at或as前端库创建更快的前端代码。加快数据库速度。或者将代码更改为在数据库中进行实际筛选。为什么要调用服务的
GetAll()
方法?它可能会返回数据库中的所有行,因此请求速度较慢。而且,在客户端代码的每次迭代中添加附加的方式也很昂贵。@Hackerman我认为soI使用的是实体框架通用存储库模式。这仍然很好。在实现您创建的存储库接口的类中,您可以使用Dapper从数据库检索数据,而不是使用Entity Framework。我认为我的前端代码很昂贵。我有200个问题和600个答案。这个数字对于EFYes来说不是问题。是的,我建议您在前端使用ReactJS,否则您的浏览器将重新呈现800个元素,这肯定会降低它的速度
public ActionResult GetQuestionsBySubCategory(string subcattext)
{
var subcatid = db.Query("SELECT Id from SubcategoryServices WHERE Title = @title", new { title = subcattext });
IEnumerable<QuestionVM> questionList = db.Query("SELECT * FROM QuestionServices WHERE SubCategory_Id = @subcategoryId", new { subcategoryId = subcatid });
return Json(questionList, JsonRequestBehavior.AllowGet);
}