2017-04-24 1 views
0

나는 내 SQL 요청의 처리 시간을 줄이고 싶다. (실제로는 10 분 정도 걸린다.) 중첩 된 SQL 쿼리에서 문제가 발생한다고 생각한다. 이 시도SQL 요청이 너무 길어 ... 단순화하는 방법?

SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount` 
FROM globe_statistique 
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21` 
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers` 
WHERE `gst.page` = 'send_order' 
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d') 

UNION 

SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, '-' 
FROM globe_statistique 
WHERE `gst.page` NOT LIKE 'send_order' " 
AND (`gst.codeAP21`,`gst.date`) NOT IN 
      (SELECT `gst.codeAP21`,`gst.date` FROM globe_statistique 
       WHERE `gst.page`='send_order'); 

감사

+0

NOT IN 재 - 쓰기 끝으로 그룹은 동일한 결과를 제공합니다. 그리고 NOT LIKE에서! =로 테스트 스위치를 전환하십시오. – jarlh

+0

필자는 좋지 않음으로 변경하려고 시도하지만! = 같지만 중첩 된 쿼리를 제거하면 작동합니다. 내 테이블 globe_statistique : 56000 rows – Gabi

+0

즉, 첫 번째 SELECT 느린 것입니까? – jarlh

답변

0

(내 영어 죄송합니다, 난 프랑스어 학생입니다) :

SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount` 
FROM globe_statistique 
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21` 
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers` 
WHERE `gst.page` = 'send_order' 
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d') 

UNION 
SELECT DISTINCT t1.`gst.codeAP21`, t1.`gst.email`, t1.`gst.date`, '-' 
FROM globe_statistique t1 
left join globe_statistique t2 on t1.gst.page =t2.gst.page and t1.gst.date =t2.gst.date and t2.gst.page =send_order 
WHERE `gst.page` <> 'send_order' AND t2.gst.date is null 

을하지만 난 당신의 열 이름을 이름을 변경하고 점을 제거하는 recomment.

또한 쿼리 속도가 느린 이유를 찾을 EXPLAIN를 사용하여 별개의 사용을 방지하기 위해 올바른 인덱스를

+0

안녕하세요! 응답 주셔서 감사합니다,하지만 난 마지막 라인을 이해하지 못했는데, 무엇을 발견 'gst.page' <> 'send_order'? – Gabi

+0

@Gabi Sould be! = – Jens

+0

왼쪽 결합과 함께 작동하지만 이미 매우 오래되었습니다. – Gabi

0

시도를 추가합니다. 이를 위해 UNION ALL을 사용해야합니다.

select codeAP21, email, date, amount 
from (--> your query without distinct but with UNION ALL <--) 
group by codeAP21, email, date, amount 

: 참조 : 왼쪽은 가입으로 Huge performance difference when using group by vs distinct

+1

이미 'UNION'이 있으므로 암시적인 DISTINCT도 수행 할 것입니다 –

+0

당신 말이 맞습니다. 당신은 또한 모든 조합을 대신해야합니다. 내 대답을 수정하겠습니다. – frank

관련 문제