# 지연로딩, 즉시로딩과 N+1 문제

즉시 로딩을 할 경우 예상치 못한 SQL이 발생하여 N+1문제가 발생한다.

지연 로딩을 할 경우, 일단 프록시 객체로 사용하다가 실제 그 객체를 사용할 때에 프록시 객체가 초기화되면서 DB로 조회하여 실제 엔티티 값을 가져온다.

Member와 연관관계인 Team 객체가 있다고 하자.\
Member를 조회할 때 Team을 즉시 로딩하면 한번의 쿼리로 Member, Team 데이터 둘 다 가져올 수 있다.\
하지만 Team을 지연 로딩하면 Team 객체를 사용할 때 DB 조회 쿼릭가 한번 더 나간다.

지연 로딩에서 하나의 쿼리에 대해 N개의 쿼리가 각각 따로 실행되는 것이다.\
이러한 문제를 N+1 문제라고 한다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/jpa-2-api/3.-api/n+1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
