订单的表结构采用了垂直分表的策略,将订单相关的不同模块的字段维护在不同表中

在订单处理这个页面,需要查询各种维度,

因此为了方便查询创建了v_sale_order视图(老版本)

drop view v_sale_order;
CREATE
VIEW `v_sale_order` AS
SELECT
`so`.`sale_order_id` AS `v_sale_order_id`,
`so`.`sale_order_id` AS `sale_order_id`,
`so`.`sale_order_no` AS `sale_order_no`,
`so`.`order_type` AS `order_type`,
`so`.`platform_order_code2` AS `platform_order_code2`,
`so`.`platform_order_code` AS `platform_order_code`,
`so`.`platform_type` AS `platform_type`,
`so`.`platform_order_status` AS `platform_order_status`,
`so`.`created` AS `created`,
`so`.`end_time` AS `end_time`,
`so`.`total_num` AS `total_num`,
`so`.`total_sku` AS `total_sku`,
`so`.`modified` AS `modified`,
`so`.`seller_flag` AS `seller_flag`,
`so`.`seller_memo` AS `seller_memo`,
`so`.`seller_rate` AS `seller_rate`,
`so`.`snapshot_url` AS `snapshot_url`,
`so`.`status` AS `status`,
`so`.`step_trade_status` AS `step_trade_status`,
`so`.`trade_from` AS `trade_from`,
`so`.`trade_memo` AS `trade_memo`,
`so`.`trade_source` AS `trade_source`,
`so`.`type` AS `type`,
`so`.`shop_id` AS `shop_id`,
`so`.`origin_type` AS `origin_type`,
`so`.`sys_promotion_info` AS `sys_promotion_info`,
`sor`.`buyer_area` AS `buyer_area`,
`sor`.`buyer_email` AS `buyer_email`,
`sor`.`buyer_ip` AS `buyer_ip`,
`sor`.`buyer_memo` AS `buyer_memo`,
`sor`.`buyer_message` AS `buyer_message`,
`sor`.`buyer_nick` AS `buyer_nick`,
`sor`.`buyer_rate` AS `buyer_rate`,
`sor`.`receiver_address` AS `receiver_address`,
`sor`.`receiver_city` AS `receiver_city`,
`sor`.`receiver_country` AS `receiver_country`,
`sor`.`receiver_district` AS `receiver_district`,
`sor`.`receiver_mobile` AS `receiver_mobile`,
`sor`.`receiver_name` AS `receiver_name`,
`sor`.`receiver_phone` AS `receiver_phone`,
`sor`.`receiver_state` AS `receiver_state`,
`sor`.`receiver_town` AS `receiver_town`,
`sor`.`receiver_zip` AS `receiver_zip`,
`sor`.`area_id` AS `area_id`,
`sor`.`customer_id` AS `customer_id`,
`soc`.`courier_id` AS `courier_id`,
`soc`.`courier_order_no` AS `courier_order_no`,
`soc`.`courier_print_mark_state` AS `courier_print_mark_state`,
`soc`.`courier_print_time` AS `courier_print_time`,
`sof`.`alipay_id` AS `alipay_id`,
`sof`.`alipay_no` AS `alipay_no`,
`sof`.`payment` AS `payment`,
`sof`.`total_fee` AS `total_fee`,
`soi`.`invoice_order_no` AS `invoice_order_no`,
`soi`.`invoice_content` AS `invoice_content`,
`soi`.`invoice_type` AS `invoice_type`,
`soi`.`bank` AS `bank`,
`soi`.`title` AS `title`,
`soi`.`bank_account` AS `bank_account`,
`soi`.`tariff_lines` AS `tariff_lines`,
`sos`.`oms_process_type` AS `oms_process_type`,
`sos`.`play_state` AS `play_state`,
`sos`.`pause_state` AS `pause_state`,
`sos`.`stop_state` AS `stop_state`,
`sos`.`archive_state` AS `archive_state`,
`sos`.`is_paid` AS `is_paid`,
`sos`.`is_checked` AS `is_checked`,
`sos`.`is_approved` AS `is_approved`,
`sos`.`is_suspended` AS `is_suspended`,
`sos`.`is_invalidated` AS `is_invalidated`,
`sos`.`is_to_be_shipped` AS `is_to_be_shipped`,
`sos`.`is_after_sale` AS `is_after_sale`,
`sos`.`is_split` AS `is_split`,
`sos`.`is_combined` AS `is_combined`,
`sos`.`is_closed` AS `is_closed`,
`sos`.`is_after_sale_closed` AS `is_after_sale_closed`,
`sos`.`is_amount_changed` AS `is_amount_changed`,
`sos`.`is_part_changed` AS `is_part_changed`,
`sos`.`is_out_of_stock` AS `is_out_of_stock`,
`sos`.`pay_type` AS `pay_type`,
`sos`.`pay_time` AS `pay_time`,
`sos`.`original_order_id` AS `original_order_id`,
`sos`.`after_sale_note` AS `after_sale_note`,
`sos`.`suspend_note` AS `suspend_note`,
`sos`.`unapprove_note` AS `unapprove_note`,
`sos`.`after_sale_type` AS `after_sale_type`,
`sos`.`blacklist_type` AS `blacklist_type`,
`sow`.`warehouse_id` AS `warehouse_id`,
`sow`.`retry_num` AS `retry_num`,
`sow`.`out_warehouse_time` AS `out_warehouse_time`,
`sow`.`purchase_order_no` AS `purchase_order_no`,
`sow`.`purchase_order_id` AS `purchase_order_id`,
`sow`.`wms_order_state` AS `wms_order_state`,
`sow`.`checked_time` AS `checked_time`,
`so`.`creator` AS `creator`,
`so`.`create_time` AS `create_time`,
`so`.`last_updater` AS `last_updater`,
`so`.`last_update_time` AS `last_update_time`,
`so`.`is_usable` AS `is_usable`,
`so`.`tenant_id` AS `tenant_id`
FROM
(
(
(
(
(
(
`sale_order` `so`
LEFT JOIN `sale_order_receiver` `sor` ON (
(
`so`.`sale_order_id` = `sor`.`sale_order_id`
)
)
)
LEFT JOIN `sale_order_status` `sos` ON (
(
`so`.`sale_order_id` = `sos`.`sale_order_id`
)
)
)
LEFT JOIN `sale_order_warehouse` `sow` ON (
(
`so`.`sale_order_id` = `sow`.`sale_order_id`
)
)
)
LEFT JOIN `sale_order_courier` `soc` ON (
(
`so`.`sale_order_id` = `soc`.`sale_order_id`
)
)
)
LEFT JOIN `sale_order_invoice` `soi` ON (
(
`so`.`sale_order_id` = `soi`.`sale_order_id`
)
)
)
LEFT JOIN `sale_order_finance` `sof` ON (
(
`so`.`sale_order_id` = `sof`.`sale_order_id`
)
)
);

