重现步骤

数据库:zhcxkj-center-warehouse
查询时间:11.8669
执行次数: 164
扫描行数:4665790
发送行数:1988.02
指纹SQL: SELECT a . id , CONCAT ( ? , a . order_code ) as order_code , a . sku , a . group_sku , a . fnsku , a . prodct_namecn , a . prodct_nameen , a . add_time , a . product_num + a . product_num_update as needNum , b . order_state_name , b . warehouse_name , IFNULL ( d . buy_put_in_num , ? ) + IFNULL ( d . other_put_in_num , ? ) AS putInNum , a . product_assigned_num , case when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) != ? THEN a . group_product_num + a . group_product_num_update when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) = ? THEN a . product_num + a . product_num_update end as shipmentNum , case when be . order_build_type = ? THEN ? when be . order_build_type = ? THEN ? end as orderbuildTypeName FROM ? a LEFT JOIN t_stk_order_info b ON a . order_code = b . order_code AND a . customer_id = b . customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b . order_code = be . order_code AND b . customer_id = be . customer_id LEFT JOIN t_stk_stockpile_order_list d ON a . id = d . order_list_id where ? = ? and b . order_state = ? and a . add_time >= ? and ? >= a . add_time order by a . id desc limit ? , ? ;
真实SQL:SELECT a.id, CONCAT(,a.order_code) as order_code, a.sku, a.group_sku, a.fnsku,a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update as needNum, b.order_state_name, b.warehouse_name, IFNULL( d.buy_put_in_num, 0 )+ IFNULL( d.other_put_in_num, 0 ) AS putInNum, a.product_assigned_num, case when a.purchase_type=4 and IFNULL(a.group_sku,)!= THEN a.group_product_num+a.group_product_num_update when a.purchase_type=4 and IFNULL(a.group_sku,)= THEN a.product_num+a.product_num_update end as shipmentNum, case when be.order_build_type=1 THEN FBA备库订单 when be.order_build_type=3 THEN 海外仓备库订单 end as orderbuildTypeName FROM t_stk_order_list a LEFT JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id where 1=1 and b.order_state= 2 and a.add_time>= 2025-07-01 00:00:00 and 2025-08-28 23:59:59 >= a.add_time order by a.id desc limit 60000,2000;
无人工修改
AISQL修改建议:以下是优化后的SQL语句:


SELECT a.id,
CONCAT(, a.order_code) AS order_code,
a.sku,
a.group_sku,
a.fnsku,
a.prodct_namecn,
a.prodct_nameen,
a.add_time,
a.product_num+a.product_num_update AS needNum,
b.order_state_name,
b.warehouse_name,
IFNULL(d.buy_put_in_num, 0)+ IFNULL(d.other_put_in_num, 0) AS putInNum,
a.product_assigned_num,
IF(a.purchase_type=4 AND IFNULL(a.group_sku, )!=, a.group_product_num+a.group_product_num_update,
IF(a.purchase_type=4, a.product_num+a.product_num_update, NULL)) AS shipmentNum,
CASE be.order_build_type
WHEN 1 THEN FBA备库订单
WHEN 3 THEN 海外仓备库订单
END AS orderbuildTypeName
FROM t_stk_order_list a
INNER JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id AND b.order_state = 2
LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id
LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id
WHERE a.add_time BETWEEN 2025-07-01 00:00:00 AND 2025-08-28 23:59:59
ORDER BY a.id DESC
LIMIT 60000, 2000;


优化点:
1. 将 LEFT JOIN t_stk_order_info 改为 INNER JOIN 并添加 b.order_state = 2 到 JOIN 条件中
2. 简化 WHERE 条件,使用 BETWEEN 替代原来的范围判断
3. 用 IF 函数替代复杂的 CASE WHEN 结构
4. 移除了冗余的 1=1 条件
5. 简化了第二个 CASE WHEN 表达式
explain表:









id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE a None ALL idx_order_code,idx_customerid_updatetime None None None 2194920 11.11 Using where; Using temporary; Using filesort
1 SIMPLE b None ref idx_order_code,idx_order_code_,idx_ordercode_customerid_isstockorder_addtime,idx_ordercode_ordersourceid,idx_optimize_query,ordercode_customerid_orderstatus_orderstate_isstockorder_addtime,idx_customer_order_status,idx_customer_order_code idx_order_code 202 zhcxkj-center-warehouse.a.order_code 1 10 Using where
1 SIMPLE be None eq_ref PK_RB_OrderInfo_BeikuExpand_1,idx_order_code_fission_origin_order PK_RB_OrderInfo_BeikuExpand_1 206 zhcxkj-center-warehouse.a.customer_id,zhcxkj-center-warehouse.a.order_code 1 100 None
1 SIMPLE d None ALL None None None None 1485421 100 Using where; Using join buffer (hash join)

额外建议:
索引建议:join联表查询,on关联字段必须增加索引!

需要添加索引:ALTER TABLE t_stk_stockpile_order_list ADD INDEX idx_order_list_id(order_list_id);

【t_stk_stockpile_order_list】表 【order_list_id】字段,索引分析:
+--------------+--------------+---------------+---------------+
| TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |
+==============+==============+===============+===============+
+--------------+--------------+---------------+---------------+

建议添加索引:ALTER TABLE t_stk_order_list ADD INDEX idx_add_time_id(add_time,id);


【t_stk_order_list】表 【add_time,id】字段,索引分析:
+------------------+--------------+---------------+---------------+
| TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |
+==================+==============+===============+===============+
| t_stk_order_list | PRIMARY | id | 2076302 |
+------------------+--------------+---------------+---------------+

