php order by where,无合适where条件过滤时尽量选择order by后的字段以驱动表进行查询...

后台查询语句SELECTo.orders_id,s.orders_status_name,ot.text,af.affiliate_idFROMordersoLEFTJOINorders_totalotON(o.orders_id=ot.orders_id)LEFTJOINaffilia

后台查询语句SELECT o.orders_id, s.orders_status_name, ot.text ,af.affiliate_id

FROM orders o

LEFT JOIN orders_total ot ON (o.orders_id = ot.orders_id)

LEFT JOIN affiliate_sales AS afs ON afs.affiliate_orders_id = o.orders_id

LEFT JOIN affiliate_affiliate AS af ON af.affiliate_id = afs.affiliate_id

LEFT JOIN orders_status s ON o.orders_status = s.orders_status_id

WHERE

s.language_id = '1'

AND (ot.class = 'ot_total' OR ot.orders_total_id IS NULL)

ORDER BY o.orders_id DESC LIMIT 0, 20

有客户反应某后台查询非常慢,通过程序找到对应的sql,如上!

explain发现+----+-------------+-------+--------+----------------------------+----------------------------+---------+-----------------------------+-------+----------------------------------------------+

| id | select_type | table | type | possible_keys

| key

| key_len | ref

| rows | Extra

|

+----+-------------+-------+--------+----------------------------+----------------------------+---------+-----------------------------+-------+----------------------------------------------+

| 1 | SIMPLE

| s

| ALL | PRIMARY

| NULL

| NULL | NULL

| 21 | Using where; Using temporary; Using filesort |

| 1 | SIMPLE

| o

| ref | orders_status

| orders_status

| 4

| banggood.s.orders_status_id | 31747 |

|

| 1 | SIMPLE

| ot | ref | idx_orders_total_orders_id | idx_orders_total_orders_id | 4

| banggood.o.orders_id

| 19 | Using where

|

| 1 | SIMPLE

| afs | ref | PRIMARY

| PRIMARY

| 4

| banggood.o.orders_id

| 11 | Using index

|

| 1 | SIMPLE

| af | eq_ref | PRIMARY

| PRIMARY

| 4

| banggood.afs.affiliate_id |

1 | Using index

|

+----+-------------+-------+--------+----------------------------+----------------------------+---------+-----------------------------+-------+----------------------------------------------+

s表被作为驱动表,s表为全表扫描,o表使用了status类型的可选择性非常低的字段作为索引。

初步一看就知道索引使用不恰当!

我们可以看到这条语句where条件中,没有什么合适的可驱动条件;但是,在order by中,发现order by o.orders_id(orders_id为orders表的主键)。我们就可以利用这个特性!

强制使用orders表的orders_id索引进行驱动!

更改如下:EXPLAIN SELECT o.orders_id, s.orders_status_name, ot.text ,af.affiliate_id

FROM orders o FORCE INDEX(PRIMARY)

LEFT JOIN orders_total ot ON (o.orders_id = ot.orders_id)

LEFT JOIN affiliate_sales AS afs ON afs.affiliate_orders_id = o.orders_id

LEFT JOIN affiliate_affiliate AS af ON af.affiliate_id = afs.affiliate_id

LEFT JOIN orders_status s ON o.orders_status = s.orders_status_id

WHERE

s.language_id = '1'

AND (ot.class = 'ot_total' OR ot.orders_total_id IS NULL)

ORDER BY o.orders_id DESC LIMIT 0, 20;

+----+-------------+-------+--------+----------------------------+----------------------------+---------+--------------------------------+------+-------------+

| id | select_type | table | type | possible_keys

| key

| key_len | ref

| rows | Extra

|

+----+-------------+-------+--------+----------------------------+----------------------------+---------+--------------------------------+------+-------------+

| 1 | SIMPLE

| o

| index | NULL

| PRIMARY

| 4

| NULL

| 1 |

|

| 1 | SIMPLE

| s

| eq_ref | PRIMARY

| PRIMARY

| 8

| banggood.o.orders_status,const | 1 | Using where |

| 1 | SIMPLE

