Skip to content

Commit

Permalink
refactor: Explain과 Explain Anaylze 쿼리 실행하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Aug 26, 2024
1 parent a0aac58 commit c37ef36
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.few.api.repo.explain

import org.jooq.DSLContext
import org.jooq.Query

class ExplainGenerator {
companion object {
/**
* Execute EXPLAIN and EXPLAIN ANALYZE FORMAT=TREE
*/
fun execute(dslContext: DSLContext, query: Query): String {
val sql = query.sql
val values = query.bindValues
mapSqlAndValues(sql, values).let {
val explain = StringBuilder()
explain.append("EXPLAIN $it\n")
explain.append("\n")
dslContext.fetch("EXPLAIN $it").let {
explain.append(it)
}
explain.append("\n\n")
explain.append("EXPLAIN ANALYZE FORMAT=TREE $it\n")
explain.append("\n")
dslContext.fetch("EXPLAIN ANALYZE FORMAT=TREE $it").let {
it.forEach { record ->
explain.append(record[0].toString())
}
}
return explain.toString()
}
}

/**
* Execute EXPLAIN
*/
fun explain(dslContext: DSLContext, query: Query): String {
val sql = query.sql
val values = query.bindValues
mapSqlAndValues(sql, values).let {
val explain = StringBuilder()
explain.append("EXPLAIN $it\n")
explain.append("\n")
dslContext.fetch("EXPLAIN $it").let {
explain.append(it)
}
return explain.toString()
}
}

/**
* Execute EXPLAIN ANALYZE FORMAT=TREE
*/
fun analyzeFormatTree(dslContext: DSLContext, query: Query): String {
val sql = query.sql
val values = query.bindValues
mapSqlAndValues(sql, values).let {
val explain = StringBuilder()
explain.append("EXPLAIN ANALYZE FORMAT=TREE $it\n")
explain.append("\n")
dslContext.fetch("EXPLAIN ANALYZE FORMAT=TREE $it").let {
it.forEach { record ->
explain.append(record[0].toString())
}
}
return explain.toString()
}
}

private fun mapSqlAndValues(sql: String, values: List<Any>) =
values.foldIndexed(sql) { index, acc, value ->
if (value is String) {
acc.replaceFirst("?", "'$value'")
} else {
acc.replaceFirst("?", "$value")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.few.api.repo.dao.article.query.SelectArticleIdByWorkbookIdAndDayQuery
import com.few.api.repo.dao.article.query.SelectArticleRecordQuery
import com.few.api.repo.dao.article.query.SelectWorkBookArticleRecordQuery
import com.few.api.repo.dao.article.query.SelectWorkbookMappedArticleRecordsQuery
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
Expand Down Expand Up @@ -63,7 +64,7 @@ class ArticleDaoExplainGenerateTest : JooqTestSpec() {
articleDao.selectArticleRecordQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectArticleRecordQueryExplain")
}
Expand All @@ -74,7 +75,7 @@ class ArticleDaoExplainGenerateTest : JooqTestSpec() {
articleDao.selectWorkBookArticleRecordQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectWorkBookArticleRecordQueryExplain")
}
Expand All @@ -85,7 +86,7 @@ class ArticleDaoExplainGenerateTest : JooqTestSpec() {
articleDao.selectWorkbookMappedArticleRecordsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectWorkbookMappedArticleRecordsQueryExplain")
}
Expand Down Expand Up @@ -130,7 +131,7 @@ class ArticleDaoExplainGenerateTest : JooqTestSpec() {
articleDao.selectArticleIdByWorkbookIdAndDayQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectArticleIdByWorkbookIdAndDayQueryExplain")
}
Expand All @@ -141,7 +142,7 @@ class ArticleDaoExplainGenerateTest : JooqTestSpec() {
articleDao.selectArticleContentsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectArticleContentsQueryExplain")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.few.api.repo.dao.article.ArticleMainCardDao
import com.few.api.repo.dao.article.command.ArticleMainCardExcludeWorkbookCommand
import com.few.api.repo.dao.article.command.UpdateArticleMainCardWorkbookCommand
import com.few.api.repo.dao.article.command.WorkbookCommand
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
import com.few.data.common.code.CategoryType
Expand Down Expand Up @@ -59,7 +61,7 @@ class ArticleMainCardDaoExplainGenerateTest : JooqTestSpec() {
fun selectArticleMainCardsRecordQueryExplain() {
val query = articleMainCardDao.selectArticleMainCardsRecordQuery(setOf(1L))

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)
ResultGenerator.execute(query, explain, "selectArticleMainCardsRecordQueryExplain")
}

Expand All @@ -80,7 +82,7 @@ class ArticleMainCardDaoExplainGenerateTest : JooqTestSpec() {
articleMainCardDao.insertArticleMainCardCommand(it)
}

val explain = command.toString()
val explain = InsertUpdateExplainGenerator.execute(dslContext, command.sql, command.bindValues)

ResultGenerator.execute(command, explain, "insertArticleMainCardCommandExplain")
}
Expand All @@ -103,7 +105,7 @@ class ArticleMainCardDaoExplainGenerateTest : JooqTestSpec() {
articleMainCardDao.updateArticleMainCardSetWorkbookCommand(it)
}

val explain = command.toString()
val explain = InsertUpdateExplainGenerator.execute(dslContext, command.sql, command.bindValues)

ResultGenerator.execute(command, explain, "updateArticleMainCardSetWorkbookCommandExplain")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.few.api.repo.dao.article.command.ArticleViewCountCommand
import com.few.api.repo.dao.article.query.ArticleViewCountQuery
import com.few.api.repo.dao.article.query.SelectArticlesOrderByViewsQuery
import com.few.api.repo.dao.article.query.SelectRankByViewsQuery
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
Expand Down Expand Up @@ -46,7 +47,7 @@ class ArticleViewCountDaoExplainGenerateTest : JooqTestSpec() {
articleViewCountDao.selectArticleViewCountQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)
ResultGenerator.execute(query, explain, "selectArticleViewCountQueryExplain")
}

Expand Down Expand Up @@ -84,7 +85,7 @@ class ArticleViewCountDaoExplainGenerateTest : JooqTestSpec() {
articleViewCountDao.selectRankByViewsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)
ResultGenerator.execute(query, explain, "selectRankByViewsQueryExplain")
}

Expand All @@ -97,7 +98,7 @@ class ArticleViewCountDaoExplainGenerateTest : JooqTestSpec() {
articleViewCountDao.selectArticlesOrderByViewsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)
ResultGenerator.execute(query, explain, "selectArticlesOrderByViewsQueryExplain")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.few.api.repo.explain.article
import com.few.api.repo.dao.article.ArticleViewHisDao
import com.few.api.repo.dao.article.command.ArticleViewHisCommand
import com.few.api.repo.dao.article.query.ArticleViewHisCountQuery
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
Expand Down Expand Up @@ -42,7 +43,7 @@ class ArticleViewHisDaoExplainGenerateTest : JooqTestSpec() {
articleViewHisDao.countArticleViewsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)
ResultGenerator.execute(query, explain, "selectArticleViewCountQueryExplain")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ import com.few.api.repo.dao.member.query.BrowseWorkbookWritersQuery
import com.few.api.repo.dao.member.query.SelectMemberByEmailNotConsiderDeletedAtQuery
import com.few.api.repo.dao.member.query.SelectMemberByEmailQuery
import com.few.api.repo.dao.member.query.SelectWriterQuery
import com.few.api.repo.dao.member.support.WriterDescription
import com.few.api.repo.dao.member.support.WriterDescriptionJsonMapper
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
import com.few.data.common.code.MemberType
import io.github.oshai.kotlinlogging.KotlinLogging
import jooq.jooq_dsl.tables.Member
import org.jooq.DSLContext
import org.jooq.JSON
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL

@Tag("explain")
class MemberDaoExplainGenerateTest : JooqTestSpec() {
Expand All @@ -39,47 +35,13 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
@Autowired
private lateinit var writerDescriptionJsonMapper: WriterDescriptionJsonMapper

@BeforeEach
fun setUp() {
log.debug { "===== start setUp =====" }
dslContext.deleteFrom(Member.MEMBER).execute()
dslContext.insertInto(Member.MEMBER)
.set(Member.MEMBER.ID, 1)
.set(Member.MEMBER.EMAIL, "[email protected]")
.set(Member.MEMBER.TYPE_CD, MemberType.NORMAL.code)
.execute()

val writerDescription = writerDescriptionJsonMapper.toJson(
WriterDescription(
"few2",
URL("http://localhost:8080/writers/url2"),
URL("https://github.com/user-attachments/assets/28df9078-488c-49d6-9375-54ce5a250742")
)
)

dslContext.insertInto(Member.MEMBER)
.set(Member.MEMBER.ID, 2)
.set(Member.MEMBER.EMAIL, "[email protected]")
.set(Member.MEMBER.TYPE_CD, MemberType.WRITER.code)
.set(Member.MEMBER.DESCRIPTION, JSON.valueOf(writerDescription))
.execute()

dslContext.insertInto(Member.MEMBER)
.set(Member.MEMBER.ID, 3)
.set(Member.MEMBER.EMAIL, "[email protected]")
.set(Member.MEMBER.TYPE_CD, MemberType.WRITER.code)
.set(Member.MEMBER.DESCRIPTION, JSON.valueOf(writerDescription))
.execute()
log.debug { "===== finish setUp =====" }
}

@Test
fun selectWriterQueryExplain() {
val query = SelectWriterQuery(1).let {
memberDao.selectWriterQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectWriterQueryExplain")
}
Expand All @@ -90,7 +52,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
memberDao.selectWritersQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectWritersQueryExplain")
}
Expand All @@ -101,7 +63,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
memberDao.selectWritersQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectWritersQueryExplainByWorkbookIds")
}
Expand All @@ -112,7 +74,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
memberDao.selectMemberByEmailQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectMemberByEmailQueryExplainExplain")
}
Expand All @@ -123,7 +85,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
memberDao.selectMemberByEmailQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectMemberByEmailNotConsiderDeletedAtQueryExplain")
}
Expand All @@ -148,7 +110,7 @@ class MemberDaoExplainGenerateTest : JooqTestSpec() {
memberDao.selectMemberIdAndTypeQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectMemberIdAndTypeQueryExplain")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.few.api.repo.dao.problem.query.SelectProblemsByArticleIdQuery
import com.few.api.repo.dao.problem.support.Content
import com.few.api.repo.dao.problem.support.Contents
import com.few.api.repo.dao.problem.support.ContentsJsonMapper
import com.few.api.repo.explain.ExplainGenerator
import com.few.api.repo.explain.InsertUpdateExplainGenerator
import com.few.api.repo.explain.ResultGenerator
import com.few.api.repo.jooq.JooqTestSpec
Expand Down Expand Up @@ -69,7 +70,7 @@ class ProblemDaoExplainGenerateTest : JooqTestSpec() {
problemDao.selectProblemContentsQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectProblemContentsQueryExplain")
}
Expand All @@ -80,7 +81,7 @@ class ProblemDaoExplainGenerateTest : JooqTestSpec() {
problemDao.selectProblemAnswerQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectProblemAnswerQueryExplain")
}
Expand All @@ -91,7 +92,7 @@ class ProblemDaoExplainGenerateTest : JooqTestSpec() {
problemDao.selectProblemsByArticleIdQuery(it)
}

val explain = dslContext.explain(query).toString()
val explain = ExplainGenerator.execute(dslContext, query)

ResultGenerator.execute(query, explain, "selectProblemsByArticleIdQueryExplain")
}
Expand Down
Loading

0 comments on commit c37ef36

Please sign in to comment.