mysql sql优化<1>
<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>的更多相关文章
- mysql sql优化实例
mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...
- Mysql SQL优化&执行计划
SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...
- 18.Mysql SQL优化
18.SQL优化18.1 优化SQL语句的一般步骤 18.1.1 通过show status命令了解各种SQL的执行频率show [session|global] status; -- 查看服务器状态 ...
- mysql sql优化及注意事项
sql优化分析 通过slow_log等方式可以捕获慢查询sql,然后就是减少其对io和cpu的使用(不合理的索引.不必要的数据访问和排序)当我们面对具体的sql时,首先查看其执行计划A.看其是否使用索 ...
- MySQL sql优化(摘抄自文档)
前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...
- MySQL SQL优化
一.优化数据库的一般步骤: (A) 通过 show status 命令了解各种SQL的执行频率. (B) 定位执行效率较低的SQL语句,方法两种: 事后查询定位:慢查询日志:--log-slow-qu ...
- MySQL SQL优化之in与range查询【转】
本文来自:http://myrock.github.io/ 首先我们来说下in()这种方式的查询.在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效 ...
- MySql Sql 优化技巧分享
有天发现一个带inner join的sql 执行速度虽然不是很慢(0.1-0.2),但是没有达到理想速度.两个表关联,且关联的字段都是主键,查询的字段是唯一索引. sql如下: SELECT p_it ...
- mysql sql优化实例1(force index使用)
今天和运维同学一块查找mysql慢查询日志,发现了如下一条sql: SELECT sum(`android` + ios) total,pictureid,title,add_time FROM `j ...
- 一个MySql Sql 优化技巧分享
有天发现一个带inner join的sql 执行速度虽然不是很慢(0.1-0.2),但是没有达到理想速度.两个表关联,且关联的字段都是主键,查询的字段是唯一索引. sql如下: SELECT p_it ...
随机推荐
- 浅谈C#抽象类和C#接口
原文地址:http://www.cnblogs.com/zhxhdean/archive/2011/04/21/2023353.html 一.C#抽象类: C#抽象类是特殊的类,只是不能被实例化:除此 ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- Listen第二个参数的意义
今天主要回顾下listen的第二个参数的意义. 话说现在现在都是用框架写业务代码.真的很少在去关注最基本的socket函数的意义了.该忘得都忘得差不多了.~~~ 要慢慢捡起来. 主要是在看redi ...
- css+javascript 写的HTML5 微信端输入支付密码键盘
微信端没有纯数字键盘,用html5写了一个模仿ios输入支付密码键盘效果 keyboard.js var _keyboard = {}; $(document).ready(function(){ _ ...
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- Apache+Subversion+TortoiseSVN
Key words: dav_svn, apache, subversion, tortoisesvn # install apache2 sudo apt-get install libapache ...
- (3)选择元素——(16)延伸阅读(Further reading)
The topic of selectors and traversal methods will be explored in more detail in Chapter 9. A complet ...
- dojo 学习笔记之dojo.query - query(id) 与query(class)的差别
考虑这个样例:动态创建一个页面的时候,用new listtem()生成多个listitem, 且每一个listitem中都生成一个按钮button. 假设想要给每一个按钮都绑定一个click事件,用d ...
- [Falcor] Return the data from server
<!-- index.html --> <html> <head> <!-- Do _not_ rely on this URL in production. ...
- 《Linux Device Drivers》第十二章 PCI司机——note
一个简短的引论 它给这一章总线架构的高级概述 集中访问讨论Peripheral Component Interconnect(PCI,外围组件互连)外设内核函数 PCI公交车是最好的支持的内核总线 本 ...