TIL91. Django ORM : Logging & Lazy Loading

ID์งฑ์žฌยท2021๋…„ 11์›” 20์ผ
0

Django ORM

๋ชฉ๋ก ๋ณด๊ธฐ
5/7
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” query๋ฅผ loggingํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ Lazy Loading์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.


๐ŸŒˆ Logging & Lazy Loading

๐Ÿ”ฅ Logging ๋‹ค๋Š” ๋ฐฉ๋ฒ•

๐Ÿ”ฅ Lazy Loading ์ด๋ž€?


1. Logging

๐Ÿค” Django Logging ๋‹ค๋Š” ๋ฒ•

โœ”๏ธ settgins.py์— logging์„ ๋‹ฌ๋ฉด, django๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ๋ฐœ์ƒ๋˜๋Š” query์˜ ์–‘์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ”๏ธ ์ฆ‰, ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•œ API๊ฐ€ DB์— ์–ด๋–ค ์š”์ฒญ์„ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ ๋งˆ๋‹ค ์–ด๋–ค SQL๋ฌธ์ด ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , shell์—์„œ๋„ ์ž‘๋™ํ•œ๋‹ค.

โœ”๏ธ ์ด๋ฅผ ํ†ตํ•ด API์˜ ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๊ณ , ์ตœ์ ํ™”์— ๋Œ€ํ•œ ์ง€ํ‘œ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

โœ”๏ธ ์•„๋ž˜ ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ ์‹คํ–‰๋•Œ๋งˆ๋‹ค ์ฟผ๋ฆฌ๊ฐ€ ์ฐํžˆ๋Š” ์„ค์ •์ด๋‹ค.

#settings.py
LOGGING = {
    'disable_existing_loggers': False,
    'version': 1,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

โœ”๏ธ ์•„๋ž˜ ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ ์‹คํ–‰๋•Œ๋งˆ๋‹ค ์ฐํžŒ ์ฟผ๋ฆฌ๋ฅผ log ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์“ธ ๋•Œ์—๋Š” .gitignore ๋กœ๊ทธ ํŒŒ์ผ์ด ์ถ”์ ๋˜์ง€ ์•Š๊ฒŒ ์ถ”๊ฐ€์‹œ์ผœ์•ผํ•œ๋‹ค.

#settings.py
LOGGING = {
    'disable_existing_loggers': False,
    'version': 1,
    'formatters': {
         'verbose': {
            'format': '{asctime} {levelname} {message}',
            'style': '{'
        },
    },
    'handlers': {
        'console': {
            'class'     : 'logging.StreamHandler',
            'formatter' : 'verbose',
            'level'     : 'DEBUG',
        },
        'file': {
            'level'     : 'DEBUG',
            'class'     : 'logging.FileHandler',
            'formatter' : 'verbose',
            'filename'  : 'debug.log',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers' : ['console','file'],
            'level'    : 'DEBUG',
            'propagate': False,
        },
    },
}

2. Lazy Loading ์ด๋ž€?

๐Ÿค” Lazy Loading

โœ”๏ธ Django๋ฟ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ORM์—์„œ๋„ Lazy-loading๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. Lazy Loading์€ CS ์ „๋ฐ˜์˜ ๊ฐœ๋…์œผ๋กœ generator์˜ ๊ฐœ๋…๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

โœ”๏ธ Lazy-loading์ด๋ž€ ORM์—์„œ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค DB์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผํ•  ๋•Œ Query๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค.

โœ”๏ธ ์ฆ‰, DB์— ์š”์ฒญํ•˜๋Š” ๋กœ์ง์„ ๋งค๋ฒˆ ์‹คํ–‰์‹œ์ผœ DB์— ๋ฌด๋ฆฌ๋ฅผ ์ฃผ๋Š”๊ฒŒ ์•„๋‹Œ, ์ตœ์ข…์ ์œผ๋กœ Query๋ฌธ์ด ํ‰๊ฐ€๋˜์–ด์•ผํ•  ๋•Œ DB์— ์š”์ฒญํ•˜๋Š” ๊ฒŒ์œผ๋ฅธ ๋ฐฉ์‹์ด๋‹ค.

โœ”๏ธ ์‹ค์ œ๋กœ ํ˜ธ์ถœ(ํ‰๊ฐ€)ํ•˜๋Š” ์‹œ์  : slicing, Iteration(for๋ฌธ), if๋ฌธ, print(), repr(), len(), list(), bool() ๋“ฑ

โœ”๏ธ ์ด์— alll, filter, exclude, annotate๋ฌธ์ด ์กด์žฌํ•  ๋•Œ๋Š” ๋ฐ”๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š์ง€๋งŒ, print()๋ฌธ์ด ์žˆ์„ ๋•Œ๋Š” query๋ฅผ ์‹คํ–‰์‹œ์ผœ DB์— ์š”์ฒญํ•œ๋‹ค.

โœ”๏ธ ์•„๋ž˜ ์ฝ”๋“œ๋Š” query๋ฌธ์ด 4ํšŒ ํ˜ธ์ถœ๋œ๋‹ค.

query = User.objects.filter(name_startswith = "kim")
queyry[0]     # ๐Ÿ‘ˆ 1ํšŒ ํ˜ธ์ถœ
queyry[1]     # ๐Ÿ‘ˆ 2ํšŒ ํ˜ธ์ถœ
queyry[2]     # ๐Ÿ‘ˆ 3ํšŒ ํ˜ธ์ถœ
list(queyry)  # ๐Ÿ‘ˆ 4ํšŒ ํ˜ธ์ถœ

โœ”๏ธ ์•„๋ž˜ ์ฝ”๋“œ๋Š” query๋ฌธ์ด 1๋ฒˆ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— DB์— 1ํšŒ ํ˜ธ์ถœ๋œ๋‹ค. list()๋ฅผ ํ•  ๋•Œ query๊ฐ€ ์‹คํ–‰๋˜๋Š”๋ฐ list()๊ฐ€ ์ด๋ฏธ ๋ฆฌ์†Œ์Šค๋ฅผ ์บ์Šํ•ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์•„๋ž˜ slicing์—์„œ๋Š” ์บ์Šํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋‹ค์‹œ DB๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด query๋ฌธ์„ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.

query = User.objects.filter(name_startswith = "kim")
list(queyry) # ๐Ÿ‘ˆ 1ํšŒ ํ˜ธ์ถœ
queyry[0]
queyry[1]
queyry[2]
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€