【问题标题】:Autocomplete search taking too long to retrieve data from server自动完成搜索需要很长时间才能从服务器检索数据
【发布时间】:2018-03-08 21:36:56
【问题描述】:

我在用户控制页面中有一个 AJAX 自动完成搜索,它在 document.ready 上被调用。我们对 Web 服务进行 AJAX 调用,该服务从数据库中获取数据(大约 90,000 个),将数据插入缓存,将数据返回到 JavaScript 并添加到数组中。

它首先从数据库中获取数据,然后将数据插入缓存后,每次从缓存中获取数据。当我们在文本框上键入内容时,它会将文本框的文本与数组匹配并显示列表。要从存储过程中获取 90,000 个项目,在本地服务器中需要 2 秒。

但在实时服务器上,从存储过程中获取数据大约需要 40 秒。同样,从缓存中获取数据也需要同样的时间。如何减少时间并提高性能?

AJAX 调用:

var locationSearchListData = [];
        var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
        var postData = '{cultureId : "DE"}';
        // Ajax call to run webservice's methods.
        $.ajax({
            url: "/asmx/SearchLocations.asmx/GetAutoCompleteSearchLocations",
            type: "POST",
            data: postData,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (responseData) {
                if (responseData != undefined && responseData != null && responseData.d.length > 0) {
                    for (i = 0; i < responseData.d.length; i++) {
                        // Add resopnse data in location search lis, this list is used as a source for autocomplete textbox.
                        locationSearchListData.push({
                            label: responseData.d[i].locationData,
                            latitude: responseData.d[i].latitude,
                            longitude: responseData.d[i].longitude
                        });
                    }
                }

网络服务:

    [ScriptMethod]
    [WebMethod(Description = "Provides instant search suggestions")]
    public List<GeoLocationObject> GetAutoCompleteSearchLocations(string cultureId)
    {
        SqlDatabase database = new SqlDatabase(WebConfigurationManager.ConnectionStrings["MasterDB"].ConnectionString);

        string databaseName = WebConfigurationManager.AppSettings["databaseName"];
        // Key to identify location search data in cache
        string cacheKey = "auto_complete_result";
        // List to store locations 
        List<GeoLocationObject> lstGeolocationObject = new List<GeoLocationObject>();

        // If location data is present in cache then return data from cache.
        if (Context.Cache[cacheKey] is List<GeoLocationObject>)
        {
            return Context.Cache[cacheKey] as List<GeoLocationObject>;
        }
        else // If data is not present in cache, get data from db and add into cache.
        {
            // Call method GetAutoCompleteSearchLocations of LocationManager to get list of geo location object.
            lstGeolocationObject = LocationManager.GetAutoCompleteSearchLocations(database, cultureId);

            // Checking if lstGeolocationObject is not null
            // If its not null then adding the lstGeolocationObject in the cache
            if (lstGeolocationObject.Count > 0)
            {
                // Add locationdata in cache.
                // Removed sqlcache dependency.
                Context.Cache.Insert(cacheKey,
                                    lstGeolocationObject,
                                    null,
                                    Cache.NoAbsoluteExpiration,
                                    Cache.NoSlidingExpiration,
                                    CacheItemPriority.NotRemovable,
                                    null);
            }

            // Return geolocation data list
            return lstGeolocationObject;
        }

    } // GetAutoCompleteSearchLocations

【问题讨论】:

  • 与其写一个pad,为什么不把代码放在有问题的地方呢?如果您真的需要帮助,我们会更好......
  • 请检查代码。
  • 我的建议是:不要提前下载所有 9000 条记录,这会浪费大量带宽,因为在页面的生命周期中可能只使用了一小部分。而是将自动完成设置为直接向服务器发出请求,传入搜索词,然后只需查询数据库以查找与该特定项目的匹配项。然后,您应该每次都会收到一个很小的请求和响应,因此希望时间延迟会小得多。
  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。

标签: javascript asp.net ajax web-services caching


【解决方案1】:

对不起,我的回复晚了。 感谢大家帮助我找到解决方案。

我们已通过对 ajax 调用进行一些更改来解决该问题。我们将搜索文本发送为:

    // set auto complete textbox
    $("#<%= txtOrtOderPlz.ClientID%>").autocomplete({            
    // Set required minimum length to display autocomplete list.
    minLength: 3,

    // Set source for textbox, this source is used in autocomplete search. 
    source: function (request, response) {      
    // Ajax call to run webservice's methods.
    $.ajax({
       url: "/asmx/SearchLocations.asmx/GetAutoCompleteSearchLocations",
       type: "POST",
       data: '{ searchText :\'' + request.term + '\' }',
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       Success: function (responseData) {                                                     
       response(responseData.d);
   },
   error: function (XMLHttpRequest, textStatus, errorThrown) {                        
   }
   });
   },

每次我们搜索任何内容时,它都会获取搜索文本并调用 Web 服务,将搜索文本发送到 SP,从 SP 获取数据并显示在自动完成搜索中。

【讨论】:

    【解决方案2】:

    自动完成功能的主要目的是缩小搜索范围并仅将几乎匹配的记录放在前面,以方便用户选择他想要的确切记录。如果可能,请尝试添加 debounceTime()。 其他选项是微调 sql 查询、实现服务器端分页和检查浏览器中的页面呈现时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 2018-02-07
      • 2016-07-30
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多