优化Big`IN` SQL

时间:2015-10-28 08:08:11

标签: mysql sql sql-optimization

我有一个很大的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')

如何优化它?

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)

```

2 个答案:

答案 0 :(得分:1)

我已从您的SELECT * FROM new_table_name WHERE username = 'some_user_name'中移除了重复项,可能会针对创建临时表进行基准测试。

IN

答案 1 :(得分:0)

首先,您应该使用table来存储onQuit列表中的值,然后加入。如果这是瞬态数据,您可以使用临时表。

IN

上面的语句创建临时表,将数据插入其中,并删除所有重复项。

完成后,您只需在查询中使用您的表格:

CREATE TEMPORARY TABLE t1 (yourid VARCHAR(9) PRIMARY KEY);
INSERT IGNORE INTO t1 VALUES
    ('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');

我还建议您将临时表格中的类型与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 JOIN t1 ON (yourid=kdt_goods_id); 对的列进行匹配,例如:如果JOINkdt_goods_id,则临时表中的INT应为yourid