项目上线后,谈一下感触比较深的一点:查询优化

  • 时间:
  • 浏览:0

前言

  开心一刻

    儿子有道题我这麼多 做,喊我过去教他。我推了推一旁的老公:我头疼,你去吧。老公不动,我:零花钱涨一千。话音刚落,老公就屁颠屁颠跑去儿子房间。进去不到几分钟,一声怒吼伴随着儿子的哭声传来的瞬间,老公从儿子房间出来,边走边说:“朽木不可雕也。”儿子从房间探出半个身子,一脸委屈:“爸爸这麼来越多这麼来越多会做,你爱不爱我给我一块钱,给你 明天早点去学校抄同学的。还给你 未必告诉你,我不肯,他就吼我。”

问提报告 背景

  前段时间,被紧急调到两个 新项目,支撑新项目的开发。跌跌撞撞之下,项目也正常上线了,期间收获颇多,无论是业务上的,还是业务之外的。业务上的就这麼来越多说了,不具通用性,意义不大,有這個业务之外的东东给我的感触比较深,特记录下来,与亲戚亲戚亲们分享下 : 查询优化

查询优化

  全版示例工程:data-init,包括数据库表的 ddl 和 dml,以及数据批量的生成

  相关表

    涉及的表这麼来越多,一共三张:额度表、记录表 、 存款表

    额度表 t_custmor_credit

CREATE TABLE t_customer_credit (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  login_name VARCHAR(3000) NOT NULL COMMENT '名称',
    credit_type TINYINT(1) NOT NULL COMMENT '额度类型,1:自由资金,2:冻结资金,3:优惠',
    amount DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '额度值',
    create_by VARCHAR(3000) NOT NULL COMMENT '创建者',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
    update_by VARCHAR(3000) NOT NULL COMMENT '修改者',
  PRIMARY KEY (id)
);
View Code

      记录每个顾客的当前额度,额度一共分一种:自由资金、冻结资金和优惠,就是多这麼来越多说每个顾客会有 3 条记录来表示他的各个额度。表中数据如下

    额度记录 t_custmor_credit_record

CREATE TABLE t_customer_credit_record (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  login_name VARCHAR(3000) NOT NULL COMMENT '名称',
    credit_type TINYINT(1) NOT NULL COMMENT '额度类型,参考t_custmor_credit的credit_type',
    bill_no VARCHAR(3000) NOT NULL COMMENT '订单号',
    amount_before DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '前额度值',
    amount_change DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '额度变化值',
    amount_after DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '后额度值',
    create_by VARCHAR(3000) NOT NULL COMMENT '创建者',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    remark VARCHAR(30000) NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (id)
);
View Code

      记录顾客额度的每一次变化,只要有额度变化(不管是哪个额度进行了变化),回会新增3条记录,每个类型的额度回会新增第三根记录。另外,该表只会有数据的插入,我这麼多 有数据的删、改。表中数据如下

    存款表 t_custmor_deposit

CREATE TABLE t_customer_deposit (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  login_name VARCHAR(3000) NOT NULL COMMENT '名称',
    bill_no VARCHAR(3000) NOT NULL COMMENT '订单号',
    amount DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '存款金额',
    deposit_state TINYINT(1) NOT NULL COMMENT '存款情況: 1成功,2失败,3未知',
    channal TINYINT(2) NOT NULL COMMENT '存款渠道: 1:银联,2支付宝,3微信',
    create_by VARCHAR(3000) NOT NULL COMMENT '创建者',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    remark VARCHAR(30000) NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (id)
);
View Code

      记录顾客的每一次存款,该表只会有数据的插入,我这麼多 有数据的删、改。表中数据如下

  产品需求

    分页展示如下列表(具体的过滤条件就不列了,亲戚亲戚亲们就当是初始情況,还未输入过滤条件)

     实现比较简单,t_custmor_credit_record 左关联 t_custmor_deposit 就好,有但是亲戚亲戚亲们的额度记录表与需求列表這個许的出入,后能 做一下简单的行转列。

  需求实现

    亲戚亲戚亲们先来看看最初的SQL查询,这肯能是这麼来越多这麼来越多人最容易想到的