之前的代码(老版本):

@Service
public class OrderService extends TemplateService { public static final String DEFALUT_FILTER = " AND NOT(is_split = 1 AND archive_state=3) AND NOT(is_combined = 1 AND archive_state=4) " +
" AND NOT(oms_process_type =0) AND (v_sale_order.platform_order_status != 'TRADE_FINISHED' OR origin_type=2) " +
"AND NOT is_invalidated=1" +
" AND NOT archive_state=5 AND NOT archive_state=6";
public static final String HISTORY_FILTER = " AND NOT(is_split = 1 AND archive_state=3) AND NOT(is_combined = 1 AND archive_state=4) " +
        " AND NOT archive_state=5 AND NOT archive_state=6"; }

DEFAULT_FILTER是订单处理里面,固定的查询条件,每次查询都会有该部分条件,但是sql的写法包含了太多OR,NOT,!= 等操作

优化第一步:  根据业务规则合并一些字段,将一些排除条件改为正向命中的条件(第二版):

@Service
public class OrderService extends TemplateService { /**
订单处理:
过滤掉:合并拆分的订单
过滤掉:交易完成或交易关闭
要求:跑过预处理
要求:已付款或者货到付款
要求:未作废的
*/
public static final String DEFALUT_FILTER = " AND archive_state IN (0, 1) AND v_sale_order.is_paid = 1 AND oms_process_type = 1 " +
" AND v_sale_order.is_invalidated=0 AND is_closed = 0";
/**
* 订单查询:
* 过滤掉:合并拆分的订单
*/
public static final String HISTORY_FILTER = " AND archive_state IN (0, 1)";
public static final String AFTER_FILTER = " AND archive_state IN (0,1,2) AND is_paid=1";
}

