简体   繁体   中英

How would you optimize this mysql query?

Can anybody help me in optimizing this query:

SELECT DISTINCT
A.X1,      
A.X2   
FROM TABLEAA A
JOIN TABLEBB B ON A.Y = B.Y AND B.Z1='SELECTED1' AND B.W NOT LIKE 'SLECTED3'
JOIN TABLECC C ON A.Y = C.Y AND C.Z2='SELECTED2'
AND A.W NOT LIKE 'SLECTED3'

WHEREAS

TABLEAA : 1 million entries TABLEBB : 17 million entries TABLECC : 1.2 million entries

It works but it takes almost 8 to 10 seconds.

Is there any other way to write this?

edit: primary index on TableBB is combination of B.Z1 and BY primary index on TableCC is combination of C.Z2 and CY primary index on TableAA is AY

I hope this's better.

SELECT DISTINCT A.X1, A.X2   
FROM TABLEAA AS A
INNER JOIN(TABLEBB AS B)
   ON(A.Y = B.Y)
INNER JOIN(TABLECC AS C)
   ON(A.Y = C.Y)
WHERE B.Z1 = 'SELECTED1' AND
      B.W NOT LIKE '%SLECTED3%' AND
      C.Z2='SELECTED2' AND
      A.W NOT LIKE '%SLECTED3%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM