程序是数据库的用户,为打造良好的用户体验,我们一直在努力。

此次介绍一个基于SQL的数据库优化。SQL的优劣对数据库的性能影响非常关键。

查询只涉及如下表结构中的三个字段。如下

开发原始SQL

SELECT
            f002_ths043 AS 'navDate', f003_ths043 AS 'recentNav', f001_ths043 AS 'fundCode'
        FROM
        (
            SELECT
                info.*,
                @rn := IF(@pre = f001_ths043, @rn + 1, 1) AS rn,
                @pre := f001_ths043
            FROM
            (
                SELECT * FROM ths043
                WHERE f001_ths043 IN ( '040008','050006','050106','150001','150002','150006','150007','150010','150011','162209','184688','184689','184690','184691','184692','184693','184695','184696','184698','184699','184700','184701','184702','184703','184705','184706','184708','184709','184710','184711','184712','184713','184718','184719','184720','184721','184722','184728','184738','200008','360007','500001','500002','500003','500005','500006','500007','500008','500009','500010','500011','500013','500015','500016','500017','500018','500019','500021','500025','500028','500029','500035','500038','500039','500056','500058','519694','590002','620001','CX0001','FB0001','J00023','J00108','J00112','J00117','J00118','J00120','J00127','J00128','J00130','J00141','J00144','J00149','J00153','J00154','J00164','J00246','J00327','J00328','J00329','J00330','J00331','J00332','J00333','J00334','J00335','J00336','J00337','J00338','J00339','J00340','J00341','J00342','J00343','J00344','J02337','J02338','J02339','J02340','J02341','J02342','J02343','J02344','J02345','J02346','J02347','J02349','J02350','J02351','J02352','J02353','J02354','J02355','J02356','J02357','J02358','J02359','J02360','J02361','J02362','J02363','J02364','J02365','J02366','J02367','J02368','J02369','J02370','J02371','J02372','J02373','J02374','J02375','J02376','J02377','J02378','J02540','J02541','J02542','J02550','J02551','J02552','J02553','J02554','J02555','J02556','J02557','J02558','J02559','J02560','J02561','J02562','J02563','J02564','J02565','J02566','J02567','J02568','J02569','J02570','J02571','J02573','J02575','J02576','J02577','J02579','J02580','J02581','J02583','J02584','J02585','J02586','J02587','J02588','J02589','J02590','J02591','J02592','J02593','J02595','J02596','J02597','J02598','J02599','J02600','J02601','J02602','J02603','J02604','J02605','J02606','J02607','J02608','J02609','J02610','J02611','J02612','J02613','J02614','J02615','J02616','J02617','J02618','J02619','J02620','J02621','J02622','J02623','J02624','J02625','J02626','J02627','J02629','J02630','J02631','J02632','J02633','J02634','J02635','J02636','J02637','J02638','J02639','J02640','J02641','J02642','J02643','J02644','J02647','J02650','J02651','J02652','J02653','J02654','J02657','J02658','J02692','J02701','J02705','J02715','J02717','J02718','J02722','J02723','J02724','J02725','J02727','J02728','J02729','J02730','J02737','J02740','J02741','J02744','KF0001','KF0002','KF0003','KF0004','KF0005','KF0006','KF0007','KF0008','KF0009','KF0010','KF0011','KF0012','KF0013','KF0014','KF0015','KF0016','KF0017','KF0018','KF0019','KF0020','KF0021','KF0022','KF0023','KF0024','KF0025','KF0026','KF0027','KF0028','KF0029','KF0030','KF0031','KF0032','KF0033','KF0034','KF0035','KF0036')
                    AND f002_ths043 IS NOT NULL AND f003_ths043 IS NOT NULL
                ORDER BY f001_ths043, f002_ths043 DESC
            ) info
            JOIN (SELECT @rn := 0, @pre := 0) AS vars
        ) r
        WHERE rn = 1;

其目的为了寻找指定基金代码中,最后的价格。。

其实对于这种需求,在我个人角度来看,有很多中解决办法。

1、如果价格变更非常频繁,可以在应用层使用一张表记录当前(最后一次)价格,每次更新该表。附加一张记录表用于存储价格波动趋势。这样查询第一张表即可满足要求,速度相当快,收益比100%。