| ot | ref | idx_orders_total_orders_id | idx_orders_total_orders_id | 4

| banggood.o.orders_id

| 19 | Using where |

| 1 | SIMPLE

| afs | ref | PRIMARY

| PRIMARY

| 4

| banggood.o.orders_id

| 11 | Using index |

| 1 | SIMPLE

| af | eq_ref | PRIMARY

| PRIMARY

| 4

| banggood.afs.affiliate_id

| 1 | Using index |

+----+-------------+-------+--------+----------------------------+----------------------------+---------+--------------------------------+------+-------------+

对比两次profiling;

前者:+--------------------------------+------------+-----------+------------+--------------+---------------+

| Status

| Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |

+--------------------------------+------------+-----------+------------+--------------+---------------+

| starting

| 0.000027 | 0.000000 | 0.000000 |

0 |

0 |

| Waiting for query cache lock | 0.000006 | 0.000000 | 0.000000 |

0 |

0 |

| checking query cache for query | 0.000130 | 0.000000 | 0.000000 |

0 |

0 |

| checking permissions

| 0.000007 | 0.000000 | 0.000000 |

0 |

0 |

| checking permissions

| 0.000003 | 0.000000 | 0.000000 |

0 |

0 |

| checking permissions

| 0.000003 | 0.000000 | 0.000000 |

0 |

0 |

| checking permissions

| 0.000003 | 0.000000 | 0.000000 |

0 |

0 |

| checking permissions

| 0.000007 | 0.000000 | 0.000000 |

0 |

0 |

| Opening tables

| 0.000130 | 0.000000 | 0.000000 |

0 |

8 |

| System lock

| 0.000017 | 0.000000 | 0.000000 |

0 |

0 |

| Waiting for query cache lock | 0.000033 | 0.000000 | 0.000000 |

0 |

0 |

| init

| 0.000057 | 0.000000 | 0.000000 |

0 |

0 |

| optimizing

| 0.000026 | 0.000000 | 0.000000 |

0 |

0 |

| statistics

| 0.000041 | 0.000000 | 0.000000 |

0 |

0 |

| preparing

| 0.000031 | 0.000000 | 0.000000 |

0 |

0 |

| Creating tmp table

| 0.000111 | 0.001000 | 0.000000 |

0 |

0 |

| executing

| 0.000007 | 0.000000 | 0.000000 |

0 |

0 |

| Copying to tmp table

| 3.541123 | 0.968852 | 2.357642 |

75800 |

0 |

| converting HEAP to MyISAM

| 0.239566 | 0.038994 | 0.198969 |

0 |

262152 |

| Copying to tmp table on disk | 174.185144 | 13.864893 | 35.361625 |

2135152 |

2500280 |

| Sorting result

| 20.923419 | 0.127980 | 3.017541 |

2770408 |

27536 |

| Sending data

| 0.045078 | 0.000000 | 0.002999 |

1208 |

0 |

| end

| 0.000018 | 0.000000 | 0.000000 |

0 |

0 |

| removing tmp table

| 0.881884 | 0.018997 | 0.160976 |

760 |

8 |

| end

| 0.003960 | 0.000000 | 0.002000 |

448 |

0 |

| query end

| 0.000012 | 0.000000 | 0.000000 |

0 |

0 |

| closing tables

| 0.031745 | 0.000000 | 0.000999 |

936 |

0 |

| freeing items

| 0.015499 | 0.000000 | 0.003000 |

808 |

0 |

| Waiting for query cache lock | 0.000017 | 0.000000 | 0.000000 |

0 |

0 |

| freeing items

| 0.000791 | 0.000000 | 0.000000 |

0 |

0 |

| Waiting for query cache lock | 0.000009 | 0.000000 | 0.000000 |

0 |

0 |

| freeing items

| 0.000003 | 0.000000 | 0.000000 |

0 |

0 |

| storing result in query cache | 0.000009 | 0.000000 | 0.000000 |

0 |

0 |

| logging slow query

| 0.000003 | 0.000000 | 0.000000 |

0 |

0 |

| logging slow query