SELECT MIN(tcd.channal) channal, MAX(tccr.id) mId,tccr.login_name,tccr.bill_no,tccr.create_time,
    IF(credit_type=1,amount_before,0) AS freeBefore,
    IF(credit_type=1,amount_change,0) AS freeChange,
    IF(credit_type=1,amount_after,0) AS freeAfter,
    IF(credit_type=2,amount_before,0) AS freezeBefore,
    IF(credit_type=2,amount_change,0) AS freezeChange,
    IF(credit_type=2,amount_after,0) AS freezeAfter,
    IF(credit_type=3,amount_before,0) AS promotionBefore,
    IF(credit_type=3,amount_change,0) AS promotionChange,
    IF(credit_type=3,amount_after,0) AS promotionAfter
FROM t_customer_credit_record tccr
LEFT JOIN t_customer_deposit tcd ON tccr.bill_no = tcd.bill_no
GROUP BY tccr.bill_no,tccr.login_name,tccr.create_time
ORDER BY mId desc
LIMIT 0, 10;
View Code

    数据量少的另两个 ,你爱不爱给你 在亲戚亲戚亲们接受的时间内查出亲戚亲戚亲们后能 的结果,一旦数据量多了,這個SQL就跑不动了;亲戚亲戚亲们先看下 300w 数据的情況下,亲戚亲戚亲们只进行 t_custmor_credit_record 单表查询

SELECT MAX(id) mId,login_name,bill_no,create_time,
    IF(credit_type=1,amount_before,0) AS freeBefore,
    IF(credit_type=1,amount_change,0) AS freeChange,
    IF(credit_type=1,amount_after,0) AS freeAfter,
    IF(credit_type=2,amount_before,0) AS freezeBefore,
    IF(credit_type=2,amount_change,0) AS freezeChange,
    IF(credit_type=2,amount_after,0) AS freezeAfter,
    IF(credit_type=3,amount_before,0) AS promotionBefore,
    IF(credit_type=3,amount_change,0) AS promotionChange,
    IF(credit_type=3,amount_after,0) AS promotionAfter
FROM t_customer_credit_record
GROUP BY bill_no,login_name,create_time
ORDER BY mId desc
LIMIT 0, 10;
View Code

    效果如下

    花了近 8 秒,这还这麼来越多这麼来越多单表,肯能执行顶端的联表SQL,那时间又得增加不少(我试验的结果是直接卡住了,看不到查询结果);

  优化处理

    加索引

      查询慢的另两个 ,亲戚亲戚亲们最容易想到的优化最好的土办法往往这麼来越多这麼来越多加索引;上述SQL执行的另两个 ,t_custmor_credit_record 和 t_custmor_deposit都没哟 建索引(主键索引除外),没哟 亲戚亲戚亲们就加索引呗。我的项目中加的是唯一索引,做了唯一约束,另两个 这里也加唯一索引

ALTER TABLE t_customer_credit_record ADD UNIQUE uk_unique (bill_no,login_name,create_time,credit_type);
ALTER TABLE t_customer_deposit ADD UNIQUE uk_billno (bill_no);

      此时亲戚亲戚亲们看下SQL执行效果

      亲戚亲戚亲们发现,t_custmor_credit_record 单表查询的传输速率几乎没变,将近 8 秒,但 t_custmor_credit_record 与 t_custmor_deposit 联表的查询却在 11 秒内有结果了。加了索引为那些还没哟 慢了? 难道没走索引?

      亲戚亲戚亲们是回会 发现了那些? IF函数对联表查询有无走索引有影响,也对单表的查询传输速率有影响。上图中的 t_custmor_credit_record 单表查询,有IF函数,查询时间近 8 秒,没哟 IF函数,查询时间 2 秒左右;t_custmor_credit_record 与 t_custmor_deposit 联表查,有IF函数,t_custmor_credit_record 走的是全表查,查询时间近 11 秒,没哟 IF函数,t_custmor_credit_record 走的是索引,查询时间 3 秒不到。没哟 亲戚亲戚亲们有没哟 那些最好的土办法拿掉這個IF函数呢?

    使用 CASE...WHEN....THEN 代替 IF

SELECT MAX(id) mId,login_name,bill_no,create_time,
    CASE credit_type WHEN 1 THEN amount_before ELSE 0 END AS freeBefore,
    CASE credit_type WHEN 1 THEN amount_change ELSE 0 END AS freeChange,
    CASE credit_type WHEN 1 THEN amount_after ELSE 0 END AS freeAfter,
    CASE credit_type WHEN 2 THEN amount_before ELSE 0 END AS freeChange,
    CASE credit_type WHEN 2 THEN amount_change ELSE 0 END AS freeChange,
    CASE credit_type WHEN 2 THEN amount_after ELSE 0 END AS freeChange,
    CASE credit_type WHEN 3 THEN amount_before ELSE 0 END AS promotionBefore,
    CASE credit_type WHEN 3 THEN amount_change ELSE 0 END AS promotionChange,
    CASE credit_type WHEN 3 THEN amount_after ELSE 0 END AS promotionAfter
