<pre name="code" class="html">explain SELECT t.*  FROM
(
SELECT
t1.sn AS clientSn,
t1.userNick,
t1.mobilePhone,
t3.personName,
t2.availableBalance,
(SELECT IFNULL(SUM(amount) , 0) FROM ClientRechargeOrder t WHERE t.clientSn= t1.sn AND t.status ='2') AS rechargeAmount,
(SELECT IFNULL(SUM(amount) , 0) FROM ClientWithDrawOrder t WHERE t.clientSn= t1.sn AND t.status IN ('1','2','3','4') ) AS withdrawAmount,
( (SELECT IFNULL(SUM(capitalBalance) , 0) FROM ProductRepayment t WHERE t.clientSn= t1.sn AND t.status= '1')
+
(SELECT IFNULL(SUM(capitalBalance) , 0) FROM VirtualProductOrder t WHERE t.clientSn= t1.sn AND t.status= '1')
) AS investAmount,
( (SELECT IFNULL(SUM(yieldBalance) , 0) FROM ProductRepayment t WHERE t.clientSn= t1.sn AND t.status= '2')
+
(SELECT IFNULL(SUM(yieldBalance) , 0) FROM VirtualProductOrder t WHERE t.clientSn= t1.sn AND t.status= '2')
) AS yieldAmount,
(SELECT IFNULL(SUM(t0.amount) , 0) FROM ClientCoupon t,Coupon t0 WHERE t.clientSn= t1.sn AND t.status = '2' AND t.couponSn = t0.sn AND t0.type IN (1,2)) AS cashCouponAmount
FROM Client t1 , ClientAssetInfo t2 , ClientPersonalInfo t3
WHERE t1.sn = t2.clientSn AND t1.sn = t3.clientSn
) t WHERE (t.rechargeAmount + t.yieldAmount + t.cashCouponAmount - t.withdrawAmount - t.investAmount - t.availableBalance) != 0; +----+--------------------+------------+--------+---------------+---------+---------+-----------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+---------------+---------+---------+-----------------+-------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2327 | Using where |
| 2 | DERIVED | t1 | ALL | PRIMARY | NULL | NULL | NULL | 2327 | NULL |
| 2 | DERIVED | t3 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t1.sn | 1 | NULL |
| 2 | DERIVED | t2 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t1.sn | 1 | NULL |
| 9 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 9 | DEPENDENT SUBQUERY | t0 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t.couponSn | 1 | Using where |
| 8 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 7 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 12890 | Using where |
| 6 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 5 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 12890 | Using where |
| 4 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 2126 | Using where |
| 3 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 5786 | Using where |
+----+--------------------+------------+--------+---------------+---------+---------+-----------------+-------+-------------+
12 rows in set (0.00 sec) 添加3个索引:
mysql> create index ProductRepayment_idx1 on ProductRepayment(clientSn); mysql> create index ClientRechargeOrder_idx1 on ClientRechargeOrder(clientSn);
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> create index ClientWithDrawOrder_idx1 on ClientWithDrawOrder(clientSn);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0 create index ClientCoupon_idx1 on ClientCoupon(clientSn);
修改后的执行计划: mysql> explain SELECT t.* FROM ( SELECT t1.sn AS clientSn, t1.userNick, t1.mobilePhone, t3.personName, t2.availableBalance, (SELECT IFNULL(SUM(amount) , 0) FROM ClientRechargeOrder t WHERE t.clientSn= t1.sn AND t.status ='2') AS rechargeAmount, (SELECT IFNULL(SUM(amount) , 0) FROM ClientWithDrawOrder t WHERE t.clientSn= t1.sn AND t.status IN ('1','2','3','4') ) AS withdrawAmount, ( (SELECT IFNULL(SUM(capitalBalance) , 0) FROM ProductRepayment t WHERE t.clientSn= t1.sn AND t.status= '1') + (SELECT IFNULL(SUM(capitalBalance) , 0) FROM VirtualProductOrder t WHERE t.clientSn= t1.sn AND t.status= '1') ) AS investAmount, ( (SELECT IFNULL(SUM(yieldBalance) , 0) FROM ProductRepayment t WHERE t.clientSn= t1.sn AND t.status= '2') + (SELECT IFNULL(SUM(yieldBalance) , 0) FROM VirtualProductOrder t WHERE t.clientSn= t1.sn AND t.status= '2') ) AS yieldAmount, (SELECT IFNULL(SUM(t0.amount) , 0) FROM ClientCoupon t,Coupon t0 WHERE t.clientSn= t1.sn AND t.status = '2' AND t.couponSn = t0.sn AND t0.type IN (1,2)) AS cashCouponAmount FROM Client t1 , ClientAssetInfo t2 , ClientPersonalInfo t3 WHERE t1.sn = t2.clientSn AND t1.sn = t3.clientSn ) t WHERE (t.rechargeAmount + t.yieldAmount + t.cashCouponAmount - t.withdrawAmount - t.investAmount - t.availableBalance) != 0;
+----+--------------------+------------+--------+--------------------------+--------------------------+---------+-----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+--------------------------+--------------------------+---------+-----------------+------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2327 | Using where |
| 2 | DERIVED | t1 | ALL | PRIMARY | NULL | NULL | NULL | 2327 | NULL |
| 2 | DERIVED | t3 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t1.sn | 1 | NULL |
| 2 | DERIVED | t2 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t1.sn | 1 | NULL |
| 9 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 9 | DEPENDENT SUBQUERY | t0 | eq_ref | PRIMARY | PRIMARY | 4 | zjzc.t.couponSn | 1 | Using where |
| 8 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 7 | DEPENDENT SUBQUERY | t | ref | ProductRepayment_idx1 | ProductRepayment_idx1 | 4 | zjzc.t1.sn | 1 | Using where |
| 6 | DEPENDENT SUBQUERY | t | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
| 5 | DEPENDENT SUBQUERY | t | ref | ProductRepayment_idx1 | ProductRepayment_idx1 | 4 | zjzc.t1.sn | 1 | Using where |
| 4 | DEPENDENT SUBQUERY | t | ref | ClientWithDrawOrder_idx1 | ClientWithDrawOrder_idx1 | 4 | zjzc.t1.sn | 1 | Using where |
| 3 | DEPENDENT SUBQUERY | t | ref | ClientRechargeOrder_idx1 | ClientRechargeOrder_idx1 | 4 | zjzc.t1.sn | 1 | Using where |
+----+--------------------+------------+--------+--------------------------+--------------------------+---------+-----------------+------+-------------+
12 rows in set (0.00 sec)
												

