# All files / utils queryOptimizer.js

0% Statements 0/120
0% Branches 0/51
0% Functions 0/20
0% Lines 0/116

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363

     // utils/queryOptimizer.js - 데이터베이스 쿼리 최적화 유틸리티

    const { getDb } = require('../dbPool');

    /**
     * 페이지네이션 헬퍼
     */
    const paginate = (page = 1, limit = 10) => {
      const pageNum = Math.max(1, parseInt(page));
      const limitNum = Math.min(100, Math.max(1, parseInt(limit))); // 최대 100개 제한
      const offset = (pageNum - 1) * limitNum;

      return {
        limit: limitNum,
        offset,
        page: pageNum
      };
    };

    /**
     * 페이지네이션된 쿼리 실행
     */
    const executePagedQuery = async (baseQuery, countQuery, params = [], options = {}) => {
      const { page = 1, limit = 10, orderBy = 'id', orderDirection = 'DESC' } = options;
      const { limit: limitNum, offset, page: pageNum } = paginate(page, limit);

      try {
        const db = await getDb();

        // 전체 개수 조회
        const [countResult] = await db.execute(countQuery, params);
        const totalCount = countResult[0]?.total || 0;

        // 데이터 조회 (ORDER BY와 LIMIT 추가)
        const pagedQuery = `${baseQuery} ORDER BY ${orderBy} ${orderDirection} LIMIT ${limitNum} OFFSET ${offset}`;
        const [rows] = await db.execute(pagedQuery, params);

        // 페이지네이션 메타데이터 계산
        const totalPages = Math.ceil(totalCount / limitNum);

        return {
          data: rows,
          pagination: {
            currentPage: pageNum,
            totalPages,
            totalCount,
            limit: limitNum,
            hasNextPage: pageNum < totalPages,
            hasPrevPage: pageNum > 1
          }
        };

      } catch (error) {
        throw new Error(`페이지네이션 쿼리 실행 오류: ${error.message}`);
      }
    };

    /**
     * 인덱스 최적화 제안
     */
    const suggestIndexes = async (tableName) => {
      try {
        const db = await getDb();

        // 현재 인덱스 조회
        const [indexes] = await db.execute(`SHOW INDEX FROM ${tableName}`);

        // 테이블 구조 조회
        const [columns] = await db.execute(`DESCRIBE ${tableName}`);

        const suggestions = [];

        // 외래키 컬럼에 인덱스 제안
        const foreignKeyColumns = columns.filter(col =>
          col.Field.endsWith('_id') && !indexes.some(idx => idx.Column_name === col.Field)
        );

        foreignKeyColumns.forEach(col => {
          suggestions.push({
            type: 'INDEX',
            column: col.Field,
            reason: '외래키 컬럼에 인덱스 추가로 JOIN 성능 향상',
            sql: `CREATE INDEX idx_${tableName}_${col.Field} ON ${tableName}(${col.Field});`
          });
        });

        // 날짜 컬럼에 인덱스 제안
        const dateColumns = columns.filter(col =>
          (col.Type.includes('date') || col.Type.includes('timestamp')) &&
          !indexes.some(idx => idx.Column_name === col.Field)
        );

        dateColumns.forEach(col => {
          suggestions.push({
            type: 'INDEX',
            column: col.Field,
            reason: '날짜 범위 검색 성능 향상',
            sql: `CREATE INDEX idx_${tableName}_${col.Field} ON ${tableName}(${col.Field});`
          });
        });

        return {
          tableName,
          currentIndexes: indexes.map(idx => ({
            name: idx.Key_name,
            column: idx.Column_name,
            unique: idx.Non_unique === 0
          })),
          suggestions
        };

      } catch (error) {
        throw new Error(`인덱스 분석 오류: ${error.message}`);
      }
    };

    /**
     * 쿼리 성능 분석
     */
    const analyzeQuery = async (query, params = []) => {
      try {
        const db = await getDb();

        // EXPLAIN 실행
        const explainQuery = `EXPLAIN ${query}`;
        const [explainResult] = await db.execute(explainQuery, params);

        // 쿼리 실행 시간 측정
        const startTime = Date.now();
        await db.execute(query, params);
        const executionTime = Date.now() - startTime;

        // 성능 분석
        const analysis = {
          executionTime,
          explainResult,
          recommendations: []
        };

        // 성능 권장사항 생성
        explainResult.forEach(row => {
          if (row.type === 'ALL') {
            analysis.recommendations.push({
              type: 'WARNING',
              message: `테이블 전체 스캔 발생: ${row.table}`,
              suggestion: '적절한 인덱스 추가 권장'
            });
          }

          if (row.rows > 1000) {
            analysis.recommendations.push({
              type: 'WARNING',
              message: `많은 행 검사: ${row.rows}행`,
              suggestion: 'WHERE 조건 최적화 또는 인덱스 추가 권장'
            });
          }

          if (row.Extra && row.Extra.includes('Using filesort')) {
            analysis.recommendations.push({
              type: 'INFO',
              message: '파일 정렬 사용 중',
              suggestion: 'ORDER BY 컬럼에 인덱스 추가 고려'
            });
          }
        });

        return analysis;

      } catch (error) {
        throw new Error(`쿼리 분석 오류: ${error.message}`);
      }
    };

    /**
     * 배치 삽입 최적화
     */
    const batchInsert = async (tableName, data, batchSize = 100) => {
      if (!Array.isArray(data) || data.length === 0) {
        throw new Error('삽입할 데이터가 없습니다.');
      }

      try {
        const db = await getDb();
        const connection = await db.getConnection();

        await connection.beginTransaction();

        const columns = Object.keys(data[0]);
        const placeholders = columns.map(() => '?').join(', ');
        const insertQuery = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;

        let insertedCount = 0;

        // 배치 단위로 처리
        for (let i = 0; i < data.length; i += batchSize) {
          const batch = data.slice(i, i + batchSize);

          for (const row of batch) {
            const values = columns.map(col => row[col]);
            await connection.execute(insertQuery, values);
            insertedCount++;
          }
        }

        await connection.commit();
        connection.release();

        return {
          insertedCount,
          batchSize,
          totalBatches: Math.ceil(data.length / batchSize)
        };

      } catch (error) {
        throw new Error(`배치 삽입 오류: ${error.message}`);
      }
    };

    /**
     * 쿼리 캐시 키 생성
     */
    const generateCacheKey = (query, params = [], prefix = 'query') => {
      const paramString = params.length > 0 ? JSON.stringify(params) : '';
      const queryHash = require('crypto')
        .createHash('md5')
        .update(query + paramString)
        .digest('hex');

      return `${prefix}:${queryHash}`;
    };

    /**
     * 자주 사용되는 최적화된 쿼리들
     */
    const optimizedQueries = {
      // 작업자 목록 (페이지네이션)
      getWorkersPaged: async (page = 1, limit = 10, search = '') => {
        let baseQuery = `
          SELECT w.*, COUNT(dwr.id) as report_count
          FROM workers w
          LEFT JOIN daily_work_reports dwr ON w.worker_id = dwr.worker_id
        `;

        let countQuery = 'SELECT COUNT(*) as total FROM workers w';
        let params = [];

        if (search) {
          const searchCondition = ' WHERE w.worker_name LIKE ? OR w.position LIKE ?';
          baseQuery += searchCondition + ' GROUP BY w.worker_id';
          countQuery += searchCondition;
          params = [`%${search}%`, `%${search}%`];
        } else {
          baseQuery += ' GROUP BY w.worker_id';
        }

        return executePagedQuery(baseQuery, countQuery, params, {
          page, limit, orderBy: 'w.worker_id', orderDirection: 'DESC'
        });
      },

      // 프로젝트 목록 (페이지네이션)
      getProjectsPaged: async (page = 1, limit = 10, status = '') => {
        let baseQuery = `
          SELECT p.*, COUNT(dwr.id) as report_count,
                 SUM(dwr.work_hours) as total_hours
          FROM projects p
          LEFT JOIN daily_work_reports dwr ON p.project_id = dwr.project_id
        `;

        let countQuery = 'SELECT COUNT(*) as total FROM projects p';
        let params = [];

        if (status) {
          const statusCondition = ' WHERE p.status = ?';
          baseQuery += statusCondition + ' GROUP BY p.project_id';
          countQuery += statusCondition;
          params = [status];
        } else {
          baseQuery += ' GROUP BY p.project_id';
        }

        return executePagedQuery(baseQuery, countQuery, params, {
          page, limit, orderBy: 'p.project_id', orderDirection: 'DESC'
        });
      },

      // 일일 작업 보고서 (날짜 범위, 페이지네이션)
      getDailyWorkReportsPaged: async (startDate, endDate, page = 1, limit = 10) => {
        const baseQuery = `
          SELECT dwr.*, w.worker_name, p.project_name,
                 wt.name as work_type_name, wst.name as work_status_name,
                 et.name as error_type_name, u.name as created_by_name
          FROM daily_work_reports dwr
          LEFT JOIN workers w ON dwr.worker_id = w.worker_id
          LEFT JOIN projects p ON dwr.project_id = p.project_id
          LEFT JOIN work_types wt ON dwr.work_type_id = wt.id
          LEFT JOIN work_status_types wst ON dwr.work_status_id = wst.id
          LEFT JOIN error_types et ON dwr.error_type_id = et.id
          LEFT JOIN users u ON dwr.created_by = u.user_id
          WHERE dwr.report_date BETWEEN ? AND ?
        `;

        const countQuery = `
          SELECT COUNT(*) as total
          FROM daily_work_reports dwr
          WHERE dwr.report_date BETWEEN ? AND ?
        `;

        return executePagedQuery(baseQuery, countQuery, [startDate, endDate], {
          page, limit, orderBy: 'dwr.report_date', orderDirection: 'DESC'
        });
      }
    };

    /**
     * 데이터베이스 성능 모니터링
     */
    const getPerformanceStats = async () => {
      try {
        const db = await getDb();

        // 연결 상태 조회
        const [connections] = await db.execute('SHOW STATUS LIKE "Threads_connected"');
        const [maxConnections] = await db.execute('SHOW VARIABLES LIKE "max_connections"');

        // 쿼리 캐시 상태 (MySQL 8.0 이전 버전)
        let queryCacheStats = null;
        try {
          const [qcStats] = await db.execute('SHOW STATUS LIKE "Qcache%"');
          queryCacheStats = qcStats;
        } catch (error) {
          // MySQL 8.0+에서는 쿼리 캐시가 제거됨
        }

        // 슬로우 쿼리 로그 상태
        const [slowQueries] = await db.execute('SHOW STATUS LIKE "Slow_queries"');

        return {
          connections: {
            current: parseInt(connections[0]?.Value || 0),
            max: parseInt(maxConnections[0]?.Value || 0)
          },
          queryCacheStats,
          slowQueries: parseInt(slowQueries[0]?.Value || 0),
          timestamp: new Date().toISOString()
        };

      } catch (error) {
        throw new Error(`성능 통계 조회 오류: ${error.message}`);
      }
    };

    module.exports = {
      paginate,
      executePagedQuery,
      suggestIndexes,
      analyzeQuery,
      batchInsert,
      generateCacheKey,
      optimizedQueries,
      getPerformanceStats
    };