FROM t_customer_credit_record
GROUP BY bill_no,login_name,create_time
ORDER BY mId desc
LIMIT 0, 10;

SELECT MIN(tcd.channal) channal, MAX(tccr.id) mId,tccr.login_name,tccr.bill_no,tccr.create_time,
    CASE credit_type WHEN 1 THEN amount_before ELSE 0 END AS freeBefore,
    CASE credit_type WHEN 1 THEN amount_change ELSE 0 END AS freeChange,
    CASE credit_type WHEN 1 THEN amount_after ELSE 0 END AS freeAfter,
    CASE credit_type WHEN 2 THEN amount_before ELSE 0 END AS freeChange,
    CASE credit_type WHEN 2 THEN amount_change ELSE 0 END AS freeChange,
    CASE credit_type WHEN 2 THEN amount_after ELSE 0 END AS freeChange,
    CASE credit_type WHEN 3 THEN amount_before ELSE 0 END AS promotionBefore,
    CASE credit_type WHEN 3 THEN amount_change ELSE 0 END AS promotionChange,
    CASE credit_type WHEN 3 THEN amount_after ELSE 0 END AS promotionAfter
FROM t_customer_credit_record tccr
LEFT JOIN t_customer_deposit tcd ON tccr.bill_no = tcd.bill_no
GROUP BY tccr.bill_no,tccr.login_name,tccr.create_time
ORDER BY mId desc
LIMIT 0, 10;
View Code

      亲戚亲戚亲们都后能 想看 ,执行时间与 IF 所差无几,执行计划也是与 IF 的一致,这也就反映出回会 IF的问提报告 ,应该是 GROUP BY 的问提报告 。亲戚亲戚亲们用 GROUP BY 结合 IF(或 CASE...WHEN....THEN),这麼来越多这麼来越多为了将 3 条额度记录合并成第三根、行转列另两个 输出亲戚亲戚亲们我这麼多 的结果,那有没哟 我这麼多  GROUP BY、又能实现亲戚亲戚亲们需求的最好的土办法了?

    自联代替 GROUP BY

      亲戚亲戚亲们再仔细琢磨下這個需求,咋一看,我我其实后能 行转列,没哟 就后能 用到 GROUP BY,没哟 传输速率也就低了,这似乎是无解了? 真的不到行转列吗,假设亲戚亲戚亲们将额度记录拆分成 3 张表:一张表只存自由资金的额度变化、一张表只存冻结资金的额度变化、一张表只存优惠的额度变化,另两个 是回会 只后能 联表查而未必用 GROUP BY 来进行行转列了? 有小伙伴有肯能会问:t_custmor_credit_record 表肯能定了,数据都跑了不少了,再将其进行拆分,既要改表(一同后能 迁移数据),后能 改代码,工程量会很大! 亲戚亲戚亲们换个深度1来看 t_custmor_credit_record ,目前它是 3 中额度记录的两个 总和表,亲戚亲戚亲们都后能 从它的身上做文章,变化出亲戚亲戚亲们我这麼多 的那 3 张表,有但是进行联表查询呢? 肯定都后能 的,类事如下

-- 自由资金额度记录表
SELECT * FROM t_customer_credit_record WHERE credit_type = 1;
-- 冻结资金额度记录表
SELECT * FROM t_customer_credit_record WHERE credit_type = 2;
-- 优惠额度记录表
SELECT * FROM t_customer_credit_record WHERE credit_type = 3;

      接下来的 SQL 为啥写,给你 亲戚亲戚亲们都知道了吧,自联就行了,写法有这麼来越多这麼来越多种,常见的写法有如下 4 种

-- 我这麼多

group by,做法1, 另一方比较推荐, 但此种最好的土办法不支持存款表的过滤条件
SELECT d.channal,a.amount_before AS freeBefore,a.amount_change AS freeChange, a.amount_after freeAfter,
    b.amount_before AS freezeBefore,b.amount_change AS freezeChange, b.amount_after freezeAfter,
    c.amount_before AS promotionBefore,c.amount_change AS promotionChange, c.amount_after promotionAfter
