加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL索引原理及慢查询优化

发布时间:2016-09-27 18:25:29 所属栏目:MySql教程 来源:站长网
导读:副标题#e# MySQL凭借着出色的性能、低廉的成本、丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库。虽然性能出色,但所谓好马配好鞍,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位描述上看到诸如精通MySQL、SQL语句优化

发现排序之前居然锁定了778878条记录,如果针对70万的结果集排序,将是灾难性的,怪不得这么慢,那我们能不能换个思路,先根据contact的created_time排序,再来join会不会比较快呢?

于是改造成下面的语句,也可以用straight_join来优化

select

c.id,

c.name,

c.position,

c.sex,

c.phone,

c.office_phone,

c.feature_info,

c.birthday,

c.creator_id,

c.is_keyperson,

c.giveup_reason,

c.status,

c.data_source,

from_unixtime(c.created_time) as created_time,

from_unixtime(c.last_modified) as last_modified,

c.last_modified_user_id

from

contact c

where

exists (

select

1

from

contact_branch cb

inner join

branch_user bu

on cb.branch_id = bu.branch_id

and bu.status in (

1,

2)

inner join

org_emp_info oei

on oei.data_id = bu.user_id

and oei.node_left >= 2875

and oei.node_right <= 10802

and oei.org_category = - 1

where

c.id = cb.contact_id

)

order by

c.created_time desc limit 0 ,

10;

验证一下效果 预计在1ms内,提升了13000多倍!

10 rows in set (0.00 sec)

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读