热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

后台查询语句SELECTo.orders_id,s.orders_status_name,ot.text,af.affiliate_idFROMordersoLEFTJOINord

后台查询语句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中文网,转载请注明出处,感谢您的尊重!



推荐阅读
  • vue使用
    关键词: ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
author-avatar
碧落无双2502879687
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有