优化第二步:   订单处理相比订单查询多了很多固定条件,大部分处于sale_order_status表中,但是之前视图的创建方式固定了最左边的表,因此修改视图创建的脚本,如下:

从固定的left join改为 Join

CREATE OR REPLACE VIEW v_sale_order AS
SELECT
`so`.`sale_order_id` AS `v_sale_order_id`,
`so`.`sale_order_id` AS `sale_order_id`,
`so`.`sale_order_no` AS `sale_order_no`,
`so`.`order_type` AS `order_type`,
`so`.`platform_order_code2` AS `platform_order_code2`,
`so`.`platform_order_code` AS `platform_order_code`,
`so`.`platform_type` AS `platform_type`,
`so`.`platform_order_status` AS `platform_order_status`,
`so`.`created` AS `created`,
`so`.`end_time` AS `end_time`,
`so`.`total_num` AS `total_num`,
`so`.`total_sku` AS `total_sku`,
`so`.`modified` AS `modified`,
`so`.`seller_flag` AS `seller_flag`,
`so`.`seller_memo` AS `seller_memo`,
`so`.`seller_rate` AS `seller_rate`,
`so`.`snapshot_url` AS `snapshot_url`,
`so`.`status` AS `status`,
`so`.`step_trade_status` AS `step_trade_status`,
`so`.`trade_from` AS `trade_from`,
`so`.`trade_memo` AS `trade_memo`,
`so`.`trade_source` AS `trade_source`,
`so`.`type` AS `type`,
`so`.`shop_id` AS `shop_id`,
`so`.`origin_type` AS `origin_type`,
`so`.`sys_promotion_info` AS `sys_promotion_info`,
`sor`.`buyer_area` AS `buyer_area`,
`sor`.`buyer_email` AS `buyer_email`,
`sor`.`buyer_ip` AS `buyer_ip`,
`sor`.`buyer_memo` AS `buyer_memo`,
`sor`.`buyer_message` AS `buyer_message`,
`sor`.`buyer_nick` AS `buyer_nick`,
`sor`.`buyer_rate` AS `buyer_rate`,
`sor`.`receiver_address` AS `receiver_address`,
`sor`.`receiver_city` AS `receiver_city`,
`sor`.`receiver_country` AS `receiver_country`,
`sor`.`receiver_district` AS `receiver_district`,
`sor`.`receiver_mobile` AS `receiver_mobile`,
`sor`.`receiver_name` AS `receiver_name`,
`sor`.`receiver_phone` AS `receiver_phone`,
`sor`.`receiver_state` AS `receiver_state`,
`sor`.`receiver_town` AS `receiver_town`,
`sor`.`receiver_zip` AS `receiver_zip`,
`sor`.`area_id` AS `area_id`,
`sor`.`customer_id` AS `customer_id`,
`soc`.`courier_id` AS `courier_id`,
`soc`.`courier_order_no` AS `courier_order_no`,
`soc`.`courier_print_mark_state` AS `courier_print_mark_state`,
`soc`.`courier_print_time` AS `courier_print_time`,
`sof`.`alipay_id` AS `alipay_id`,
`sof`.`alipay_no` AS `alipay_no`,
`sof`.`payment` AS `payment`,
`sof`.`total_fee` AS `total_fee`,
`soi`.`invoice_order_no` AS `invoice_order_no`,
`soi`.`invoice_content` AS `invoice_content`,
`soi`.`invoice_type` AS `invoice_type`,
`soi`.`bank` AS `bank`,
`soi`.`title` AS `title`,
`soi`.`bank_account` AS `bank_account`,
`soi`.`tariff_lines` AS `tariff_lines`,
`sos`.`oms_process_type` AS `oms_process_type`,
`sos`.`play_state` AS `play_state`,
`sos`.`pause_state` AS `pause_state`,
`sos`.`stop_state` AS `stop_state`,
`sos`.`archive_state` AS `archive_state`,
`sos`.`is_paid` AS `is_paid`,
`sos`.`is_checked` AS `is_checked`,
`sos`.`is_approved` AS `is_approved`,
`sos`.`is_suspended` AS `is_suspended`,
`sos`.`is_invalidated` AS `is_invalidated`,
`sos`.`is_to_be_shipped` AS `is_to_be_shipped`,
`sos`.`is_after_sale` AS `is_after_sale`,
`sos`.`is_split` AS `is_split`,
`sos`.`is_combined` AS `is_combined`,
`sos`.`is_closed` AS `is_closed`,
`sos`.`is_after_sale_closed` AS `is_after_sale_closed`,
`sos`.`is_amount_changed` AS `is_amount_changed`,
`sos`.`is_part_changed` AS `is_part_changed`,
`sos`.`is_out_of_stock` AS `is_out_of_stock`,
`sos`.`pay_type` AS `pay_type`,
`sos`.`pay_time` AS `pay_time`,
`sos`.`original_order_id` AS `original_order_id`,
`sos`.`after_sale_note` AS `after_sale_note`,
`sos`.`suspend_note` AS `suspend_note`,
`sos`.`unapprove_note` AS `unapprove_note`,
`sos`.`after_sale_type` AS `after_sale_type`,
`sos`.`blacklist_type` AS `blacklist_type`,
`sow`.`warehouse_id` AS `warehouse_id`,
`sow`.`retry_num` AS `retry_num`,
`sow`.`out_warehouse_time` AS `out_warehouse_time`,
`sow`.`purchase_order_no` AS `purchase_order_no`,
`sow`.`purchase_order_id` AS `purchase_order_id`,
`sow`.`wms_order_state` AS `wms_order_state`,
`sow`.`checked_time` AS `checked_time`,
`so`.`creator` AS `creator`,
`sos`.`create_time` AS `create_time`,
`so`.`last_updater` AS `last_updater`,
`sos`.`last_update_time` AS `last_update_time`,
`sos`.`is_usable` AS `is_usable`,
`sos`.`tenant_id` AS `tenant_id`
FROM ((((((`sale_order_status` `sos`
JOIN `sale_order_receiver` `sor` ON ((`sos`.`sale_order_id` = `sor`.`sale_order_id`))) JOIN
`sale_order` `so` ON ((`so`.`sale_order_id` = `sos`.`sale_order_id`))) JOIN
`sale_order_warehouse` `sow` ON ((`sos`.`sale_order_id` = `sow`.`sale_order_id`))) JOIN
`sale_order_courier` `soc` ON ((`sos`.`sale_order_id` = `soc`.`sale_order_id`))) JOIN
`sale_order_finance` `sof` ON ((`sos`.`sale_order_id` = `sof`.`sale_order_id`))) LEFT JOIN
`sale_order_invoice` `soi` ON ((`sos`.`sale_order_id` = `soi`.`sale_order_id`)))