取出表 【t_stk_order_info】 where条件表达式 【order_state= 2】 100000 条记录,重复的数据有:【61489】 条,没有必要为该字段创建索引。

→ 【t_stk_order_info】 表,无需添加任何索引。

→ 【t_stk_order_info_beikuexpand】 表,无需添加任何索引。

→ 【t_stk_stockpile_order_list】 表,无需添加任何索引。


历史记录  
  1. 2025-09-09 10:49:15, 由 周杰 创建。
  2. 2025-09-09 18:04:56, 由 程伟 指派给 谢业福。
    修改了 指派给,旧值为 "chengwei",新值为 "xieyefu"。
  3. 2025-09-09 19:39:51, 由 谢业福 解决,方案为 性能问题 。
    修改了 解决方案,旧值为 "",新值为 "performance"。
    修改了 解决版本,旧值为 "",新值为 "trunk"。
    修改了 解决日期,旧值为 "",新值为 "2025-09-09 19:39:41"。
    修改了 指派给 ,旧值为 "xieyefu",新值为 "zhoujie"。
    修改了 Bug状态 ,旧值为 "active",新值为 "resolved"。
    修改了 是否确认,旧值为 "0",新值为 "1"。
    修改了 解决者 ,旧值为 "",新值为 "xieyefu"。
  4. 2025-09-09 19:43:33, 由 周杰 编辑。
    修改了 所属模块 ,旧值为 "0",新值为 "1932"。
    修改了 业务部门 ,旧值为 "0",新值为 "2"。
    修改了 开发责任人,旧值为 "",新值为 "xieyefu"。
    修改了 测试责任人,旧值为 "",新值为 "admin"。
    修改了 解决时效 ,旧值为 "0",新值为 "8.84"。
    修改了 重现步骤 ,区别为:
    001-
    001+ 数据库:zhcxkj-center-warehouse 查询时间:11.8669 执行次数: 164 扫描行数:4665790 发送行数:1988.02 指纹SQL: SELECT a . id , CONCAT ( ? , a . order_code ) as order_code , a . sku , a . group_sku , a . fnsku , a . prodct_namecn , a . prodct_nameen , a . add_time , a . product_num + a . product_num_update as needNum , b . order_state_name , b . warehouse_name , IFNULL ( d . buy_put_in_num , ? ) + IFNULL ( d . other_put_in_num , ? ) AS putInNum , a . product_assigned_num , case when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) != ? THEN a . group_product_num + a . group_product_num_update when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) = ? THEN a . product_num + a . product_num_update end as shipmentNum , case when be . order_build_type = ? THEN ? when be . order_build_type = ? THEN ? end as orderbuildTypeName FROM ? a LEFT JOIN t_stk_order_info b ON a . order_code = b . order_code AND a . customer_id = b . customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b . order_code = be . order_code AND b . customer_id = be . customer_id LEFT JOIN t_stk_stockpile_order_list d ON a . id = d . order_list_id where ? = ? and b . order_state = ? and a . add_time >= ? and ? >= a . add_time order by a . id desc limit ? , ? ; 真实SQL:SELECT a.id, CONCAT(,a.order_code) as order_code, a.sku, a.group_sku, a.fnsku,a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update as needNum, b.order_state_name, b.warehouse_name, IFNULL( d.buy_put_in_num, 0 )+ IFNULL( d.other_put_in_num, 0 ) AS putInNum, a.product_assigned_num, case when a.purchase_type=4 and IFNULL(a.group_sku,)!= THEN a.group_product_num+a.group_product_num_update when a.purchase_type=4 and IFNULL(a.group_sku,)= THEN a.product_num+a.product_num_update end as shipmentNum, case when be.order_build_type=1 THEN FBA备库订单 when be.order_build_type=3 THEN 海外仓备库订单 end as orderbuildTypeName FROM t_stk_order_list a LEFT JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id where 1=1 and b.order_state= 2 and a.add_time>= 2025-07-01 00:00:00 and 2025-08-28 23:59:59 >= a.add_time order by a.id desc limit 60000,2000; 无人工修改 AISQL修改建议:以下是优化后的SQL语句: SELECT a.id, CONCAT(, a.order_code) AS order_code, a.sku, a.group_sku, a.fnsku, a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update AS needNum, b.order_state_name, b.warehouse_name, IFNULL(d.buy_put_in_num, 0)+ IFNULL(d.other_put_in_num, 0) AS putInNum, a.product_assigned_num, IF(a.purchase_type=4 AND IFNULL(a.group_sku, )!=, a.group_product_num+a.group_product_num_update, IF(a.purchase_type=4, a.product_num+a.product_num_update, NULL)) AS shipmentNum, CASE be.order_build_type WHEN 1 THEN FBA备库订单 WHEN 3 THEN 海外仓备库订单 END AS orderbuildTypeName FROM t_stk_order_list a INNER JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id AND b.order_state = 2 LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id WHERE a.add_time BETWEEN 2025-07-01 00:00:00 AND 2025-08-28 23:59:59 ORDER BY a.id DESC LIMIT 60000, 2000; 优化点: 1. 将 LEFT JOIN t_stk_order_info 改为 INNER JOIN 并添加 b.order_state = 2 到 JOIN 条件中 2. 简化 WHERE 条件,使用 BETWEEN 替代原来的范围判断 3. 用 IF 函数替代复杂的 CASE WHEN 结构 4. 移除了冗余的 1=1 条件 5. 简化了第二个 CASE WHEN 表达式 explain表:id
    002- 数据库:zhcxkj-center-warehouse
    002+ select_type
    003- 查询时间:11.8669
    003+ table
    004- 执行次数: 164
    004+ partitions
    005- 扫描行数:4665790
    005+ type
    006- 发送行数:1988.02
    006+ possible_keys
    007- 指纹SQL: SELECT a . id , CONCAT ( ? , a . order_code ) as order_code , a . sku , a . group_sku , a . fnsku , a . prodct_namecn , a . prodct_nameen , a . add_time , a . product_num + a . product_num_update as needNum , b . order_state_name , b . warehouse_name , IFNULL ( d . buy_put_in_num , ? ) + IFNULL ( d . other_put_in_num , ? ) AS putInNum , a . product_assigned_num , case when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) != ? THEN a . group_product_num + a . group_product_num_update when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) = ? THEN a . product_num + a . product_num_update end as shipmentNum , case when be . order_build_type = ? THEN ? when be . order_build_type = ? THEN ? end as orderbuildTypeName FROM ? a LEFT JOIN t_stk_order_info b ON a . order_code = b . order_code AND a . customer_id = b . customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b . order_code = be . order_code AND b . customer_id = be . customer_id LEFT JOIN t_stk_stockpile_order_list d ON a . id = d . order_list_id where ? = ? and b . order_state = ? and a . add_time >= ? and ? >= a . add_time order by a . id desc limit ? , ? ;
    007+ key
    008- 真实SQL:SELECT a.id, CONCAT(,a.order_code) as order_code, a.sku, a.group_sku, a.fnsku,a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update as needNum, b.order_state_name, b.warehouse_name, IFNULL( d.buy_put_in_num, 0 )+ IFNULL( d.other_put_in_num, 0 ) AS putInNum, a.product_assigned_num, case when a.purchase_type=4 and IFNULL(a.group_sku,)!= THEN a.group_product_num+a.group_product_num_update when a.purchase_type=4 and IFNULL(a.group_sku,)= THEN a.product_num+a.product_num_update end as shipmentNum, case when be.order_build_type=1 THEN FBA备库订单 when be.order_build_type=3 THEN 海外仓备库订单 end as orderbuildTypeName FROM t_stk_order_list a LEFT JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id where 1=1 and b.order_state= 2 and a.add_time>= 2025-07-01 00:00:00 and 2025-08-28 23:59:59 >= a.add_time order by a.id desc limit 60000,2000;
    008+ key_len
    009- 无人工修改
    009+ ref
    010- AISQL修改建议:以下是优化后的SQL语句:
    010+ rows
    011-
    011+ filtered
    012-
    012+ Extra
    013- SELECT a.id,
    013+ 1
    014- CONCAT(, a.order_code) AS order_code,
    014+ SIMPLE
    015- a.sku,
    015+ a
    016- a.group_sku,
    016+ None
    017- a.fnsku,
    017+ ALL
    018- a.prodct_namecn,
    018+ idx_order_code,idx_customerid_updatetime
    019- a.prodct_nameen,
    019+ None
    020- a.add_time,
    020+ None
    021- a.product_num+a.product_num_update AS needNum,
    021+ None
    022- b.order_state_name,
    022+ 2194920
    023- b.warehouse_name,
    023+ 11.11
    024- IFNULL(d.buy_put_in_num, 0)+ IFNULL(d.other_put_in_num, 0) AS putInNum,
    024+ Using where; Using temporary; Using filesort
    025- a.product_assigned_num,
    025+ 1
    026- IF(a.purchase_type=4 AND IFNULL(a.group_sku, )!=, a.group_product_num+a.group_product_num_update,
    026+ SIMPLE
    027- IF(a.purchase_type=4, a.product_num+a.product_num_update, NULL)) AS shipmentNum,
    027+ b
    028- CASE be.order_build_type
    028+ None
    029- WHEN 1 THEN FBA备库订单
    029+ ref
    030- WHEN 3 THEN 海外仓备库订单
    030+ idx_order_code,idx_order_code_,idx_ordercode_customerid_isstockorder_addtime,idx_ordercode_ordersourceid,idx_optimize_query,ordercode_customerid_orderstatus_orderstate_isstockorder_addtime,idx_customer_order_status,idx_customer_order_code
    031- END AS orderbuildTypeName
    031+ idx_order_code
    032- FROM t_stk_order_list a
    032+ 202
    033- INNER JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id AND b.order_state = 2
    033+ zhcxkj-center-warehouse.a.order_code
    034- LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id
    034+ 1
    035- LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id
    035+ 10
    036- WHERE a.add_time BETWEEN 2025-07-01 00:00:00 AND 2025-08-28 23:59:59
    036+ Using where
    037- ORDER BY a.id DESC
    037+ 1
    038- LIMIT 60000, 2000;
    038+ SIMPLE
    039-
    039+ be
    040-
    040+ None
    041- 优化点:
    041+ eq_ref
    042- 1. 将 LEFT JOIN t_stk_order_info 改为 INNER JOIN 并添加 b.order_state = 2 到 JOIN 条件中
    042+ PK_RB_OrderInfo_BeikuExpand_1,idx_order_code_fission_origin_order
    043- 2. 简化 WHERE 条件,使用 BETWEEN 替代原来的范围判断
    043+ PK_RB_OrderInfo_BeikuExpand_1
    044- 3. 用 IF 函数替代复杂的 CASE WHEN 结构
    044+ 206
    045- 4. 移除了冗余的 1=1 条件
    045+ zhcxkj-center-warehouse.a.customer_id,zhcxkj-center-warehouse.a.order_code
    046- 5. 简化了第二个 CASE WHEN 表达式
    046+ 1
    047- explain表:
    047+ 100
    048-
    048+ None
    049-
    049+ 1
    050-
    050+ SIMPLE
    051-
    051+ d
    052-
    052+ None
    053-
    053+ ALL
    054-
    054+ None
    055-
    055+ None
    056-
    056+ None
    057-
    057+ None
    058-
    058+ 1485421
    059-
    059+ 100
    060-
    060+ Using where; Using join buffer (hash join)
    061- id
    061+
    062-
    062+ 额外建议: 索引建议:join联表查询,on关联字段必须增加索引! 需要添加索引:ALTER TABLE t_stk_stockpile_order_list ADD INDEX idx_order_list_id(order_list_id); 【t_stk_stockpile_order_list】表 【order_list_id】字段,索引分析: +--------------+--------------+---------------+---------------+ | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY | +==============+==============+===============+===============+ +--------------+--------------+---------------+---------------+ 建议添加索引:ALTER TABLE t_stk_order_list ADD INDEX idx_add_time_id(add_time,id); 【t_stk_order_list】表 【add_time,id】字段,索引分析: +------------------+--------------+---------------+---------------+ | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY | +==================+==============+===============+===============+ | t_stk_order_list | PRIMARY | id | 2076302 | +------------------+--------------+---------------+---------------+ 取出表 【t_stk_order_info】 where条件表达式 【order_state= 2】 100000 条记录,重复的数据有:【61489】 条,没有必要为该字段创建索引。→ 【t_stk_order_info】 表,无需添加任何索引。→ 【t_stk_order_info_beikuexpand】 表,无需添加任何索引。→ 【t_stk_stockpile_order_list】 表,无需添加任何索引。
    063-
    064- select_type
    065-
    066-
    067- table
    068-
    069-
    070- partitions
    071-
    072-
    073- type
    074-
    075-
    076- possible_keys
    077-
    078-
    079- key
    080-
    081-
    082- key_len
    083-
    084-
    085- ref
    086-
    087-
    088- rows
    089-
    090-
    091- filtered
    092-
    093-
    094- Extra
    095-
    096-
    097-
    098-
    099-
    100-
    101- 1
    102-
    103-
    104- SIMPLE
    105-
    106-
    107- a
    108-
    109-
    110- None
    111-
    112-
    113- ALL
    114-
    115-
    116- idx_order_code,idx_customerid_updatetime
    117-
    118-
    119- None
    120-
    121-
    122- None
    123-
    124-
    125- None
    126-
    127-
    128- 2194920
    129-
    130-
    131- 11.11
    132-
    133-
    134- Using where; Using temporary; Using filesort
    135-
    136-
    137-
    138-
    139- 1
    140-
    141-
    142- SIMPLE
    143-
    144-
    145- b
    146-
    147-
    148- None
    149-
    150-
    151- ref
    152-
    153-
    154- idx_order_code,idx_order_code_,idx_ordercode_customerid_isstockorder_addtime,idx_ordercode_ordersourceid,idx_optimize_query,ordercode_customerid_orderstatus_orderstate_isstockorder_addtime,idx_customer_order_status,idx_customer_order_code
    155-
    156-
    157- idx_order_code
    158-
    159-
    160- 202
    161-
    162-
    163- zhcxkj-center-warehouse.a.order_code
    164-
    165-
    166- 1
    167-
    168-
    169- 10
    170-
    171-
    172- Using where
    173-
    174-
    175-
    176-
    177- 1
    178-
    179-
    180- SIMPLE
    181-
    182-
    183- be
    184-
    185-
    186- None
    187-
    188-
    189- eq_ref
    190-
    191-
    192- PK_RB_OrderInfo_BeikuExpand_1,idx_order_code_fission_origin_order
    193-
    194-
    195- PK_RB_OrderInfo_BeikuExpand_1
    196-
    197-
    198- 206
    199-
    200-
    201- zhcxkj-center-warehouse.a.customer_id,zhcxkj-center-warehouse.a.order_code
    202-
    203-
    204- 1
    205-
    206-
    207- 100
    208-
    209-
    210- None
    211-
    212-
    213-
    214-
    215- 1
    216-
    217-
    218- SIMPLE
    219-
    220-
    221- d
    222-
    223-
    224- None
    225-
    226-
    227- ALL
    228-
    229-
    230- None
    231-
    232-
    233- None
    234-
    235-
    236- None
    237-
    238-
    239- None
    240-
    241-
    242- 1485421
    243-
    244-
    245- 100
    246-
    247-
    248- Using where; Using join buffer (hash join)
    249-
    250-
    251-
    252-
    253-
    254- 额外建议:
    255- 索引建议:join联表查询,on关联字段必须增加索引!
    256-
    257- 需要添加索引:ALTER TABLE t_stk_stockpile_order_list ADD INDEX idx_order_list_id(order_list_id);
    258- 【t_stk_stockpile_order_list】表 【order_list_id】字段,索引分析:
    259- +--------------+--------------+---------------+---------------+
    260- | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |
    261- +==============+==============+===============+===============+
    262- +--------------+--------------+---------------+---------------+
    263-
    264- 建议添加索引:ALTER TABLE t_stk_order_list ADD INDEX idx_add_time_id(add_time,id);
    265-
    266- 【t_stk_order_list】表 【add_time,id】字段,索引分析:
    267- +------------------+--------------+---------------+---------------+
    268- | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |
    269- +==================+==============+===============+===============+
    270- | t_stk_order_list | PRIMARY | id | 2076302 |
    271- +------------------+--------------+---------------+---------------+
    272-
    273- 取出表 【t_stk_order_info】 where条件表达式 【order_state= 2】 100000 条记录,重复的数据有:【61489】 条,没有必要为该字段创建索引。
    274-
    275- → 【t_stk_order_info】 表,无需添加任何索引。
    276-
    277- → 【t_stk_order_info_beikuexpand】 表,无需添加任何索引。
    278-
    279- → 【t_stk_stockpile_order_list】 表,无需添加任何索引。
    280-
    281-
    001- <br />
    001+ <br /> 数据库:zhcxkj-center-warehouse<br /> 查询时间:11.8669<br /> 执行次数: 164<br /> 扫描行数:4665790<br /> 发送行数:1988.02<br /> 指纹SQL: SELECT a . id , CONCAT ( ? , a . order_code ) as order_code , a . sku , a . group_sku , a . fnsku , a . prodct_namecn , a . prodct_nameen , a . add_time , a . product_num + a . product_num_update as needNum , b . order_state_name , b . warehouse_name , IFNULL ( d . buy_put_in_num , ? ) + IFNULL ( d . other_put_in_num , ? ) AS putInNum , a . product_assigned_num , case when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) != ? THEN a . group_product_num + a . group_product_num_update when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) = ? THEN a . product_num + a . product_num_update end as shipmentNum , case when be . order_build_type = ? THEN ? when be . order_build_type = ? THEN ? end as orderbuildTypeName FROM ? a LEFT JOIN t_stk_order_info b ON a . order_code = b . order_code AND a . customer_id = b . customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b . order_code = be . order_code AND b . customer_id = be . customer_id LEFT JOIN t_stk_stockpile_order_list d ON a . id = d . order_list_id where ? = ? and b . order_state = ? and a . add_time &gt;= ? and ? &gt;= a . add_time order by a . id desc limit ? , ? ;<br /> 真实SQL:SELECT a.id, CONCAT(,a.order_code) as order_code, a.sku, a.group_sku, a.fnsku,a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update as needNum, b.order_state_name, b.warehouse_name, IFNULL( d.buy_put_in_num, 0 )+ IFNULL( d.other_put_in_num, 0 ) AS putInNum, a.product_assigned_num, case when a.purchase_type=4 and IFNULL(a.group_sku,)!= THEN a.group_product_num+a.group_product_num_update when a.purchase_type=4 and IFNULL(a.group_sku,)= THEN a.product_num+a.product_num_update end as shipmentNum, case when be.order_build_type=1 THEN FBA备库订单 when be.order_build_type=3 THEN 海外仓备库订单 end as orderbuildTypeName FROM t_stk_order_list a LEFT JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id where 1=1 and b.order_state= 2 and a.add_time&gt;= 2025-07-01 00:00:00 and 2025-08-28 23:59:59 &gt;= a.add_time order by a.id desc limit 60000,2000;<br /> 无人工修改<br /> AISQL修改建议:以下是优化后的SQL语句:<br /><br /><br /> SELECT a.id,<br /> CONCAT(, a.order_code) AS order_code,<br /> a.sku,<br /> a.group_sku,<br /> a.fnsku,<br /> a.prodct_namecn,<br /> a.prodct_nameen,<br /> a.add_time,<br /> a.product_num+a.product_num_update AS needNum,<br /> b.order_state_name,<br /> b.warehouse_name,<br /> IFNULL(d.buy_put_in_num, 0)+ IFNULL(d.other_put_in_num, 0) AS putInNum,<br /> a.product_assigned_num,<br /> IF(a.purchase_type=4 AND IFNULL(a.group_sku, )!=, a.group_product_num+a.group_product_num_update, <br /> IF(a.purchase_type=4, a.product_num+a.product_num_update, NULL)) AS shipmentNum,<br /> CASE be.order_build_type<br /> WHEN 1 THEN FBA备库订单<br /> WHEN 3 THEN 海外仓备库订单<br /> END AS orderbuildTypeName<br /> FROM t_stk_order_list a<br /> INNER JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id AND b.order_state = 2<br /> LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id<br /> LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id<br /> WHERE a.add_time BETWEEN 2025-07-01 00:00:00 AND 2025-08-28 23:59:59<br /> ORDER BY a.id DESC<br /> LIMIT 60000, 2000;<br /><br /><br /> 优化点:<br /> 1. 将 LEFT JOIN t_stk_order_info 改为 INNER JOIN 并添加 b.order_state = 2 到 JOIN 条件中<br /> 2. 简化 WHERE 条件,使用 BETWEEN 替代原来的范围判断<br /> 3. 用 IF 函数替代复杂的 CASE WHEN 结构<br /> 4. 移除了冗余的 1=1 条件<br /> 5. 简化了第二个 CASE WHEN 表达式<br /> explain表:<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><table><tbody><tr><th>id</th>
    002- 数据库:zhcxkj-center-warehouse<br />
    002+ <th>select_type</th>
    003- 查询时间:11.8669<br />
    003+ <th>table</th>
    004- 执行次数: 164<br />
    004+ <th>partitions</th>
    005- 扫描行数:4665790<br />
    005+ <th>type</th>
    006- 发送行数:1988.02<br />
    006+ <th>possible_keys</th>
    007- 指纹SQL: SELECT a . id , CONCAT ( ? , a . order_code ) as order_code , a . sku , a . group_sku , a . fnsku , a . prodct_namecn , a . prodct_nameen , a . add_time , a . product_num + a . product_num_update as needNum , b . order_state_name , b . warehouse_name , IFNULL ( d . buy_put_in_num , ? ) + IFNULL ( d . other_put_in_num , ? ) AS putInNum , a . product_assigned_num , case when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) != ? THEN a . group_product_num + a . group_product_num_update when a . purchase_type = ? and IFNULL ( a . group_sku , ? ) = ? THEN a . product_num + a . product_num_update end as shipmentNum , case when be . order_build_type = ? THEN ? when be . order_build_type = ? THEN ? end as orderbuildTypeName FROM ? a LEFT JOIN t_stk_order_info b ON a . order_code = b . order_code AND a . customer_id = b . customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b . order_code = be . order_code AND b . customer_id = be . customer_id LEFT JOIN t_stk_stockpile_order_list d ON a . id = d . order_list_id where ? = ? and b . order_state = ? and a . add_time &gt;= ? and ? &gt;= a . add_time order by a . id desc limit ? , ? ;<br />
    007+ <th>key</th>
    008- 真实SQL:SELECT a.id, CONCAT(,a.order_code) as order_code, a.sku, a.group_sku, a.fnsku,a.prodct_namecn, a.prodct_nameen, a.add_time, a.product_num+a.product_num_update as needNum, b.order_state_name, b.warehouse_name, IFNULL( d.buy_put_in_num, 0 )+ IFNULL( d.other_put_in_num, 0 ) AS putInNum, a.product_assigned_num, case when a.purchase_type=4 and IFNULL(a.group_sku,)!= THEN a.group_product_num+a.group_product_num_update when a.purchase_type=4 and IFNULL(a.group_sku,)= THEN a.product_num+a.product_num_update end as shipmentNum, case when be.order_build_type=1 THEN FBA备库订单 when be.order_build_type=3 THEN 海外仓备库订单 end as orderbuildTypeName FROM t_stk_order_list a LEFT JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id where 1=1 and b.order_state= 2 and a.add_time&gt;= 2025-07-01 00:00:00 and 2025-08-28 23:59:59 &gt;= a.add_time order by a.id desc limit 60000,2000;<br />
    008+ <th>key_len</th>
    009- 无人工修改<br />
    009+ <th>ref</th>
    010- AISQL修改建议:以下是优化后的SQL语句:<br />
    010+ <th>rows</th>
    011- <br />
    011+ <th>filtered</th>
    012- <br />
    012+ <th>Extra</th>
    013- SELECT a.id,<br />
    013+ </tr></tbody><tbody><tr><td>1</td>
    014- CONCAT(, a.order_code) AS order_code,<br />
    014+ <td>SIMPLE</td>
    015- a.sku,<br />
    015+ <td>a</td>
    016- a.group_sku,<br />
    016+ <td>None</td>
    017- a.fnsku,<br />
    017+ <td>ALL</td>
    018- a.prodct_namecn,<br />
    018+ <td>idx_order_code,idx_customerid_updatetime</td>
    019- a.prodct_nameen,<br />
    019+ <td>None</td>
    020- a.add_time,<br />
    020+ <td>None</td>
    021- a.product_num+a.product_num_update AS needNum,<br />
    021+ <td>None</td>
    022- b.order_state_name,<br />
    022+ <td>2194920</td>
    023- b.warehouse_name,<br />
    023+ <td>11.11</td>
    024- IFNULL(d.buy_put_in_num, 0)+ IFNULL(d.other_put_in_num, 0) AS putInNum,<br />
    024+ <td>Using where; Using temporary; Using filesort</td>
    025- a.product_assigned_num,<br />
    025+ </tr><tr><td>1</td>
    026- IF(a.purchase_type=4 AND IFNULL(a.group_sku, )!=, a.group_product_num+a.group_product_num_update, <br />
    026+ <td>SIMPLE</td>
    027- IF(a.purchase_type=4, a.product_num+a.product_num_update, NULL)) AS shipmentNum,<br />
    027+ <td>b</td>
    028- CASE be.order_build_type<br />
    028+ <td>None</td>
    029- WHEN 1 THEN FBA备库订单<br />
    029+ <td>ref</td>
    030- WHEN 3 THEN 海外仓备库订单<br />
    030+ <td>idx_order_code,idx_order_code_,idx_ordercode_customerid_isstockorder_addtime,idx_ordercode_ordersourceid,idx_optimize_query,ordercode_customerid_orderstatus_orderstate_isstockorder_addtime,idx_customer_order_status,idx_customer_order_code</td>
    031- END AS orderbuildTypeName<br />
    031+ <td>idx_order_code</td>
    032- FROM t_stk_order_list a<br />
    032+ <td>202</td>
    033- INNER JOIN t_stk_order_info b ON a.order_code = b.order_code AND a.customer_id = b.customer_id AND b.order_state = 2<br />
    033+ <td>zhcxkj-center-warehouse.a.order_code</td>
    034- LEFT JOIN t_stk_order_info_beikuexpand be ON b.order_code = be.order_code AND b.customer_id = be.customer_id<br />
    034+ <td>1</td>
    035- LEFT JOIN t_stk_stockpile_order_list d ON a.id = d.order_list_id<br />
    035+ <td>10</td>
    036- WHERE a.add_time BETWEEN 2025-07-01 00:00:00 AND 2025-08-28 23:59:59<br />
    036+ <td>Using where</td>
    037- ORDER BY a.id DESC<br />
    037+ </tr><tr><td>1</td>
    038- LIMIT 60000, 2000;<br />
    038+ <td>SIMPLE</td>
    039- <br />
    039+ <td>be</td>
    040- <br />
    040+ <td>None</td>
    041- 优化点:<br />
    041+ <td>eq_ref</td>
    042- 1. 将 LEFT JOIN t_stk_order_info 改为 INNER JOIN 并添加 b.order_state = 2 到 JOIN 条件中<br />
    042+ <td>PK_RB_OrderInfo_BeikuExpand_1,idx_order_code_fission_origin_order</td>
    043- 2. 简化 WHERE 条件,使用 BETWEEN 替代原来的范围判断<br />
    043+ <td>PK_RB_OrderInfo_BeikuExpand_1</td>
    044- 3. 用 IF 函数替代复杂的 CASE WHEN 结构<br />
    044+ <td>206</td>
    045- 4. 移除了冗余的 1=1 条件<br />
    045+ <td>zhcxkj-center-warehouse.a.customer_id,zhcxkj-center-warehouse.a.order_code</td>
    046- 5. 简化了第二个 CASE WHEN 表达式<br />
    046+ <td>1</td>
    047- explain表:<br />
    047+ <td>100</td>
    048- <br />
    048+ <td>None</td>
    049- <br />
    049+ </tr><tr><td>1</td>
    050- <br />
    050+ <td>SIMPLE</td>
    051- <br />
    051+ <td>d</td>
    052- <br />
    052+ <td>None</td>
    053- <br />
    053+ <td>ALL</td>
    054- <br />
    054+ <td>None</td>
    055- <br />
    055+ <td>None</td>
    056- <br />
    056+ <td>None</td>
    057- <table>
    057+ <td>None</td>
    058- <tbody>
    058+ <td>1485421</td>
    059- <tr>
    059+ <td>100</td>
    060- <th>
    060+ <td>Using where; Using join buffer (hash join)</td>
    061- id
    061+ </tr></tbody></table><br />
    062- </th>
    062+ 额外建议:<br /> 索引建议:join联表查询,on关联字段必须增加索引!<br /><span style="color:#FF0000;"><br /> 需要添加索引:ALTER TABLE t_stk_stockpile_order_list ADD INDEX idx_order_list_id(order_list_id);</span><br /> 【t_stk_stockpile_order_list】表 【order_list_id】字段,索引分析:<br /> +--------------+--------------+---------------+---------------+<br /> | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |<br /> +==============+==============+===============+===============+<br /> +--------------+--------------+---------------+---------------+<br /><span style="color:#FFA500;"><br /> 建议添加索引:ALTER TABLE t_stk_order_list ADD INDEX idx_add_time_id(add_time,id);</span><br /><br /> 【t_stk_order_list】表 【add_time,id】字段,索引分析:<br /> +------------------+--------------+---------------+---------------+<br /> | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |<br /> +==================+==============+===============+===============+<br /> | t_stk_order_list | PRIMARY | id | 2076302 |<br /> +------------------+--------------+---------------+---------------+<br /><br /> 取出表 【t_stk_order_info】 where条件表达式 【order_state= 2】 100000 条记录,重复的数据有:【61489】 条,没有必要为该字段创建索引。<br /><br /><span style="color:#008000;font-weight:bold;">→ 【t_stk_order_info】 表,无需添加任何索引。</span><br /><br /><span style="color:#008000;font-weight:bold;">→ 【t_stk_order_info_beikuexpand】 表,无需添加任何索引。</span><br /><br /><span style="color:#008000;font-weight:bold;">→ 【t_stk_stockpile_order_list】 表,无需添加任何索引。</span><br /><br /><br />
    063- <th>
    064- select_type
    065- </th>
    066- <th>
    067- table
    068- </th>
    069- <th>
    070- partitions
    071- </th>
    072- <th>
    073- type
    074- </th>
    075- <th>
    076- possible_keys
    077- </th>
    078- <th>
    079- key
    080- </th>
    081- <th>
    082- key_len
    083- </th>
    084- <th>
    085- ref
    086- </th>
    087- <th>
    088- rows
    089- </th>
    090- <th>
    091- filtered
    092- </th>
    093- <th>
    094- Extra
    095- </th>
    096- </tr>
    097- </tbody>
    098- <tbody>
    099- <tr>
    100- <td>
    101- 1
    102- </td>
    103- <td>
    104- SIMPLE
    105- </td>
    106- <td>
    107- a
    108- </td>
    109- <td>
    110- None
    111- </td>
    112- <td>
    113- ALL
    114- </td>
    115- <td>
    116- idx_order_code,idx_customerid_updatetime
    117- </td>
    118- <td>
    119- None
    120- </td>
    121- <td>
    122- None
    123- </td>
    124- <td>
    125- None
    126- </td>
    127- <td>
    128- 2194920
    129- </td>
    130- <td>
    131- 11.11
    132- </td>
    133- <td>
    134- Using where; Using temporary; Using filesort
    135- </td>
    136- </tr>
    137- <tr>
    138- <td>
    139- 1
    140- </td>
    141- <td>
    142- SIMPLE
    143- </td>
    144- <td>
    145- b
    146- </td>
    147- <td>
    148- None
    149- </td>
    150- <td>
    151- ref
    152- </td>
    153- <td>
    154- idx_order_code,idx_order_code_,idx_ordercode_customerid_isstockorder_addtime,idx_ordercode_ordersourceid,idx_optimize_query,ordercode_customerid_orderstatus_orderstate_isstockorder_addtime,idx_customer_order_status,idx_customer_order_code
    155- </td>
    156- <td>
    157- idx_order_code
    158- </td>
    159- <td>
    160- 202
    161- </td>
    162- <td>
    163- zhcxkj-center-warehouse.a.order_code
    164- </td>
    165- <td>
    166- 1
    167- </td>
    168- <td>
    169- 10
    170- </td>
    171- <td>
    172- Using where
    173- </td>
    174- </tr>
    175- <tr>
    176- <td>
    177- 1
    178- </td>
    179- <td>
    180- SIMPLE
    181- </td>
    182- <td>
    183- be
    184- </td>
    185- <td>
    186- None
    187- </td>
    188- <td>
    189- eq_ref
    190- </td>
    191- <td>
    192- PK_RB_OrderInfo_BeikuExpand_1,idx_order_code_fission_origin_order
    193- </td>
    194- <td>
    195- PK_RB_OrderInfo_BeikuExpand_1
    196- </td>
    197- <td>
    198- 206
    199- </td>
    200- <td>
    201- zhcxkj-center-warehouse.a.customer_id,zhcxkj-center-warehouse.a.order_code
    202- </td>
    203- <td>
    204- 1
    205- </td>
    206- <td>
    207- 100
    208- </td>
    209- <td>
    210- None
    211- </td>
    212- </tr>
    213- <tr>
    214- <td>
    215- 1
    216- </td>
    217- <td>
    218- SIMPLE
    219- </td>
    220- <td>
    221- d
    222- </td>
    223- <td>
    224- None
    225- </td>
    226- <td>
    227- ALL
    228- </td>
    229- <td>
    230- None
    231- </td>
    232- <td>
    233- None
    234- </td>
    235- <td>
    236- None
    237- </td>
    238- <td>
    239- None
    240- </td>
    241- <td>
    242- 1485421
    243- </td>
    244- <td>
    245- 100
    246- </td>
    247- <td>
    248- Using where; Using join buffer (hash join)
    249- </td>
    250- </tr>
    251- </tbody>
    252- </table>
    253- <br />
    254- 额外建议:<br />
    255- 索引建议:join联表查询,on关联字段必须增加索引!<br />
    256- <span style="color:red;"><br />
    257- 需要添加索引:ALTER TABLE t_stk_stockpile_order_list ADD INDEX idx_order_list_id(order_list_id);</span><br />
    258- 【t_stk_stockpile_order_list】表 【order_list_id】字段,索引分析:<br />
    259- +--------------+--------------+---------------+---------------+<br />
    260- | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |<br />
    261- +==============+==============+===============+===============+<br />
    262- +--------------+--------------+---------------+---------------+<br />
    263- <span style="color:FFA500;"><br />
    264- 建议添加索引:ALTER TABLE t_stk_order_list ADD INDEX idx_add_time_id(add_time,id);</span><br />
    265- <br />
    266- 【t_stk_order_list】表 【add_time,id】字段,索引分析:<br />
    267- +------------------+--------------+---------------+---------------+<br />
    268- | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY |<br />
    269- +==================+==============+===============+===============+<br />
    270- | t_stk_order_list | PRIMARY | id | 2076302 |<br />
    271- +------------------+--------------+---------------+---------------+<br />
    272- <br />
    273- 取出表 【t_stk_order_info】 where条件表达式 【order_state= 2】 100000 条记录,重复的数据有:【61489】 条,没有必要为该字段创建索引。<br />
    274- <br />
    275- <span style="color:green;font-weight:bold;">→ 【t_stk_order_info】 表,无需添加任何索引。</span><br />
    276- <br />
    277- <span style="color:green;font-weight:bold;">→ 【t_stk_order_info_beikuexpand】 表,无需添加任何索引。</span><br />
    278- <br />
    279- <span style="color:green;font-weight:bold;">→ 【t_stk_stockpile_order_list】 表,无需添加任何索引。</span><br />
    280- <br />
    281- <br />
  5. 2025-09-09 19:43:38, 由 周杰 关闭。
    修改了 Bug状态 ,旧值为 "resolved",新值为 "closed"。
    修改了 指派给 ,旧值为 "zhoujie",新值为 "closed"。
    修改了 由谁关闭,旧值为 "",新值为 "zhoujie"。
    修改了 关闭日期,旧值为 "",新值为 "2025-09-09 19:43:38"。
  6. 2025-09-12 15:32:54, 由 陈莉珍 编辑。
    修改了 测试责任人,旧值为 "admin",新值为 "xieyefu"。
所属项目 技术中心-需求池
所属模块 供应链   仓储物流   仓储
所属计划
所属迭代
Bug类型
严重程度 一般
优先级 P2
Bug状态 已关闭
业务部门 总经办
影响功能
开发责任人 谢业福
测试责任人 谢业福
解决时效 8.84
激活次数 0
激活日期
是否确认 是
当前指派 Closed 于 2025-09-12 15:32:54
截止日期 0000-00-00
上线时间 2025-09-09 19:40:00
上线时效 8.85
操作系统
浏览器
关键词
抄送给
所属迭代
相关需求
相关任务
由谁创建 周杰 于 2025-09-09 10:49:15
影响环境 生产环境-业务反馈
由谁解决 谢业福 于 2025-09-09 19:39:41
解决版本 主干
解决方案 性能问题
由谁关闭 周杰 于 2025-09-09 19:43:38
最后修改 陈莉珍 于 2025-09-12 15:32:54
相关Bug