mysql sql优化<1>的更多相关文章

  1. mysql sql优化实例

    mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...

  2. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  3. 18.Mysql SQL优化

    18.SQL优化18.1 优化SQL语句的一般步骤 18.1.1 通过show status命令了解各种SQL的执行频率show [session|global] status; -- 查看服务器状态 ...

  4. mysql sql优化及注意事项

    sql优化分析 通过slow_log等方式可以捕获慢查询sql,然后就是减少其对io和cpu的使用(不合理的索引.不必要的数据访问和排序)当我们面对具体的sql时,首先查看其执行计划A.看其是否使用索 ...

  5. MySQL sql优化(摘抄自文档)

    前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...

  6. MySQL SQL优化

    一.优化数据库的一般步骤: (A) 通过 show status 命令了解各种SQL的执行频率. (B) 定位执行效率较低的SQL语句,方法两种: 事后查询定位:慢查询日志:--log-slow-qu ...

  7. MySQL SQL优化之in与range查询【转】

    本文来自:http://myrock.github.io/ 首先我们来说下in()这种方式的查询.在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效 ...

  8. MySql Sql 优化技巧分享

    有天发现一个带inner join的sql 执行速度虽然不是很慢(0.1-0.2),但是没有达到理想速度.两个表关联,且关联的字段都是主键,查询的字段是唯一索引. sql如下: SELECT p_it ...

  9. mysql sql优化实例1(force index使用)

    今天和运维同学一块查找mysql慢查询日志,发现了如下一条sql: SELECT sum(`android` + ios) total,pictureid,title,add_time FROM `j ...

  10. 一个MySql Sql 优化技巧分享

    有天发现一个带inner join的sql 执行速度虽然不是很慢(0.1-0.2),但是没有达到理想速度.两个表关联,且关联的字段都是主键,查询的字段是唯一索引. sql如下: SELECT p_it ...

随机推荐

  1. IMS 相关名词解释

    IMS: IMS(IP Multimedia Subsystem)是IP多媒体系统,是一种全新的多媒体业务形式,它能够满足现在的终端客户更新颖.更多样化多媒体业务的需求. RCS:Rich Commu ...

  2. JS中undefined与null的区别

    1.概述: 在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? ...

  3. Jsp中response对象的所有属性

    所属接口:javax.servlet.http.HttpServletResponse,其父接口是ServletResponse,而且ServletResponse也现在只有唯一一个HttpServl ...

  4. winrar3.7-winrar4.0的注冊码

    首先新建记事本文件(txt文件),把下面红色代码复制进去,然后将文件另存为以 rarreg.key 为文件名称的文件(当然因为设置的不同,可能出现你保存后的文件为 rarreg.key.txt 没关系 ...

  5. Myeclipse7.5 下载 安装 注冊 注冊码 100%成功

    myeclipse7.5启动画面 1.下载Myeclipse官方原版 官方原版:或者 http://downloads.myeclipseide.com/downloads/products/ewor ...

  6. 自动生成 Lambda查询和排序,从些查询列表so easy

    如下图查询页面,跟据不同条件动态生成lambda的Where条件和OrderBy,如果要增加或调整查询,只用改前台HTML即可,不用改后台代码 前台代码: <div style="pa ...

  7. EF中的自动追踪与代理

    自动追踪 EF框架会自动追踪实体的变化(通过比较实体的当前值与原始值). 默认情况下,以下方法会自动触发实体变化的追踪 DbSet.Find DbSet.Local DbSet.Remove DbSe ...

  8. SqlCommand.Parameters.add()方法

    SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间:  System.Data.SqlClient 程序集:  Syst ...

  9. (转)OS X 升級後 MacPorts 重新安裝筆記

    原地址:http://blog.lyhdev.com/2012/07/os-x-macports.html Mac OS X 10.8 Mountain Lion 正式發佈,而且祭出台幣 $590 元 ...

  10. Python新手学习基础之函数-return语句与函数调用

    return语句 return语句的写法是: return 表达式 return语句用于退出函数,选择性地向调用方返回一个表达式.return在不带参数的情况下,默认返回None. None是一个特殊 ...