Optimization
SQL๋ฌธ ์คํ
Iteration
Slicing
๊ธฐ๋ณธ์ ์ผ๋ก x
step์ ํ์ฉํ ๋์๋ง
[3:10:2]
repr
len
bool
Query ๊ฐ์
ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ : django debug toolbar
installation
$ pip install django-debug-toolbarPrerequisites
settings.py
Setting up URLconf
urls.pyin the root directory
Enabling middleware
settings.py
Configuring Internal IPs
settings.py
0. ๊ด๋ จ ๋ฌธ์
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ต์ ํ
: https://docs.djangoproject.com/en/3.0/topics/db/optimization/
QuerySet ์คํ์ ๊ดํ ์ดํด : https://docs.djangoproject.com/en/3.0/topics/db/optimization/#understand-queryset-evaluation
lazyํ์ฌ, evaluated ๋๋ ์์ ์ ์คํ๋๋ฉฐ, cache๋ฅผ ํ์ฉํ ์ ์์. (๊ฐ๊ฐ ๋ฌธ์ ํ์ธํ ๊ฒ)
count,exists์ผ๋ฐ์ ์ผ๋ก ํ์ฉํ๋ ๊ฒ์ด ์ข์ผ๋, ์์์ ์ฝ๋์ ์ํฉ์์๋ cache๋ ๊ฐ์ ๋ฐํ์ผ๋ก length๋ฅผ ๊ตฌํ๋ ๋ฐฉ์์ผ๋ก ํ์ด๋๊ฐ ์ ์์
1. ๋๊ธ ์ ์ถ๋ ฅ - annotate() ํ์ฉ
annotate() ํ์ฉN+1 problem
๊ฐ์ ์ (11๋ฒ)

๊ฐ์ ํ (1๋ฒ)

2. ๊ฒ์๊ธ ์์ฑ์ ์ด๋ฆ ์ถ๋ ฅ - selected_related() ํ์ฉ
selected_related() ํ์ฉ
select_related๋ SQL JOIN์ ํตํด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค1:1, 1:N ๊ด๊ณ์์ ์ฐธ์กฐ๊ด๊ณ (N -> 1, foreignkey๊ฐ ์ ์๋์ด ์๋ ๊ณณ)
๊ฐ์ ์ (11๋ฒ)

๊ฐ์ ํ (1๋ฒ)

3. ๊ฒ์๊ธ๋ง๋ค ๋๊ธ๋ค ์ถ๋ ฅ - prefetch_related() ํ์ฉ
prefetch_related() ํ์ฉ
prefetch_related๋ python์ ํตํ join์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋คM:N, 1:N ๊ด๊ณ์์ ์ญ์ฐธ์กฐ ๊ด๊ณ (1->N)
๊ฐ์ ์ (11๋ฒ)

๊ฐ์ ํ (2๋ฒ)

4. ๊ฒ์๊ธ๋ง๋ค ์์ฑ์ ์ด๋ฆ๊ณผ ๋๊ธ๋ค ์ถ๋ ฅ
๊ฐ์ ์ (111๋ฒ)

๊ฐ์ ํ (2๋ฒ)

SQL Join

ex)
+
Gravata ํ์ฉํ์ฌ profile photo ์ค์ ํ๊ธฐ
https://en.gravatar.com/site/implement/
๋ฐฉ๋ฒ 1) @property ์ค์
accounts > models.py
๋ฐฉ๋ฒ 2) templatetags ๋ง๋ค๊ธฐ
accounts > templatetags > gravatar.py
templatetagsdirectory ์์___init__.py๋ง๋ค์ด์ผ ํจ!
Templates
templates > _nav.html
Last updated
Was this helpful?