FROM (
    SELECT * FROM t_customer_credit_record WHERE credit_type = 1 ORDER BY id DESC LIMIT 0, 10
) a
LEFT JOIN t_customer_credit_record b ON a.bill_no = b.bill_no AND b.credit_type = 2
LEFT JOIN t_customer_credit_record c ON a.bill_no = c.bill_no AND c.credit_type = 3
LEFT JOIN t_customer_deposit d ON a.bill_no = d.bill_no;

-- 我这麼多

group by,做法2, 此种最好的土办法支持存款表的过滤条件
SELECT a.channal,a.amount_before AS freeBefore,a.amount_change AS freeChange, a.amount_after freeAfter,
    b.amount_before AS freezeBefore,b.amount_change AS freezeChange, b.amount_after freezeAfter,
    c.amount_before AS promotionBefore,c.amount_change AS promotionChange, c.amount_after promotionAfter
FROM (
    SELECT r.*,d.channal FROM t_customer_credit_record r LEFT JOIN t_customer_deposit d ON r.bill_no = d.bill_no
    WHERE r.credit_type = 1 ORDER BY r.id DESC LIMIT 0, 10
) a
LEFT JOIN t_customer_credit_record b ON a.bill_no = b.bill_no AND b.credit_type = 2
LEFT JOIN t_customer_credit_record c ON a.bill_no = c.bill_no AND c.credit_type = 3;

-- 我这麼多

group by,做法3, 这是最容易想到的最好的土办法
SELECT d.channal,a.amount_before AS freeBefore,a.amount_change AS freeChange, a.amount_after freeAfter,
    b.amount_before AS freezeBefore,b.amount_change AS freezeChange, b.amount_after freezeAfter,
    c.amount_before AS promotionBefore,c.amount_change AS promotionChange, c.amount_after promotionAfter
FROM t_customer_credit_record a
LEFT JOIN t_customer_credit_record b ON a.bill_no = b.bill_no
LEFT JOIN t_customer_credit_record c ON a.bill_no = c.bill_no
LEFT JOIN t_customer_deposit d ON a.bill_no = d.bill_no
WHERE a.credit_type = 1 AND b.credit_type = 2 AND c.credit_type = 3 
ORDER BY a.id DESC LIMIT 0, 10;

-- 我这麼多

group by,做法4
SELECT d.channal,a.amount_before AS freeBefore,a.amount_change AS freeChange, a.amount_after freeAfter,
    b.amount_before AS freezeBefore,b.amount_change AS freezeChange, b.amount_after freezeAfter,
    c.amount_before AS promotionBefore,c.amount_change AS promotionChange, c.amount_after promotionAfter
FROM t_customer_credit_record a
LEFT JOIN t_customer_credit_record b ON a.bill_no = b.bill_no AND b.credit_type = 2
LEFT JOIN t_customer_credit_record c ON a.bill_no = c.bill_no AND c.credit_type = 3
LEFT JOIN t_customer_deposit d ON a.bill_no = d.bill_no
WHERE a.credit_type = 1 
ORDER BY a.id DESC LIMIT 0, 10;
View Code

      执行结果如下

      就目前的数据量而言,4 种写法的传输速率一样,有但是数据量再往上走,它们另两个 还是有性能差别的,亲戚亲戚亲们都后能 仔细看看这 4 个 SQL 的执行计划,它们之间还是有区别的。最终我的项目中采用的是第一种写法

    表重新设计

      亲戚亲戚亲们回过头去看看 t_customer_credit 和 t_custmor_credit_record,有无真的有必要用 3 条记录来存放顾客的 3 种额度,第三根记录将用户的 3 种额度都记录下来回会 更好吗? 如下所示

