1. 优化工具
- QuerySet.explain(),查看sql执行计划
- django-debug-toolbar ,网页性能优化
QuerySet.explain()
>>> from blueapps.account.models import User
>>> users = User.objects.all()
>>> users.explain()
'0 0 0 SCAN TABLE account_user'
>>> user = users.filter(id='1')
>>> user.explain()
'0 0 0 SEARCH TABLE account_user USING INTEGER PRIMARY KEY (rowid=?)'
django-debug-toolbar
2. django-debug-toolbar安装和使用
环境
Package | Version |
---|
Django | 2.2.6 |
python | 3.6.0 |
django-debug-toolbar | 3.2 |
a. 安装
pip install django-debug-toolbar==3.2
b.配置
1)配置setting文件
DEBUG_TOOLBAR_PATCH_SETTINGS = False
INSTALLED_APPS = INSTALLED_APPS + (
'debug_toolbar.apps.DebugToolbarConfig',
)
MIDDLEWARE = MIDDLEWARE + (
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
INTERNAL_IPS = ("127.0.0.1",)
DEBUG_TOOLBAR_PANELS = [
'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.request.RequestPanel',
'debug_toolbar.panels.sql.SQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'debug_toolbar.panels.templates.TemplatesPanel',
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.logging.LoggingPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',
]
2) 配置urls.py文件
import settings
if settings.DEBUG:
import debug_toolbar
urlpatterns.append(url(r'^__debug__/', include(debug_toolbar.urls)))
参考:
- 腾讯课堂
- Django数据库访问优化
- 在django中使用django_debug_toolbar