zoukankan      html  css  js  c++  java
  • Python学习---Django下的Sql性能的测试

    安装django-debug-tools

    Python学习---django-debug-tools安装

    性能测试:

    settings.py

    INSTALLED_APPS = [
       ...
     'app01',   # 注册app
    ]
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),)  # 现添加的配置,这里是元组,注意逗号
    TEMPLATES = [
       ...
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
    ]

    urls.py

    from django.contrib import admin
    from django.urls import path
    from django.conf.urls import url
    from app01 import views
    from django.conf.urls import include
    from django.conf import settings
    urlpatterns = [
        path('admin/', admin.site.urls),
        url('index/', views.index)
    ]
    if settings.DEBUG:
        import debug_toolbar
        urlpatterns += [
            url(r'^__debug__/', include(debug_toolbar.urls)),
        ]

    views.py

    from django.shortcuts import render, redirect, HttpResponse
    from app01 import models
    def index(request):
        v = models.U.objects.all().prefetch_related('userType')
        return render(request, 'index.html', {"v": v})

    templates/index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>    <meta charset="UTF-8">   </head>
    <body>
        {% for item in v    %}
            {{ item.name }} {{ item.email }} {{ item.userType.caption }}<br>
        {% endfor %}
    </body>
    </html>

    初始化数据库

    python manage.py makemigrations
    python manage.py migrate
    

    页面显示;

    models.U.objects.all():

    image

    models.U.objects.all().select_related(‘userType’):

    image

    models.U.objects.all().prefetch_related('userType')

    image

    性能对比:

    select_related > prefetch_related > all()

  • 相关阅读:
    jvm内存分部
    vue 浏览器滚动行为
    vue中vueRouter使用
    vue脚手架的安装和使用
    vue 在路由中复用组件
    单例模式
    ser2net使用
    怎样使用万用表来测试板子上的TX和RX引脚
    STM32W芯片的JTAG口用于GPIO
    openwrt构建过程探索
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9417885.html
Copyright © 2011-2022 走看看