views:

22

answers:

2

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?

+1  A: 

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
astander
Then it would seem that you do not have the correct index on table *bbproduct_cust*
astander
it was a typo.. thanks for help
Pentium10
A: 

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

Chinmayee