mysql 쿼리 133개 불러오는데 1분... ㅠ.ㅜ
본문
$sql = "
select c.ca_id, c.it_start, c.it_icon, c.it_id, c.it_name, c.it_id as pid, c.it_info1, c.it_info2, c.it_info3, it_icon, c.it_s_air, c.it_tour1, c.it_tour2,
a.prc_exchange, a.prc_halin, a.prc_price11, a.prc_price21, a.prc_price31,
(select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1) as price,
CASE a.prc_exchange
WHEN 'KOR'
THEN (select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1)
WHEN 'JPN'
THEN CAST((((select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1)
* round((select ex_JPY1 from exchange order by ex_regdate DESC LIMIT 1)/100) ) * 100 ) AS UNSIGNED)
WHEN 'USD'
THEN CAST((((select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1)
* round((select ex_USD1 from exchange order by ex_regdate DESC LIMIT 1)/100) ) * 100) AS UNSIGNED)
WHEN 'CHN'
THEN CAST((((select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1)
* round((select ex_CNY1 from exchange order by ex_regdate DESC LIMIT 1)/100) ) * 100) AS UNSIGNED)
WHEN 'ERP'
THEN CAST((((select CAST(prc_price11 AS UNSIGNED) AS LOWPRIC from wiz_prdprice where prc_price11>0 AND it_id = pid group by it_id order by LOWPRIC asc LIMIT 1)
* round((select ex_EUR1 from exchange order by ex_regdate DESC LIMIT 1)/100) ) * 100) AS UNSIGNED)
END SUM
from wiz_prdprice as a, exchange as b, wiz_product2 as c
WHERE 1=1
AND c.it_id = a.it_id
AND a.prc_price11 > 0
AND it_use != 'N'
$catcode_sql
GROUP BY c.it_id
$order_sql
limit $start, $rows
";
위 쿼리를 돌리면
133개 밖에 안되는 데이타를 가져오는데 1분 여 걸립니다.
이걸 최적화 하려면 어떻게 해야 할까요 ?
ㅠ.ㅜ
답변 1
index를 걸어보세요. 실행을 느리게 하는 컬럼이 있을 거에요.
저도 시스템 유지보수 쉽게 하기 위해서 쿼리 한방에 데이터가 나오게 많이 해요.
그래서 위에 쿼리보다 더 복잡하게 쿼리를 만들곤 하는 하는데요.
분명 속도를 늦게 하는 부분이 있어요.
그 부분을 찾아소 index를 걸어주시면 되요.
그리고 될 수 있으면 무결성 조건을 만족하도록 해야 해요.
DB 설계자체에 문제가 있다면 설계를 수정해서라도 ...