mysql 语句优化心得

排序导致性能较慢

优化策略:1.尽量不使用排序 2.只查有索引的结果然后 内连接查询

select  bizchance0_.*  from biz_chance bizchance0_, biz_bizcustomer bizbizcust1_
where bizchance0_.uuid=bizbizcust1_.recordinfoid and bizchance0_.ispublic=1          order by bizchance0_.orderkey desc limit 0,10;

时间 33秒  order by 排序性能较慢 原因:select  bizchance0_.*   如果只查select bizchance0_.uuid uuid带索引 性能提高

select bizchance0_.uuid as uid  from biz_chance bizchance0_, biz_bizcustomer bizbizcust1_
where bizchance0_.uuid=bizbizcust1_.recordinfoid and bizchance0_.ispublic=1   order by bizchance0_.orderkey desc     limit 0,10 ;
时间 3秒

select * from biz_chance as uu inner join (select bizchance0_.uuid as uid  from biz_chance bizchance0_, biz_bizcustomer bizbizcust1_
where bizchance0_.uuid=bizbizcust1_.recordinfoid and bizchance0_.ispublic=1   order by bizchance0_.orderkey desc     limit 0,10   ) as u  on  u.uid=uu.uuid

inner join biz_bizcustomer as cus on uu.uuid=cus.recordinfoid

综合 语句

2.筛选条件、顺序不对

select * from biz_customer  as cus

left join biz_config400 as conf on conf.customerid=cus.uuid
left join biz_billinginfo as bill on bill.configid=conf.uuid and bill.customerid=cus.uuid

用时 15秒

原因:“and bill.customerid=cus.uuid ”

优化结果

select cus.* from biz_customer  as cus

left join biz_config400 as conf on conf.customerid=cus.uuid
left join biz_billinginfo as bill on bill.configid=conf.uuid

where  bill.customerid=cus.uuid or bill.uuid is null

或者

select cus.* from biz_customer  as cus

left join biz_config400 as conf on conf.customerid=cus.uuid
left join biz_billinginfo as bill on bill.configid=conf.uuid

时间: 2024-08-29 22:47:09

mysql 语句优化心得的相关文章

mysql语句优化总结(一)

Sql语句优化和索引 1.Innerjoin和左连接,右连接,子查询 A.     inner join内连接也叫等值连接是,left/rightjoin是外连接. SELECT A.id,A.name,B.id,B.name FROM A LEFT JOIN B ON A.id =B.id; SELECT A.id,A.name,B.id,B.name FROM A RIGHT JOIN ON B A.id= B.id; SELECT A.id,A.name,B.id,B.name FROM

mysql语句优化方案(网上流传)

关于mysql处理百万级以上的数据时如何提高其查询速度的方法 最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当mysql表的数据量达到百万级时,普通SQL查询效率呈直线下降,而且如果where中的查询条件较多时,其查询速度简直无法容忍.曾经测试对一个包含400多万条记录(有索引)的表执行一条条件查询,其查询时间竟然高达40几秒,相信这么高的查询延时,任何用户都会抓狂.因此如何提高sql语句查询效率,显得十分重要.以下是网上

9 MySQL语句优化的原则

1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:  a.有大量重复值.且经常有范围查询( > ,< ,> =,< =)和order by.group by发生的列,可考虑建立群集索引:  b.经常同时存取多列,且每列都含有重复值可考虑建立组合索引:  c.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列.索引虽有助于提高性

mysql 语句优化一列

优化前语句: SELECT ifnull(s.mileage, -1) as mile, s.fuel_hkm as val, t.fhkm_rank as rank, ifnull((select u.photo from app_user u where u.user_id = t.user_id  limit 1),'') as path, case when s.car_id = '***********' then 0 else 1 end as self, ifnull(( SELE

MySQL语句优化的原则

1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说: a.有大量重复值.且经常有范围查询( > ,< ,> =,< =)和order by.group by发生的列,可考虑建立群集索引: b.经常同时存取多列,且每列都含有重复值可考虑建立组合索引: c.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列.索引虽有助于提高性能但不

MySQL语句优化方法(简单版)

基础回顾: sql语句是怎么样运行的? 一般来说,客户端发送sql语句到数据库服务器——数据库服务器进行运算并返回结果——客户端显示sql语句运行结果. 在本地运行时以workbench为例,客户端为workbench,数据库服务器则是安装在本地的mysql数据库. 为什么要优化sql语句? 加快运行速度 减少数据库资源开销 在企业级数据库中sql优化是必备技能. 基础优化技能: 在查询语句中尽量减少查询语句返回结果,尤其是 SELECT*FROMXXX 来返回全部数据.只选择必要的数据,作为返

mysql语句优化技巧

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:select id from t where num=03.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引

MySQL语句优化

一.优化SQL的一般步骤 a)通过SHOW STATUS;命令了解各种SQL的执行频率 SHOW [SESSION|GLOBAL] STATUS; SESSION (默认)表示当前连接 GLOBAL 表示自数据库启动至今 SHOW GLOBAL STATUS LIKE 'com_%' b)查看本次登录以来增删改查的次数 SHOW STATUS LIKE 'com_select%' SHOW STATUS LIKE 'com_update%' SHOW STATUS LIKE 'com_inser

Mysql语句优化小技巧

在使用group by 分组查询是,默认分组后,还会排序(Using filesort),可能会降低速度. 解决办法:加上order by null 不让它排序 select * from emp group by empno order by null; 有些情况下,可以使用连接来替代子查询.因为使用join,MySQL不需要在内存中创建临时表 select * from dept, emp where dept.deptno=emp.deptno; [简单处理方式] select * fro