简体   繁体   English

Django 数据表加载时间过长

[英]Django Datatables taking too long to load

I am trying to load 25600 rows into datatables but it's taking around 10 seconds.我正在尝试将 25600 行加载到数据表中,但大约需要 10 秒。 The request is via an ajax API call.该请求是通过 ajax API 调用进行的。

views.py视图.py

@api_view()
def get_all_data(request):


get_all_data_ = Data.objects.values("name","contact_number","email_address","address","age",
"location_type","sector","phase","total_data","total_usage","city","district")
                 

return JsonResponse(list(get_all_data_), safe=False)

template.html模板.html

var table = $('#data-table').DataTable({

                serverSide: true,

                "ajax": {
                "url": "/alldata/",
                "dataSrc": ""
                },

              "columns": [

                  {"data": "name"},
                  {"data": "contact_number"},
                  {"data": "email_address"},
                  {"data": "address"},
                  {"data": "age"},
                  {"data": "location_type"},
                  {"data": "sector"},
                  {"data": "phase"},
                  {"data": "total_data"},
                  {"data": "total_usage"},
                  {"data": "city"},
                  {"data": "district"}
                 
              ],

        
          });

How can i make it instantaneous?我怎样才能让它瞬间完成?

What I would do:我会怎么做:

  • I would add pagination (as @Willem Van Onsem suggested) just load the first 100 (for example) samples and add link to paginate over the table (next/previous).我会添加分页(如@Willem Van Onsem 建议的那样)只需加载前 100 个(例如)样本并添加链接以在表格上分页(下一个/上一个)。 Take a look at Django Paginator .看看 Django分页器
  • If you want to show some information about the whole dataset, I would compute statistics describing the data and display it instead of whole data.如果你想显示关于整个数据集的一些信息,我会计算描述数据的统计数据并显示它而不是整个数据。 For example mean values, or median values ...例如平均值或中值...

If you really want to make it faster with the whole table:如果你真的想让整张桌子更快:

  • You can try to use a faster server (maybe will help a little),您可以尝试使用更快的服务器(也许会有所帮助),
  • You can try to cache it, maybe store the data in the memory so you don't need to run DB query for each request (but you need to remember to refresh/validate the cache).您可以尝试缓存它,也许将数据存储在内存中,这样您就不需要为每个请求运行数据库查询(但您需要记住刷新/验证缓存)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM