<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. Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)

    C. Tavas and Pashmaks   Tavas is a cheerleader in the new sports competition named "Pashmaks&qu ...

  2. eclipse svn插件安装

    1,在线直接安装 help --> Eclipse Marketplace 2,下载subclipse.zip 把features.plugins拷贝到eclipse安装目录 3,直接把subc ...

  3. 【转】opencv检测运动物体的基础_特征提取

    特征提取是计算机视觉和图像处理中的一个概念.它指的是使用计算机提取图像信息,决定每个图像的点是否属于一个图像特征.特征提取的结果是把图像上的点分为不同的子集,这些子集往往属于孤立的点.连续的曲线或者连 ...

  4. 【Python基础】计算项目代码行数

    统计代码行数 # coding: utf-8 import os import sys import time def get_line_count(file_path): ""& ...

  5. MySQL添加外键的方法

    为book表添加外键: <1>明确指定外键的名称: 语法:alter table 表名 add constraint 外键的名称 foreign key(你的外键字段名) REFERENC ...

  6. getChars的使用方法

    <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.ut ...

  7. Android ListView 滚动的N种方法

    Android 里面让ListView滚动有N种方法,这儿列举三种: 我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现 ...

  8. mysql中DES加密解密

      DES_DECRYPT(crypt_str[,key_str]) 使用DES_ENCRYPT()加密一个字符串.若出现错误,这个函数会返回 NULL. 注意,这个函数只有当MySQL在SSL 的支 ...

  9. 局域网指定 IP 地址后无法上网的问题

    子网掩码.默认网关.DNS 与局域网设置有关,建议指定前先 运行 cmd -> ipconfig /all 查看一下自动获取的信息. 另外留意指定IP 后需打开高级设置 -> WINS,勾 ...

  10. NSUserDefaults读取和写入自定义对象

    NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...