| 0.000010 | 0.000000 | 0.000000 |

0 |

0 |

| cleaning up

| 0.000007 | 0.000000 | 0.000000 |

0 |

0 |

+--------------------------------+------------+-----------+------------+--------------+---------------+

各种cpu,io损耗,惨不忍睹!其中最大的消耗是Copying to tmp table on disk。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360486.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

零垃圾创建数百万个对象

如性能优化第一规则中所述,垃圾是快速代码的敌人。 通过使用垃圾收集器的服务,它不仅会破坏任何形式的确定性性能,而且我们开始在CPU高速缓存中填充垃圾,这将导致程序的高速缓存未命中。 那么,我们可以在不创建垃圾的…

[算法]单链表专题

如何判断链表环的入口位置? 一个指针从头开始单步走,一个指针从第一次相遇位置开始单步走,再相遇的位置就是环入口,证明如下: 设链表头到环入口位置距离为a,入口位置到第一次相遇位置为b,相遇位…

批准Oracle IDM中的特定Web服务

关于Web服务端点的快速发布,OIM和SOA在与批准有关的场景中使用了Web服务端点- 基本内容,但对于初学者可能有用 。 Oracle IDM与SOA套件集成并利用其提供与批准相关的功能(说实话,SOA相当丰富,并且也被用作Web服务连接…

Oracle15001,Oracle11gR2RAC环境DBCA创建数据库报错ORA-15055ORA-15001

在Oracle 11gR2 GridInfrastructure和Database软件安装完成之后,执行DBCA创建数据库到30%的时候报如下错误,点击OK后提示忽略并问题现象:在Oracle 11gR2 GridInfrastructure和Database软件安装完成之后,执行DBCA创建数据库到30%的时候报如下错…

linux 下访问mysql

1:先进到root:/# /usr/local/mysql/bin/2:root:/# mysql -u root -p Enter password: 转载于:https://www.cnblogs.com/gaoyinghui/p/3255148.html

针对新手的Java EE7和Maven项目–第8部分

第1部分 , 第2部分 , 第3部分 , 第4部分 , 第5部分 , 第6部分 , 第7部分 第8部分 自上一篇文章以来,这一系列教程已经有很长时间了。 是时候恢复并在我们的简单项目中添加新功能了。 正…

oracle_home path,ORACLE_HOME迁移后需要设置LD_LIBRARY_PATH环境变量

而设置LD_LIBRARY_PATH后,问题解决:[orat3hpserver2 ~]$ export LD_LIBRARY_PATH$ORACLE_HOME/lib[orat3hpserver2 ~]$ sqlplus / AS sysdbaSQL*Plus: Release 10.2.0.4.0 - Production ON Sun Mar 18 16:10:57 2012Copyright (c) 1982, 2007, Oracle. A…

栈的链式存储及其基本运算

#include <stdio.h> #include <stdlib.h> #define M 10typedef struct stnode {char data;struct stnode *next; }LinkStack;void InitStack(LinkStack *&ls) //初始化栈 {lsNULL; }void PushStack(LinkStack *&ls,char x)//进栈 {LinkStack *p;p(LinkSta…

oracle的导出参数statistic,使用expdp导出时评估所需存储容量大小

我们在使用expdp进行数据导出时&#xff0c;可以事先评估需要存储大小容量(bytes)&#xff0c;Oracle可以通过两种方式进行容量估算:[more]1)、通过数据块数量2)、通过统计信息中记录的内容估算具体是通过制定参数estimate_only和estimate来评估导出的性能参数estimate_onlyy|n…

玩Weld-Probe –一站式查看CDI的所有方面

焊接3.0.0.Alpha4被释放 &#xff0c;而我一直坐在在DevConf.CZ一间会议室。 Jozef Hartinger&#xff08; jozefhartinger &#xff09;或多或少地在几分钟前告诉我有关此最新版本的新功能的信息。 有一个特别的功能真正引起了我的注意&#xff0c;它是新的焊接探针机制。 什…

排列、组合问题(递归)

这里主要介绍字符串排列组合问题,高中数学常见的题目,不用详细介绍&#xff0c;看例子就可以解决问题 "1212" 全排列结果为 1212&#xff0c;1221&#xff0c;1122&#xff0c;2112&#xff0c;2121&#xff0c;2211 组合结果是 1,2,12 我所理解的排列组合结果是…

oracle日志文件大小规则,修改oracle日志文件大小

1、创建2个新的日志组alter database add logfile group 4 (D:\ORACLE\ORADATA\ORADB\REDO04_1.LOG) size 1024k;alter database add logfile group 5 (D:\ORACLE\ORADATA\ORADB\REDO05_1.LOG) size 1024k;2、切换当前日志到新的日志组alter system switch logfile;alter syste…

Java开发工具可以促进编程!

Java开发人员通常尝试找到快速有效地编写高质量Java代码的方法&#xff0c;以使他们的编程工作更轻松。 由于情况发生了变化&#xff0c;因此出现了越来越多的工具。 因此&#xff0c;下面列出了大多数开发人员已经使用&#xff0c;将来使用或一定会使用的有用工具。 该列表包括…

linux cmake装在自己目录下,如何在Linux下安装cmake

全部展开OpenCV 2.2和更高版本需要使用Cmake生成生成文件&#xff0c;因此需要先安装cmake. 还有其他需要先安装cmake的软件1. 在Linux环境中打开Web浏览器&#xff0c;输入URL:mac cmake gui&#xff0c;找到最新版本的位置. 通常&#xff0c;发布了两个版本的开源软件: “源分…

Java Bootstrap:Dropwizard与Spring Boot

如何在尽可能短的时间内使准备就绪的Java应用程序投入生产&#xff1f; 我不是一个早起的人&#xff0c;所以有时需要一些时间才能启动“所有系统”提示。直到不久之前&#xff0c;这对于Java应用程序来说都是正确的&#xff0c;但是与发明贪睡功能不同闹钟&#xff0c;我们将在…

linux 查看libusb版本,linux / libusb获取usb设备路径

我使用libusb来枚举一些usb设备.现在我想获得“设备路径”.我认为这不是usb device-path,因为我没有成功使用谷歌.如果我用linux连接usb设备,我会在dmesg中收到一条消息,这里有一些带有usb温度传感器的“设备路径”的例子(类似于this)&#xff1a;H_301_3直接到usb端口&#xf…

如何使用Apache Drill分析高度动态的数据集

当今的数据是动态的&#xff0c;并由应用程序驱动。 由诸如Web /社交/移动/ IOT等行业趋势驱动的新业务应用时代的增长正在生成具有新数据类型和新数据模型的数据集。 这些应用程序是迭代的&#xff0c;并且关联的数据模型通常是半结构化的&#xff0c;无模式的且不断发展的。 …

MVC中不能使用原生态的#include ,可替代的解决方案

<!--#include file"../stuff/foo/box.aspx"--> 1.可以用 <%: Html.Partial("~/Views/foo/box.ascx") %>OR <% Html.RenderPartial("~/Views/foo/box.ascx"); %> 2. Html.Raw(File.ReadAllText(Server.MapPath("~/html/te…

linux备份日志文件脚本,Linux篇:Shell脚本实现Gitlab双备份

01 前言最近成功从架构组拿到了Gitlab的管理权限&#xff0c;第一件事就是想着如何备份&#xff0c;以防数据丢失背大锅&#xff0c;于是在网上搜索一番&#xff0c;发现一段非常赞的备份脚本&#xff0c;记录照着操作一下&#xff1a;尤其是第二篇文章博主&#xff0c;有非常多…

物理数据模型(PDM)-概念数据模型 (CDM)-面向对象模型 (OOM):适用于已经设计好数据库表结构了。...

步骤如下&#xff1a; 一、反向生成物理数据模型PDM 开发环境 PowerDesigner 15 ,SQL Server2005 &#xff08;1&#xff09;在开始逆向生成PDM图之前&#xff0c;需要为指定的数据库创建ODBC数据源。以Windows xp操作系统为例&#xff0c;选择“开始”/“运行”命令&#xff0…