最左边表可根据查询条件动态的变化,(如条件过滤查询sale_order_courier的courier_id字段, where courier_id= xx,并且sale_order_courier的courier_id字段上已经建立了索引,那么explain后第一个查询的表就是sale_order_courier)

之前sale_order表始终作为v_sale_order实际查询时的第一个表,而无法走索引

(P.S.本人目前的理解:mysql多表关联查询只有最左边表可以走索引,其余表的索引只能是关联的id作为索引)

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE sos ref idx_sale_order_id,oms_normal_v2,oms_check_v2,oms_suspend_v2 oms_check_v2 10 const,const,const,const 271 Using where; Using filesort
1 SIMPLE sor ref idx_sale_order_id idx_sale_order_id 9 egenie.sos.sale_order_id 1 NULL
1 SIMPLE soc ref idx_sale_order_id idx_sale_order_id 9 egenie.sos.sale_order_id 1 NULL
1 SIMPLE sof ref idx_sale_order_id idx_sale_order_id 9 egenie.sos.sale_order_id 1 NULL
1 SIMPLE so eq_ref PRIMARY PRIMARY 8 egenie.sos.sale_order_id 1 NULL
1 SIMPLE sow ref idx_sale_order_id idx_sale_order_id 9 egenie.sos.sale_order_id 1 NULL
1 SIMPLE soi ref idx_sale_order_id idx_sale_order_id 9 egenie.sos.sale_order_id 1 NULL