2、如果变更不频繁,可以将两张表合成一张。将一次查询的结果插入到一张新的临时表中,或者load进缓存,触发更新临时表,刷新缓存。由于更新不频繁,原表的数据查询也不会很频繁。多数情况下会查询临时表。

3、如果没有如果,就是想要效率高又省事,那么需要改写SQL。如下:(其中我将原本一个f001的索引,改成f001,f002,f003的复合索引。)

SELECT t1.*,t2.f003_ths043 FROM
(
SELECT f001_ths043,max(f002_ths043) f002_ths043 FROM ths043      WHERE f001_ths043 IN ( '040008','050006','050106','150001','150002','150006','150007','150010','150011','162209','184688','184689','184690','184691','184692','184693','184695','184696','184698','184699','184700','184701','184702','184703','184705','184706','184708','184709','184710','184711','184712','184713','184718','184719','184720','184721','184722','184728','184738','200008','360007','500001','500002','500003','500005','500006','500007','500008','500009','500010','500011','500013','500015','500016','500017','500018','500019','500021','500025','500028','500029','500035','500038','500039','500056','500058','519694','590002','620001','CX0001','FB0001','J00023','J00108','J00112','J00117','J00118','J00120','J00127','J00128','J00130','J00141','J00144','J00149','J00153','J00154','J00164','J00246','J00327','J00328','J00329','J00330','J00331','J00332','J00333','J00334','J00335','J00336','J00337','J00338','J00339','J00340','J00341','J00342','J00343','J00344','J02337','J02338','J02339','J02340','J02341','J02342','J02343','J02344','J02345','J02346','J02347','J02349','J02350','J02351','J02352','J02353','J02354','J02355','J02356','J02357','J02358','J02359','J02360','J02361','J02362','J02363','J02364','J02365','J02366','J02367','J02368','J02369','J02370','J02371','J02372','J02373','J02374','J02375','J02376','J02377','J02378','J02540','J02541','J02542','J02550','J02551','J02552','J02553','J02554','J02555','J02556','J02557','J02558','J02559','J02560','J02561','J02562','J02563','J02564','J02565','J02566','J02567','J02568','J02569','J02570','J02571','J02573','J02575','J02576','J02577','J02579','J02580','J02581','J02583','J02584','J02585','J02586','J02587','J02588','J02589','J02590','J02591','J02592','J02593','J02595','J02596','J02597','J02598','J02599','J02600','J02601','J02602','J02603','J02604','J02605','J02606','J02607','J02608','J02609','J02610','J02611','J02612','J02613','J02614','J02615','J02616','J02617','J02618','J02619','J02620','J02621','J02622','J02623','J02624','J02625','J02626','J02627','J02629','J02630','J02631','J02632','J02633','J02634','J02635','J02636','J02637','J02638','J02639','J02640','J02641','J02642','J02643','J02644','J02647','J02650','J02651','J02652','J02653','J02654','J02657','J02658','J02692','J02701','J02705','J02715','J02717','J02718','J02722','J02723','J02724','J02725','J02727','J02728','J02729','J02730','J02737','J02740','J02741','J02744','KF0001','KF0002','KF0003','KF0004','KF0005','KF0006','KF0007','KF0008','KF0009','KF0010','KF0011','KF0012','KF0013','KF0014','KF0015','KF0016','KF0017','KF0018','KF0019','KF0020','KF0021','KF0022','KF0023','KF0024','KF0025','KF0026','KF0027','KF0028','KF0029','KF0030','KF0031','KF0032','KF0033','KF0034','KF0035','KF0036') GROUP BY f001_ths043 ) t1 LEFT JOIN ths043 t2 ON t1.f001_ths043=t2.f001_ths043 AND t1.f002_ths043 = t2.f002_ths043 ;

由原来8S提速到0.07s。对于SQL的优化就是如此立竿见影,也应当如此。

但是,并非每条SQL都可以优化,如果业务就是想要查单表中的1000万条记录,没有任何逻辑,条件等。。这种SQL在数据库层的优化效率提升微乎其微。相反,业务逻辑层的优化成效很大。

以上优化过程使用到了很多关于数据库内部的特性。如需交流,或者有更好意见,qq 475982055

千万不可写成以下这种。虽说条条路可以通罗马,但愿不要经过西天。

SELECT
*
FROM
(
SELECT
a.f001_ths043,
a.f002_ths043,
a.f003_ths043,
(
SELECT
count(*)
FROM
ths043 b
WHERE
a.f002_ths043 <= b.f002_ths043
AND a.f001_ths043 = b.f001_ths043
) AS row_number
FROM
ths043 a
) a
WHERE
a.row_number = 1;
;

