안녕하세요.
선택기간 중간 날짜들을 가져오고
테이블에서 시작일, 종료일을 구해 시작,종료일 사이의 날짜들을 가져오는 쿼리입니다.
동작은 잘 되지만 속도가 너무 느린데 개선 방법이 있을까요?
있다면 조언좀 부탁드리겠습니다.
쿼리입니다.
+ 불필요한 WHERE절과 테이블명은 변경했습니다.
+ A괄호 내 쿼리 조회속도는 65~73 msecs, B괄호 내 쿼리 조회속도는 1~2msecs 정도 나옵니다.)
SELECT DISTINCT A.SELECTED_DATE AS CALENDAR_DATE FROM ( SELECT * FROM (SELECT ADDDATE('2018-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) SELECTED_DATE FROM (SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0, (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1, (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2, (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3, (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) v WHERE SELECTED_DATE BETWEEN '2018-07-01' AND '2018-10-01' ) A JOIN (SELECT from_unixtime(A.SDATE, '%Y-%m-%d') AS SDATE , from_unixtime(A.EDATE, '%Y-%m-%d') AS EDATE FROM T_TABLE1 A UNION SELECT from_unixtime(A.SDATE, '%Y-%m-%d') AS SDATE , from_unixtime(A.EDATE, '%Y-%m-%d') AS EDATE FROM T_TABLE2 A UNION SELECT from_unixtime(A.SDATE, '%Y-%m-%d') AS SDATE , from_unixtime(A.EDATE, '%Y-%m-%d') AS EDATE FROM T_TABLE3 A UNION SELECT from_unixtime(A.SDATE, '%Y-%m-%d') AS SDATE , from_unixtime(A.EDATE, '%Y-%m-%d') AS EDATE FROM T_TABLE4 A ) B ON A.SELECTED_DATE IN( B.SDATE , B.EDATE ) WHERE A.SELECTED_DATE < UNIX_TIMESTAMP('2018-10-01')
A괄호 쿼리
SELECTED_DATE |
---|
2018-07-01 |
2018-07-02 |
2018-07-03 |
2018-07-04 |
2018-07-05 |
2018-07-06 |
2018-07-07 |
2018-07-08 |
2018-07-09 |
2018-07-10 |
2018-07-11 |
2018-07-12 |
2018-07-13 |
2018-07-14 |
2018-07-15 |
2018-07-16 |
2018-07-17 |
2018-07-18 |
2018-07-19 |
2018-07-20 |
2018-07-21 |
2018-07-22 |
2018-07-23 |
2018-07-24 |
2018-07-25 |
2018-07-26 |
2018-07-27 |
2018-07-28 |
2018-07-29 |
2018-07-30 |
2018-07-31 |
2018-08-01 |
2018-08-02 |
2018-08-03 |
2018-08-04 |
2018-08-05 |
2018-08-06 |
2018-08-07 |
2018-08-08 |
2018-08-09 |
2018-08-10 |
2018-08-11 |
2018-08-12 |
2018-08-13 |
2018-08-14 |
2018-08-15 |
2018-08-16 |
2018-08-17 |
2018-08-18 |
2018-08-19 |
2018-08-20 |
2018-08-21 |
2018-08-22 |
2018-08-23 |
2018-08-24 |
2018-08-25 |
2018-08-26 |
2018-08-27 |
2018-08-28 |
2018-08-29 |
2018-08-30 |
2018-08-31 |
2018-09-01 |
2018-09-02 |
2018-09-03 |
2018-09-04 |
2018-09-05 |
2018-09-06 |
2018-09-07 |
2018-09-08 |
2018-09-09 |
2018-09-10 |
2018-09-11 |
2018-09-12 |
2018-09-13 |
2018-09-14 |
2018-09-15 |
2018-09-16 |
2018-09-17 |
2018-09-18 |
2018-09-19 |
2018-09-20 |
2018-09-21 |
2018-09-22 |
2018-09-23 |
2018-09-24 |
2018-09-25 |
2018-09-26 |
2018-09-27 |
2018-09-28 |
2018-09-29 |
2018-09-30 |
2018-10-01 |
B괄호 쿼리
SDATE | EDATE |
---|---|
2018-07-30 | 2018-07-31 |
2018-08-02 | 2018-08-03 |
2018-08-07 | 2018-08-08 |
2018-08-09 | 2018-08-10 |
2018-08-14 | 2018-08-15 |
2018-08-16 | 2018-08-17 |
2018-08-29 | 2018-08-29 |
2018-09-03 | 2018-09-03 |
2018-09-10 | 2018-09-10 |
2018-09-17 | 2018-09-17 |
2018-09-24 | 2018-09-24 |
2018-07-25 | 2018-07-26 |
2018-07-27 | 2018-07-28 |
2018-08-20 | 2018-08-21 |
2018-07-28 | 2018-07-29 |
2018-08-06 | 2018-08-07 |
2018-08-07 | 2018-08-09 |
2018-08-10 | 2018-08-11 |
2018-08-13 | 2018-08-14 |
2018-08-17 | 2018-08-18 |
2018-08-27 | 2018-08-28 |
2018-08-27 | 2018-07-29 |
쿼리 결과 :
CALENDAR_DATE |
---|
2018-07-25 |
2018-07-26 |
2018-07-27 |
2018-07-28 |
2018-07-29 |
2018-07-30 |
2018-07-31 |
2018-08-02 |
2018-08-03 |
2018-08-06 |
2018-08-07 |
2018-08-08 |
2018-08-09 |
2018-08-10 |
2018-08-11 |
2018-08-13 |
2018-08-14 |
2018-08-15 |
2018-08-16 |
2018-08-17 |
2018-08-18 |
2018-08-20 |
2018-08-21 |
2018-08-27 |
2018-08-28 |
2018-08-29 |
2018-09-03 |
2018-09-10 |
2018-09-17 |
2018-09-24 |
넵 1번 2번 참고하여 수정하였습니다.
3.
TABLE1 : 전체건수 12, 조건을 만족하는 건수 : 6
TABLE2 : 전체건수 13, 조건을 만족하는 건수 : 11
TABLE3 : 전체건수 9, 조건을 만족하는 건수 : 7
TABLE4 : 전체건수 31, 조건을 만족하는 건수 : 19
TABLE5(각 테이블(TABLE1, TABLE2, TABLE3, TABLE4)마다 함께 조회하는 테이블) : 전체건수 2300, 조건을 만족하는 건수 : 1
TABLE6 (TABLE4의 조건절에 들어가는 테이블) : 전체건수: 984, 조건 만족건수 : 1
1,2,3,4테이블과 5테이블은 아래와 같이 합쳐서 조회합니다.
SELECT from_unixtime(A.SDATE, '%Y-%m-%d') AS SDATE , from_unixtime(A.EDATE, '%Y-%m-%d') AS EDATE FROM TABLE1 A, TABLE5 U WHERE U.U_IDX = 1 AND A.COL1!= 11 AND A.COL2!= 6 AND A.COL3!= 7 AND A.SDATE < UNIX_TIMESTAMP('2018-10-01') AND A.EDATE >= UNIX_TIMESTAMP('2018-07-01') AND( (A.U_IDX = U.U_IDX) OR (A.R_IDX = U.U_IDX) OR (A.C_IDX = U.U_IDX) OR (A.E_IDX = U.U_IDX) OR (CAST(U.COL1 as UNSIGNED) >= 1000) OR ( U.U_IDX IN ( SELECT S.C_IDX FROM TABLE6 S WHERE 1=1 AND S.COL1 = 'aaa' AND S.J_IDX = A.C_IDX ) ) )
이상입니다.
정보가 주실때마다 다릅니다.
추가되고 빠지고 이런건 이해하는데, 그게 아니라 아얘 다른 것은 곤란합니다.
처음 질문에 table1 과 table2 는 각각 조회하는데?
마지막 주신 쿼리에 table1 과 table2 를 조인하는 것은 무엇?
1,2,3,4테이블과 5테이블은 아래와 같이 합쳐서 조회합니다.
라고 하셨는데? 5테이블은 안보이고 6테이블만 보이는 것은 무엇?
1,2,3,4테이블 건수는 상당히 작네요?
5,6테이블이 건수가 있긴 하지만 그리 큰 건수도 아니고.
이렇게 건수가 적은데 속도가 안나온다는게 이해가 안가고요.
각 테이블에 인덱스는 있는지?
조인은 제대로 하고 있는지? 의문이네요?
정확한 정보를 주세요.