๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring Boot SQL ์„ค์ •(hibernate, logging)

by ์ฃผ๋ฐœ2 2022. 3. 12.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž Spring Boot SQL ์„ค์ •(hibernate, logging)

์•ˆ๋…•ํ•˜์„ธ์š”, Spring Boot์˜ hibernate SQL ์„ค์ •์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์‹ค์ œ ์šด์˜์„ ํ•˜๋‹ค๋ณด๋ฉด ์ˆ˜ํ–‰๋œ ์ฟผ๋ฆฌ๋ฌธ์— ๋Œ€ํ•ด ํŒŒ์•…ํ•˜๊ณ , DB์—์„œ ๊ฒฐ๊ณผ๊ฐ’์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์„๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ, ๋กœ๊ทธ ํŒŒ์ผ์— ์ฟผ๋ฆฌ๋ฌธ์ด ์ €์žฅ์ด ๋˜์–ด์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์„ ๋ณต๋ถ™ํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ๊ฒฐ๊ณผ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฌธ์— ๋Œ€ํ•ด ๋กœ๊ทธ ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค๋ฉด ์šฉ๋Ÿ‰ ๋“ฑ์˜ ์ด์Šˆ๊ฐ€ ์žˆ์„์ˆ˜๋„ ์žˆ๊ฒ ์ฃ ? 

 

ํŠนํžˆ ๋ณต์žกํ•˜๊ฒŒ ์ˆ˜ํ–‰๋˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์— ์กด์žฌํ•  ๋•Œ, ์ด๋ฅผ ํ•˜๋‚˜์”ฉ ๋ถ„์„ํ•˜์—ฌ DB์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ์—๋Š” ๊ฝค๋‚˜ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์ด ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ์š” ์ด๋Ÿด๋•Œ ๊ต‰์žฅํžˆ ํŽธํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

StringBuilder sb = new StringBuilder(some_appropriate_size);
sb.append("select id1, id2, id3, id4, ... ");
sb.append(id2);
...

sb.append(" from ");
...

sb.append(" where ");
...

sb.append(" order by ");
...
return sb.toString();

ํŠนํžˆ ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฝ์šฐ, ์œ„์™€ ๊ฐ™์ด StringBuilder๋ฅผ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฌธ์„ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋„ ์žˆ๋Š”๋ฐ, ์ด์™€ ๊ฐ™์„๋•Œ ์‹ค์ œ ์ˆ˜ํ–‰๋˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ํŒŒ์•…ํ•˜๋ ค๋ฉด.. ๋ง‰๋ง‰ํ•ฉ๋‹ˆ๋‹ค์ž‰

์š”์ฆˆ์Œ, ๋กœ๊ทธ ํŒŒ์ผ์ด ์ •๋ง ์ค‘์š”ํ•จ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋Š๋ผ๊ณ  ์žˆ๊ณ , ํŒŒ์ผ์— ์ •ํ™•ํ•œ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ ๋˜ํ•œ ์ค‘์š”ํ•œ๊ฑธ ์ฒดํ—˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ  SQL ์„ค์ •

hibernate.show_sql

spring.jpa.properties.hibernate.show_sql=true

spring.jpa.properties.hibernate.show-sql=true

https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html

 

  • Hibernate๊ฐ€ DB์— ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฌธ์„ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋””ํดํŠธ ๊ฐ’์€ false ์ž…๋‹ˆ๋‹ค.
  •  ์œ„ ์„ค์ •์€ org.hibernate.SQL๋ฅผ debug๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜์ง€๋งŒ, show_sql์˜ ๊ฒฝ์šฐ System.out์— ์ถœ๋ ฅํ•˜๋Š” ๋ฐ˜๋ฉด org.hibernate.sql์˜ ๊ฒฝ์šฐ logger์— ์ถœ๋ ฅ์„ ํ•˜๋Š” ์ฐจ์ด์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊ณต์‹๋ฌธ์„œ์—๋Š” show_sql ์œผ๋กœ ๋‚˜์™€์žˆ์œผ๋‚˜ ์‹ค์ œ properties(yml) ํŒŒ์ผ์—๋Š” ์ž๋™์™„์„ฑ์ด show-sql๋กœ ๋˜์–ด์žˆ๋Š”๋ฐ์š”, show-sql๊ณผ show_sql ๋‘˜ ์ค‘ ์–ด๋Š๊ฒƒ์„ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์ด ์—†์Šต๋‹ˆ๋‹ค.

logging.level.org.hibernate.SQL=debug

 

์‹ค์ œ ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด System.out๊ณผ logger์— ์ถœ๋ ฅ์„ ํ•˜๋ฏ€๋กœ ์ฐจ์ด์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • show_sql=true

 

  • org.hibernate.SQL=debug

 

logger์˜ ๊ฒฝ์šฐ ์‹ค์ œ ์ˆ˜ํ–‰๋˜๋Š” ์‹œ๊ฐ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ฐœ์ธ์ ์œผ๋กœ๋Š” org.hibernate.SQL=debug ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

 

hibernate.format_sql

spring.jpa.properties.hibernate.format_sql=true

