Optimization

SQL๋ฌธ ์‹คํ–‰

  • Iteration

  • Slicing

    • ๊ธฐ๋ณธ์ ์œผ๋ก  x

    • step์„ ํ™œ์šฉํ•  ๋•Œ์—๋งŒ

      • [3:10:2]

  • repr

  • len

  • bool

Query ๊ฐœ์„ 

ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : django debug toolbar

installation

$ pip install django-debug-toolbar

Prerequisites

settings.py

Setting up URLconf

urls.py in the root directory

Enabling middleware

settings.py

Configuring Internal IPs

settings.py

0. ๊ด€๋ จ ๋ฌธ์„œ

1. ๋Œ“๊ธ€ ์ˆ˜ ์ถœ๋ ฅ - annotate() ํ™œ์šฉ

N+1 problem

๊ฐœ์„  ์ „ (11๋ฒˆ)

image-20200504205530356

๊ฐœ์„  ํ›„ (1๋ฒˆ)

image-20200504205710572

select_related๋Š” SQL JOIN์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค

1:1, 1:N ๊ด€๊ณ„์—์„œ ์ฐธ์กฐ๊ด€๊ณ„ (N -> 1, foreignkey๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š” ๊ณณ)

๊ฐœ์„  ์ „ (11๋ฒˆ)

image-20200504210229746

๊ฐœ์„  ํ›„ (1๋ฒˆ)

image-20200504210339287

prefetch_related๋Š” python์„ ํ†ตํ•œ join์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค

M:N, 1:N ๊ด€๊ณ„์—์„œ ์—ญ์ฐธ์กฐ ๊ด€๊ณ„ (1->N)

๊ฐœ์„  ์ „ (11๋ฒˆ)

image-20200504210658055

๊ฐœ์„  ํ›„ (2๋ฒˆ)

image-20200504210808430

4. ๊ฒŒ์‹œ๊ธ€๋งˆ๋‹ค ์ž‘์„ฑ์ž ์ด๋ฆ„๊ณผ ๋Œ“๊ธ€๋“ค ์ถœ๋ ฅ

๊ฐœ์„  ์ „ (111๋ฒˆ)

image-20200504211024275

๊ฐœ์„  ํ›„ (2๋ฒˆ)

image-20200504211241178

SQL Join

How to join three tables in SQL with MySQL database example

ex)

+

Gravata ํ™œ์šฉํ•˜์—ฌ profile photo ์„ค์ •ํ•˜๊ธฐ

https://en.gravatar.com/site/implement/

๋ฐฉ๋ฒ• 1) @property ์„ค์ •

accounts > models.py

๋ฐฉ๋ฒ• 2) templatetags ๋งŒ๋“ค๊ธฐ

accounts > templatetags > gravatar.py

  • templatetags directory ์•ˆ์— ___init__.py ๋งŒ๋“ค์–ด์•ผ ํ•จ!

Templates

templates > _nav.html

Last updated

Was this helpful?