记一次SQL优化。的更多相关文章

  1. 记一次SQL优化

    常见的SQL优化 一.查询优化 1.避免全表扫描 模糊查询前后加%也属于全表扫描 在where子句中对字段进行表达式操作会导致引擎放弃使用索引而进行全表扫描,如: select id from t w ...

  2. 记一次Sql优化过程

    这几天在写一个存储过程,反复优化了几次,从最开始的7分钟左右,优化到最后的几秒,并且这个过程中我的导师帮我指点了很多问题,这些指点都是非常宝贵的,独乐乐不如众乐乐,一起来分享这次的优化过程吧. 这个存 ...

  3. 记一次sql优化——left join不走索引问题

    sql一执行就卡住,然后就...杀进程了 看了一下表的大小 第一反应就是加索引,然后explain看了一下走什么索引了,结果很尴尬,三个表,只走了一个索引...一群人在那纠结为毛走不了索引. 无意间发 ...

  4. 记一次SQL Server delete语句的优化过程

    今天测试反应问题,性能测试环境一个脚本执行了3个小时没有出结果,期间其他dba已经建立了一些索引但是没有效果. 语句: DELETE T  from License T  WHERE exists ( ...

  5. 提高SQL查询效率(SQL优化)

    要提高SQL查询效率where语句条件的先后次序应如何写 http://blog.csdn.net/sforiz/article/details/5345359   我们要做到不但会写SQL,还要做到 ...

  6. sql优化(oracle)

    系统优化中很重要的方面是SQL语句的优化,对于海量数据,优质的SQL能够有效的提高系统的可用性. 总结的有点罗嗦,列个简单的目录啦~ 目录 第一部分知识准备                      ...

  7. sql优化的50中方法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...

  8. sql优化--in和exists效率

    系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not exists 修改方法如下: in的SQL语句 SELEC ...

  9. 浅谈sql优化

    问题的发现:      菜鸟D在工作的时候发现项目的sql语句很怪,例如 : select a.L_ZTBH, a.D_RQ, a.VC_BKDM, (select t.vc_name from tb ...

随机推荐

  1. Entity framework 更改模型,新增表

    在Package Manager Console 中运行命令Enable-Migrations 再次运行可以更新 抄袭 在实体类中增加一个属性以后,执行 Update-Database 命令 ,可以更 ...

  2. 通过.NET客户端调用Web API(C#)

    3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...

  3. SyncML是一平台无关的信息同步标准协议集

    SyncML (Synchronization Markup Language)是一平台无关的信息同步标准协议集.分为SyncML数据传输协议(SyncML-DS)和SyncML设备管理协议(Sync ...

  4. SQLServer2008-2012开启远程连接的配置方法

    一.远程连接端口设置(很关键的一步)1.在服务器上打开SQL Server Configuration Manager.选择SQL Server配置管理器->SQL Server 网络配置-&g ...

  5. 调用其它UI文件

    调用其它UI文件 首先 要头文件包含  #include “dialog_biaozhun.h"Dialog_biaozhun *dialog = new Dialog_biaozhun() ...

  6. C、C++笔记

    2017年6月 阅读书籍<C和指针> #if 0 #endif 比注释掉代码好.(<C和指针>2017.06.07) 全局变量和全局静态变量的区别 1)全局变量是不显式用sta ...

  7. QT 线程池 + TCP 小试(三)实现最终功能

    *免分资源链接点击打开链接http://download.csdn.net/detail/goldenhawking/4492378 有了TCP.线程池,我们就可以把他们连接起来.使用最简单的 QMa ...

  8. c# Lambda扩展

    扩展类 public static class LinqExtensions { /// <summary> /// 创建lambda表达式:p=>true /// </sum ...

  9. excel导入到数据库的异常处理

    excel导入到数据库,这个是经常发生的,今天就碰到了一个非常郁闷的事情,在导入到oracle数据的时候,总是出现ORA-01756: 引号内的字符串没有正确结束,认真的排插了数据当中可能出现的错误, ...

  10. sql小计汇总 rollup用法实例分析

    这里介绍sql server2005里面的一个使用实例: ),city ),score int) GO 1. 只有一个汇总 select province as 省,sum(score) as 分数 ...