-- 自认为更好的表设计
DROP TABLE IF EXISTS t_customer_credit_plus;
CREATE TABLE t_customer_credit_plus (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  login_name VARCHAR(3000) NOT NULL COMMENT '登录名',
    free_amount DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '自由资金额度',
    freeze_amount DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '冻结资金额度',
    promotion_amount DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '优惠资金额度',
    create_by VARCHAR(3000) NOT NULL COMMENT '创建者',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
    update_by VARCHAR(3000) NOT NULL COMMENT '修改者',
  PRIMARY KEY (id),
  UNIQUE KEY `uk_login_name` (`login_name`)
);
DROP TABLE IF EXISTS t_customer_credit_record_plus;
CREATE TABLE t_customer_credit_record_plus (
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  login_name VARCHAR(3000) NOT NULL COMMENT '登录名',
    bill_no VARCHAR(3000) NOT NULL COMMENT '订单号',
    free_amount_before DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '自由资金前额度值',
    free_amount_change DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '自由资金前额度变化值',
    free_amount_after DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '自由资金前后额度值',
    freeze_amount_before DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '冻结资金前额度值',
    freeze_amount_change DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '冻结资金额度变化值',
    freeze_amount_after DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '冻结资金后额度值',
    promotion_amount_before DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '优惠前额度值',
    promotion_amount_change DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '优惠额度变化值',
    promotion_amount_after DECIMAL(22,6) NOT NULL DEFAULT '0.00000' COMMENT '优惠后额度值',
    create_by VARCHAR(3000) NOT NULL COMMENT '创建者',
    create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    remark VARCHAR(30000) NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (id),
  UNIQUE KEY `uk_unique` (`bill_no`,`login_name`,`create_time`)
);
View Code

      给你 这麼来越多这麼来越多人回会有相同的感觉吧,有但是从拓展性出发,分 3 条记录的做法更好,为那些呢? 肯能后续新增 1 种或多种另外的额度类型,上述的 1 条记录的表设计就后能 新增字段来适配了, 有但是 3 条记录的做法,只后能 拓展credit_type的值就好了,表我这麼多 改动。各有利弊,如保选折 ,后能 团队协商另两个 做出最好的选折 。

      最终项目中采用的还是 3 条记录存放 3 个额度的最好的土办法,没哟 采用你爱不爱我的;原困 是:亲戚亲戚亲们都认为传输速率影响不大,也容易理解,关键是拓展性很好,后续很方便就能加入新的额度类型。

    业务上的协调

      最后亲戚亲戚亲们再回到需求上来,這個 存款渠道 真的有必要显示在额度记录吗?

      1、对公司来说,存款记录这麼来越多,那肯定是越好,但亲戚亲戚亲们从实际出发,存款记录在额度记录中占的比例大吗,這個相信亲戚亲戚亲们也都能想象得到,比例非常低,肯能 3000 条记录中会有 1 条;

      2、另两个 回会 单独的存款页面展示顾客的存款,去专门的存款记录页面看岂回会 更直观?

      最后和产品讨论,还真把這個列给拿掉了,没哟 亲戚亲戚亲们也就不到关联存款表来查了,SQL 更简单,传输速率也更高了!

总结

  1、Oracle 的 DECODE 与 Mysql 中的 IF 回会 CASE 表达式的比较复杂函数,具有特殊性(不到 Oracle 才认识 DECODE、不到 Mysql 才认识 IF),而回会 标准的 SQL,CASE 表达式是标准 SQL 所承认的功能;

  2、SQL 行转列,往往是 GROUP BY 配合聚合函数(SUM、MAX、MIN等)来实现,当然也包括 IF 和 CASE...WHEN....THEN; 

  3、索引是提高查询传输速率的最有效的、也是最常用的最好的土办法,亲戚亲戚亲们对查询的优化回会 往索引上靠,EXPLAIN 都后能 查看SQL的执行计划,亲戚亲戚亲们都后能 从中获取SQL优化的提示;

  4、一定要结合业务来写出高效的SQL

    肯能这麼来越多这麼来越多小伙伴会有另两个 的不满:上述的 3 个额度的例子有点特殊,不具备通用性,上述高效的SQL就是多这麼来越多在你(楼主)的项目包含效。你爱不爱我的对,有但是亲戚亲戚亲们要知道,技术一种这麼来越多这麼来越多用来服务业务的,脱离了业务,技术有那些实际意义? 有但是亲戚亲戚亲们回过头去细看,我举的例子真的就特殊到独一无二? 给你 还算比较通用吧,还是能套用这麼来越多这麼来越多场景的。

  5、要敢于质疑需求

    我其实大多数另两个 产品都考虑比较周到,但这麼来越多这麼来越多能全版保证他没哟 不犯迷糊的另两个 吧。有疑惑就向产品问清楚,亲戚亲戚亲们实现的也很快、更准确。一定未必对问提报告 藏着掖着,以我亲身经历来讲,这麼来越多这麼来越多另两个 开发认为的回会 对的,肯能藏着掖着,那你就准备返工改成你另两个 认为的那样吧!