开发者

Optimize a simple JOIN or IN query

I have this query:

SELECT DISTINCT id, 
                label 
FROM   bbproduct_cust 
WHERE  CUSTNO IN (SELECT CUSTNO 
                  FROM   customer 
                  WHERE  SLSRPTGRP = '1996') 
       AND DEPTNO = '0' 
ORDER  BY label ASC 

EXPLAIN shows

id  select_type         table           type  possible_keys          key            key_len  ref    rows   Extra                                         
1   PRIMARY             bbproduct_cust  ALL   ind_deptno                 开发者_运维百科                           91834  Using where; Using temporary; Using filesort  
2   DEPENDENT SUBQUERY  customer        ref   PRIMARY,ind_slsrptgrp  ind_slsrptgrp  3        const  4      Using where                                   

it takes 2-3 seconds, and I need it optimized.

What options I have?


Use an INNER JOIN, rather than an IN select

Something like

SELECT DISTINCT id, 
                label 
FROM   bbproduct_cust INNER JOIN
        customer ON  bbproduct_cust.CUSTNO = customer.CUSTNO
WHERE  SLSRPTGRP = '1996'
       AND DEPTNO = '0' 
ORDER  BY label ASC


Use an INDEX to improve performance. Click here for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