随后创建的索引(第一版),生效

--archive_state in()结果太多 走不了索引
CREATE INDEX oms_normal on sale_order_status(tenant_id,is_usable,is_paid,oms_process_type,is_invalidated,is_closed,last_update_time); CREATE INDEX oms_check on sale_order_status(tenant_id,is_usable,is_paid,oms_process_type,is_invalidated,is_closed,
is_checked,last_update_time); CREATE INDEX oms_suspend on sale_order_status(tenant_id,is_usable,is_paid,oms_process_type,is_invalidated,is_closed,
is_suspended,last_update_time); --sale_order
CREATE INDEX shop_idx on sale_order(shop_id,order_type);
CREATE INDEX platform_idx on sale_order(platform_order_status,order_type); --sale_order_warehouse
CREATE INDEX warehouse_idx on sale_order_warehouse(warehouse_id); --sale_order_courier
CREATE INDEX courier_idx on sale_order_courier(courier_id);

由于有新需求需要改造固定的查询sql(第三版)

@Service
public class OrderService extends TemplateService { private static final String isPaySql = " AND (is_paid = 1 OR pay_type = 4 ) ";
/**
* 订单处理:
* 过滤掉:合并拆分的订单
* 过滤掉:交易完成或交易关闭
* 要求:跑过预处理
* 要求:已付款或者货到付款
* 要求:未作废的
*/
public static final String DEFALUT_FILTER = " AND archive_state IN (0, 1) AND oms_process_type = 1 " +
isPaySql + " AND v_sale_order.is_invalidated=0 AND is_closed = 0";
/**
* 订单查询:
* 过滤掉:合并拆分的订单
*/
public static final String HISTORY_FILTER = " AND archive_state IN (0, 1)";
public static final String AFTER_FILTER = " AND archive_state IN (0,1,2) " + isPaySql;
}

改进:

1.将之前的is_paid 移除之前的索引

2.调整索引的顺序,移除毫无辨识度的字段

(第二版)

CREATE INDEX oms_normal_v2 on sale_order_status(tenant_id,is_closed,oms_process_type,is_invalidated,last_update_time);

CREATE INDEX oms_check_v2 on sale_order_status(tenant_id,is_closed,oms_process_type,is_invalidated,is_checked,last_update_time);

CREATE INDEX oms_suspend_v2 on sale_order_status(tenant_id,is_closed,oms_process_type,is_invalidated,is_suspended,last_update_time);

Q&A:

1.MySQL视图可以用索引吗?

我想答案是肯定的,其索引是建立在视图后面的真实表上,而不是建立在视图上.

索引是存放在模式(schema)中的一个数据库对象,索引的作用就是提高对表的检索查询速度,索引是通过快速访问的方法来进行快速定位数据,从而减少了对磁盘的读写操作。索引是数据库的一个对象,它不能独立存在,必须对某个表对象进行依赖。

视图就是一个表或多个表的查询结果,它是一张虚拟的表,因为它并不能存储数据。

