Search code examples
mysqlsqlsql-optimization

Optimize Big `IN` SQL


I have a this big SQL:

SELECT id,kdt_id,kdt_goods_id,fx_price,min_retail_price,max_retail_price,
fx_count,stock_num,sold_num,recommend_level,is_display,is_delete,
quit_time,review_status,fx_auth,level_discount_auth,is_fx_delete,
sold_status 
FROM apple_goods 
WHERE 1 and `kdt_goods_id` in (
    '44089839','44089839','44089839','44089839','44089839',
    '44089839','44089839','44089839','44089839','44089839',
    '44089839','44089839','44089839','44089839','44089839',
    '44089839','44089839','44089839','44089839','44089839',
    '44089839','44089839','44089839','44089839','44089839',
    '96611525','96611525','96611525','96611525','96611525',
    '96611525','96611525','96611525','96611525','96611525',
    '96611525','96611525','96611525','96611525','96611525',
    '96611525','96611525','96611525','96611525','96611525',
    '96611525','96611525','96611525','96611525','96611525',
    '96611525','96611525','96611525','96611525','142061451',
    '142061451','142061451','142061451','142061451','142061451',
    '142061451','142061451','142061451','142061451','142061451',
    '142061451','142061451','142061451','142061451','142061451',
    '142061451','142061451','142061451','142801551','142801551',
    '142801551','142801551','142801551','142801551','142801551',
    '142801551','142801551','142801551','142801551','142801551',
    '142801551','142801551','142801551','142801551','142801551',
    '142801551','142801551','142801551','142801551','142801551',
    '143381529','143381529','143381529','143381529','143381529',
    '143381529','147713054','147713054','147713054','147713054',
    '147713054','147713054','147713054','148164398','148164398',
    '148164398','148164398','148164398','148164398','148164398',
    '148164398','148164398','148164398','148164398','148296160',
    '148296160','148296160','148296160','148296160','148296160',
    '151063722','151063722','151063722','151063722','151063722',
    '151063722','151063722','151063722','151063722','151063722',
    '151063722','151063722','151063722','151063722','151063722',
    '151063722','151063722','151063722','151063722','151063722',
    '151063722','153933372','153933372','153933372','153933372',
    '153933372','153933372','154447237','154447237','154447237',
    '154447237','154447237','154447237','154447237','158137781',
    '158137781','158137781','158137781','158137781','158137781',
    '158217358','158217358','158217358','158217358','158217358',
    '158217358','158217358','158217358','158217358','158217358',
    '158217358','158217358','158217358','158246547','158246547',
    '158246547','158246547','158246547','158246547','158246547',
    '158246547','158246547','158246547','158246547','158246547',
    '158246547','158246547','158246547','158246547','158246547',
    '158246547','158246547','158246547','158246547','160734561',
    '160734561','160734561','160734561','160734561','160734561',
    '162435844','162435844','162435844','162435844','162435844',
    '162435844','162435844','162435844','162435844','162435844',
    '162435844','162435844','162435844','163784258','163784258',
    '163784258','163784258','163784258','163784258','163784258',
    '163784258','163784258','163784258','163784258','163784258',
    '163784258','163784258','163784258','163784258','163784258',
    '163784258','163784258','163800813','163800813','163800813',
    '163800813','163800813','163800813','163800813','163800813',
    '163800813','163800813','163800813','163803246','163803246',
    '163803246','163803246','163803246','163803246','164617535',
    '164617535','164617535','164617535','164617535','164617535',
    '164617535','164617535','164617535','164617535','164617535',
    '164617535','164617535','164617535','164617535','164617535',
    '164617535','164617535','164617535','164617535','164617535')

How to optimize it ?

the explain :

```
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: apple_goods
         type: range
possible_keys: kdt_goods_id
          key: kdt_goods_id
      key_len: 4
          ref: NULL
         rows: 20
        Extra: Using index condition
1 row in set (0.00 sec)

```

Solution

  • I have removed the duplicates from your IN, benchmark this against creating a temporary table maybe.

    SELECT id,kdt_id,kdt_goods_id,fx_price,min_retail_price,max_retail_price,
    fx_count,stock_num,sold_num,recommend_level,is_display,is_delete,
    quit_time,review_status,fx_auth,level_discount_auth,is_fx_delete,
    sold_status 
    FROM apple_goods 
    WHERE 1 and `kdt_goods_id` in
    ('96611525',
    '44089839',
    '164617535',
    '163803246',
    '163800813',
    '163784258',
    '162435844',
    '160734561',
    '158246547',
    '158217358',
    '158137781',
    '154447237',
    '153933372',
    '151063722',
    '148296160',
    '148164398',
    '147713054',
    '143381529',
    '142801551',
    '142061451')