https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html

  • ๋กœ๊ทธ, ์ฝ˜์†”์˜ SQL์„ ์ข€ ๋” ์ด์˜๊ฒŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ ์œ„ ์„ค์ •์ธ show_sql์ด false๋ผ๋ฉด ์ถœ๋ ฅ๋˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ, ํ•„์š”์—†๋Š” ์„ค์ •์ž…๋‹ˆ๋‹ค.
  • ๋””ํดํŠธ ๊ฐ’์€ false ์ž…๋‹ˆ๋‹ค.

format_sql = true
format_sql = false

์œ„ ๋‘ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด format_sql ์„ค์ •์„ true๋กœ ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋”ฑ๋ด๋„ ํ•œ๋ˆˆ์— ๋งค์šฐ ๋ณด๊ธฐ๊ฐ€ ์ข‹์€๋ฐ์š”, ์ด์ฒ˜๋Ÿผ ๋ฐ˜๋“œ์‹œ ์„ค์ •์„ true๋กœ ํ•ด์•ผ๋˜์ง€ ์•Š์„๊นŒ?๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ์—ˆ์œผ๋‚˜ ์ œ๊ฐ€ ์ฃผ๋กœ ์ฐธ์กฐํ•˜๋Š” Baeldung์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์•ผ๊ธฐ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

"While this is extremely simple, it's not recommended, as it directly unloads everything to standard output without any optimizations of a logging framework."

์œ„ ๋ฌธ์žฅ์„ ๋ฒˆ์—ญํ•ด๋ณด์ž๋ฉด, "์ด ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‚˜ ๋กœ๊น… ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์–ด๋– ํ•œ ์ตœ์ ํ™”๋„ ์—†์ด ๋ชจ๋“  ๊ฒƒ์„ ํ‘œ์ค€ ์ถœ๋ ฅ(standard output)์œผ๋กœ ์ง์ ‘ unloadํ•˜๋ฏ€๋กœ ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค." ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€๋…์„ฑ ์ข‹๊ฒŒ ๊ฐœํ–‰์„ ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‹น์—ฐํžˆ ๋กœ๊ทธ์— ๊ธฐ๋ก๋˜๋Š” ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰์ด๋‚˜ ๋ผ์ธ์€ ๋” ๊ธธ์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–ธ์ œ๋‚˜ ๊ทธ๋ ‡๋“ฏ(?) ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๐Ÿ˜‚

 

hibernate.highlight_sql

spring.jpa.properties.hibernate.highlight_sql=true

  • SQL ์ถœ๋ ฅ์„ ANSI escape codes๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ‰์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

 

hibernate.use_sql_comments

spring.jpa.properties.hibernate.use_sql_comments=true

  • ๋ณด๋‹ค ์‰ฌ์šด ๋””๋ฒ„๊น…์„ ์œ„ํ•ด SQL ๋‚ด๋ถ€์— /* */์˜ ์ฃผ์„์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

hibernate.type.descriptor.sql=trace

logging.level.org.hibernate.type.descriptor.sql=trace
  • ์ฟผ๋ฆฌ๋ฌธ ๋กœ๊ทธ์— ์ถœ๋ ฅ๋˜์–ด ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ(?)์— ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • trace๋กœ ์„ค์ •ํ•ด์•ผ ๋ฐ”์ธ๋”ฉ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • binding parameter

 

 

๐Ÿ“Œ  ์ •๋ฆฌ

์œ„์—์„œ ์„ค์ •ํ•œ ์˜ต์…˜๋“ค์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// properties
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.highlight_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace

// yml
spring:
  jpa:
    properties:
      hibernate:
        format_sql: true
        highlight_sql: true
        use_sql_comments: true
logging:
  level:
    org:
      hibernate:
        SQL: debug
        type:
          descriptor:
            sql: trace

์ฐธ๊ณ ๋กœ properties ํ™•์žฅ์ž์˜ ํŒŒ์ผ๊ณผ yml ํ™•์žฅ์ž์˜ ํŒŒ์ผ๊ฐ„ ๋ณ€ํ™˜์€ ๋‹ค์Œ ์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„์—๋Š” SQL๊ณผ ๊ด€๋ จ๋œ hibernate ์„ค์ • ์˜ต์…˜๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ์š”, ๊ณต์‹๋ฌธ์„œ์—๋Š” ๊ต‰์žฅํžˆ ๋งŽ์€ ์„ค์ •๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋™์•ˆ show_sql๊ณผ format_sql์˜ ๊ฐ’๋งŒ ์„ค์ •ํ•ด์„œ ๋Œ€๋žต์ ์œผ๋กœ ์‚ฌ์šฉ์„ ํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ์„ค์ •๊ฐ’๋“ค์„ ๋ณด๋‹ค ์ž์„ธํžˆ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋น„์Šทํ•œ๋“ฏ ๋น„์Šทํ•˜์ง€์•Š์€ ์„ค์ •๋“ค์ด ์žˆ์–ด์„œ ๊ฝค ํ—ท๊ฐˆ๋ ธ์—ˆ๋Š”๋ฐ์š”, ํŠนํžˆ ์ฟผ๋ฆฌ๋ฌธ์„ ๋กœ๊น…ํ•˜๊ฑฐ๋‚˜ ๋ฐ”์ธ๋”ฉ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋กœ๊น…ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€