mysql优化---订单查询优化:视图优化+索引创建的更多相关文章

  1. mysql优化---订单查询优化(1):视图优化+索引创建

    订单的表结构采用了垂直分表的策略,将订单相关的不同模块的字段维护在不同表中 在订单处理这个页面,需要查询各种维度, 因此为了方便查询创建了v_sale_order视图(老版本) drop view v ...

  2. Mysql基本查询、视图、索引、触发器

    基本查询 1.修改String sql="update smbms_user set userCode=?,userName=? where id=?"; 2.删除用户String ...

  3. mysql优化---订单查询优化:异步分页处理

    订单分页查询: 老的代码是顺序执行查询数据和计算总记录数,但是如果条件复杂的话(比如关联子表)查询的时间要超过20s种 public static PagedList<Map<String ...

  4. mysql优化---订单查询优化(2):异步分页处理

    订单分页查询: 老的代码是顺序执行查询数据和计算总记录数,但是如果条件复杂的话(比如关联子表)查询的时间要超过20s种 public static PagedList<Map<String ...

  5. Mysql的事务、视图、索引、备份和恢复

    事务 事务是作为单个逻辑工作单元执行的一系列操作,一个逻辑工作单元必须具备四个属性.即:原子性.一致性.隔离性.持久性,这些特性通常简称为ACID.   原子性(Atomicity) 事务是不可分割的 ...

  6. MySQL数据库 : 自关联,视图,事物,索引

    自关联查询(自身id关联自身id(主键),查询的时候可以逻辑分为两个表,然后分别起一个别名来区分) select * from areas as cityinner join areas as pro ...

  7. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  8. Mysql单表访问方法,索引合并,多表连接原理,基于规则的优化,子查询优化

    参考书籍<mysql是怎样运行的> 非常推荐这本书,通俗易懂,但是没有讲mysql主从等内容 书中还讲解了本文没有提到的子查询优化内容, 本文只总结了常见的子查询是如何优化的 系列文章目录 ...

  9. MySQL的or/in/union与索引优化

    转载自:MySQL的or/in/union与索引优化 https://blog.csdn.net/zhangweiwei2020/article/details/80005590 假设订单业务表结构为 ...

随机推荐

  1. Delphi String 常用字串符处理函数

    Delphi 在面对跨平台开发,程序语言也改进不少,不过有些改进,让原本 Delphi 开发者有些不适应,最显注的就是字串处理函数了,原本 Pascal 语言字串起始由 1 开始,几乎是它的经典了,新 ...

  2. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  3. Scalaz(54)- scalaz-stream: 函数式多线程编程模式-Free Streaming Programming Model

    长久以来,函数式编程模式都被认为是一种学术研究用或教学实验用的编程模式.直到近几年由于大数据和多核CPU的兴起造成了函数式编程模式在一些实际大型应用中的出现,这才逐渐改变了人们对函数式编程无用论的观点 ...

  4. js中的原型、继承的一些想法

    最近看到一个别人写的js类库,突然对js中的原型及继承产生了一些想法,之前也看过其中的一些内容,但是总不是很清晰,这几天利用空闲时间,对这块理解了一下,感觉还是有不通之处,思路上没那么条理,仅作为分享 ...

  5. SQL数据库: 错误2812 未能找到存储过程 sp_password

    SET QUOTED_IDENTIFIER ON  GO  SET ANSI_NULLS OFF  GO  create procedure sp_password      @old sysname ...

  6. java内存模型-总结

    处理器内存模型 顺序一致性内存模型是一个理论参考模型,JMM 和处理器内存模型在设计时通常会把顺序一致性内存模型作为参照.JMM 和处理器内存模型在设计时会对顺序一致性模型做一些放松,因为如果完全按照 ...

  7. 20款响应式的 HTML5 网页模板【免费下载】

    下面的列表集合了20款响应式的 HTML5 网页模板,这些专业的模板能够让你的网站吸引很多的访客.除了好看的外观,HTML5 模板吸引大家的另一个原因是由于其响应性和流动性.赶紧来看看. 您可能感兴趣 ...

  8. C#开源项目汇总

    Discuz nt: 一个开源的论坛项目.估计你现在逛过大大小小的论坛没有成百上千,也有几十个吧,其中是个论坛6个以上都是Discuz(以前大部分都是php版的),现 在官方也早就放出了DotNet( ...

  9. 深入理解javascript(一)

    此段文章摘自大叔的博客: 此文的目的是书写可维护的javascript代码. 最小的全局变量: JavaScript通过函数管理作用域.在函数内部声明的变量只在这个函数内部,函数外面不可用.另一方面, ...

  10. 利用ARCHPR明文破解获取PDF

    我们经常下载一些rar或zip压缩文件,解压时有时发现要密码,而密码多是为了推广而设置的网址等,如果不知道密码,可 以去来源网站上寻找或在压缩文件的注释中查看. 而并非所有都是如此,例如,网上有些人, ...