N+1 Problem — Concept Check
Questions on N+1 Problem. Loading a list of entities and accessing a lazy association triggers one SELECT per entity; detect with Hibernate statistics or slow query log; fix with JOIN FETCH or entity graphs.
Questions in this quiz
8 questions on N+1 Problem — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a list of 1000 orders and each order has a lazy-loaded customer association. What is the consequence of accessing the customer for each order without optimization?
- A100 SELECT queries will be executed.
- B1001 SELECT queries will be executed.
- C2000 SELECT queries will be executed.
- D1 SELECT query will be executed.
2.A developer uses a JPQL query to fetch orders and their customers with JOIN FETCH. What might be a side effect if the customer association is a collection?
- AThe query will fail due to syntax errors.
- BThe query will execute faster than expected.
- CThe query will ignore the customer association.
- DA Cartesian product may occur, leading to duplicate orders.
3.What is a common way to detect the N+1 problem in a Hibernate application?
- AIncrease the application server memory.
- BDisable lazy loading for all associations.
- CUse a different database engine.
- DEnable Hibernate statistics and check the query count.
4.In a Spring Data JPA repository, how can you optimize a query to avoid the N+1 problem without modifying JPQL?
- AIncrease the database connection pool size.
- BUse @EntityGraph to specify fetch associations.
- CDisable caching for the entity.
- DUse a native SQL query instead.
5.A developer notices high latency in production due to the N+1 problem. Which approach should they take to resolve it?
- AIncrease the server's CPU resources.
- BUse a batch processing framework.
- CUse JOIN FETCH in the query to load associations.
- DSwitch to a different ORM framework.
6.When using JPQL, why might an EAGER association still cause an N+1 problem?
- AEAGER loading only applies to EntityManager.find(), not JPQL queries.
- BEAGER associations are never loaded in JPQL.
- CJPQL automatically converts EAGER to LAZY.
- DEAGER loading is always disabled in JPQL.
7.What is the primary cause of the N+1 problem in a JPA/Hibernate application?
- AAccessing a lazy-loaded association on each entity in a collection.
- BUsing too many database connections simultaneously.
- CHaving too many entities in the database.
- DIncorrectly configured transaction settings.
8.Which of the following is a valid fix for the N+1 problem when fetching a list of orders with customers?
- AIncrease the database timeout settings.
- BUse a single-threaded execution model.
- CDisable logging in the application.
- DUse JOIN FETCH in the JPQL query.