0

0

sql数据库语句优化分析和优化技巧总结(sql优化工具)

php是最好的语言

php是最好的语言

发布时间:2018-08-03 17:37:09

|

9909人浏览过

|

来源于php中文网

原创

通常sql数据库需要进行优化分析,并且还有一定的技巧,sql优化的几种方法这里就不做详细介绍了,本文将会sql语句优化进行总结,后面还附了优化工具SQL Tuning Expert for Oracle及使用方法,首先我们要遵随数据库优化的几个原则:

1.尽量避免在列上做运算,这样会导致索引失败;

2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query。不然join的越多表,就会导致越多的锁定和堵塞。

3.注意like模糊查询的使用,避免使用%%,例如select * from a where name like '%de%';

代替语句:select * from a where name >= 'de' and name

4.仅列出需要查询的字段,不要使用select * from ...,节省内存;

5.使用批量插入语句,节省交互;

insert into a (id ,name)
values(2,'a'),
(3,'s');

6.limit基数比较大时,使用between ... and ...

7.不要使用rand函数随机获取记录;

8.避免使用null ,这就需要在建表时,尽量设置为not null,提升查询性能;

9,不要使用count(id),而应该是count(*)

10.不要做无谓的排序,尽可能在索引中完成排序;

我们先来看一个sql:

 select
                    ii.product_id, 
                    p.product_name, 
                    count(distinct pim.pallet_id) count_pallet_id, 
                    if(round(sum(itg.quantity),2) > -1 && round(sum(itg.quantity),2) < 0.005, 0, round(sum(itg.quantity),2)) quantity,
                    round(ifnull(sum(itag.locked_quantity), 0.00000),2) locked_quantity,
                    pc.container_unit_code_name,
                    if(round(sum(itg.qoh),2) > -1 && round(sum(itg.qoh),2) < 0.005, 0, round(sum(itg.qoh),2)) qoh,
                    round(ifnull(sum(itag.locked_qoh), 0.00000),2) locked_qoh,
                    p.unit_code,
                    p.unit_code_name
                from (select 
                        it.inventory_item_id item_id, 
                        sum(it.quantity) quantity, 
                        sum(it.real_quantity) qoh 
                    from 
                        ws_inventory_transaction it
                    where 
                        it.enabled = 1 
                    group by 
                        it.inventory_item_id  
                    ) itg 
                    left join (select 
                                    ita.inventory_item_id item_id, 
                                    sum(ita.quantity) locked_quantity, 
                                    sum(ita.real_quantity) locked_qoh 
                               from 
                                    ws_inventory_transaction_action ita
                               where 
                                    1=1 and ita.type in ('locked', 'release') 
                               group by 
                                    ita.inventory_item_id 
                               )itag on itg.item_id = itag.item_id
                    inner join ws_inventory_item ii on itg.item_id = ii.inventory_item_id 
                    inner join ws_pallet_item_mapping pim on ii.inventory_item_id = pim.inventory_item_id  
                    inner join ws_product p on ii.product_id = p.product_id and p.status = 'OK'
                    left join ws_product_container pc on ii.container_id = pc.container_id
//总起来说关联太多表,设计表时可以多一些冗余字段,减少表之间的关联查询;
                where 
                    ii.inventory_type = 'raw_material' and 
                    ii.inventory_status = 'in_stock' and 
                    ii.facility_id = '25' and 
                    datediff(now(),ii.last_updated_time) < 3  //违反了第一个原则
                     and p.product_type = 'goods'
                     and p.product_name like '%果%'   // 违反原则3

                group by 
                    ii.product_id
                having 
                    qoh < 0.005
                order by 
                    qoh desc

 上面的sql我们在from 中使用了子查询,这样对查询是非常不利的;

更好的一种做法是下面的语句:

select  
                t.facility_id,
                f.facility_name,
                t.inventory_status,
                wis.inventory_status_name,
                t.inventory_type,
                t.product_type,
                t.product_id, 
                p.product_name,
                t.container_id, 
                t.unit_quantity, 
                p.unit_code,
                p.unit_code_name,
                pc.container_unit_code_name,
                t.secret_key,
                sum(t.quantity) quantity,
                sum(t.real_quantity) real_quantity,
                sum(t.locked_quantity) locked_quantity,
                sum(t.locked_real_quantity) locked_real_quantity
            from ( select 
                        ii.facility_id,
                        ii.inventory_status,
                        ii.inventory_type,
                        ii.product_type,
                        ii.product_id, 
                        ii.container_id, 
                        ii.unit_quantity, 
                        ita.secret_key,
                        ii.quantity quantity,
                        ii.real_quantity real_quantity,
                        sum(ita.quantity) locked_quantity,
                        sum(ita.real_quantity) locked_real_quantity
                    from 
                        ws_inventory_item ii 
                        inner join ws_inventory_transaction_action ita on ii.inventory_item_id = ita.inventory_item_id
                    where 
                        ii.facility_id = '{$facility_id}' and 
                        ii.inventory_status = '{$inventory_status}' and 
                        ii.product_type = '{$product_type}' and 
                        ii.inventory_type = '{$inventory_type}' and
                        ii.locked_real_quantity > 0 and 
                        ita.type in ('locked', 'release') 
                    group by 
                        ii.product_id, ita.secret_key, ii.container_id, ita.inventory_item_id
                    having 
                        locked_real_quantity > 0 
            ) as t
                inner join ws_product p on t.product_id = p.product_id 
                left join ws_facility f on t.facility_id = f.facility_id
                left join ws_inventory_status wis on wis.inventory_status = t.inventory_status
                left join ws_product_container pc on pc.container_id = t.container_id            
            group by 
                t.product_id, t.secret_key, t.container_id

 注意:

1、from 语句中一定不要使用子查询;

2、使用更多的where加以限制,缩小查找范围;

3、合理利用索引;

4、通过explain查看sql性能;

使用工具 SQL Tuning Expert for Oracle 优化SQL语句

对于SQL开发人员和DBA来说,根据业务需求写出一条正确的SQL很容易。但是SQL的执行性能怎么样呢?能优化一下跑得更快吗?如果不是资深 
DBA,估计很多人都没有信心。

幸运的是,自动化优化工具可以帮助我们解决这个难题。这就是今天要介绍的 Tosska SQL Tuning Expert for Oracle 工具。

下载 https://tosska.com/tosska-sql-tuning-expert-tse-oracle-free-download/

恒浪威购商城
恒浪威购商城

基于asp.net2.0框架技术与企业级分布式框架以及与 ms sql server 2000数据库无缝集合而成,并且融合当前流行的ajax技术进行编写的电子商务系统,她整合了多用户商城、单用户商城功能和恒浪网站整合管理系统,吸收绝大部分同类产品的精华和优点,独创网络团购(b2t)电子商务模式,流程化的团购功能和视频导购等功能,是一款极具商业价值的电子商务系统。商城前台功能概述:商城会员可前台自行

下载

本工具发明人Richard To, Dell的前首席工程师, 拥有超过20年的SQL优化经验.

1.png

1、创建数据库连接,也可以稍后创建。填好连接信息,点击 “Connect” 按钮。

如果您已经安装Oracle客户端,并且在Oracle客户端配置了TNS,可以在本窗口选择“TNS”作为”Connection Mode”,然后在”Database Alias”中选择配置好的TNS作为数据库别名。 

2.png

如果您没有安装Oracle客户端或者不想安装Oracle客户端, 可以选择“Basic Type”作为”Connection Mode”,只需数据库服务器IP, 端口和服务名即可。 

1.png

2、输入有性能问题的SQL 

1.png

3、点击Tune按钮,自动生成大量的等价SQL并且开始执行。虽然测试还没有完成,我们已经可以看到 SQL 20 的性能提升了100%。 

1.png

让我们仔细看一下SQL 20, 它使用了两个Hints, 以最快的执行速度脱颖而出。原来的SQL要0.99秒,优化后的SQL执行时间接近0秒。

 由于这条SQL每天要在数据库中执行上万次,优化后可节省大约 165秒的数据库执行时间。1.png

最后,用等价的SQL 20 替换 应用程序源代码中有性能问题的SQL。重新编译应用程序,性能得到了提高。

调优任务顺利完成!

相关文章:

Sql效能优化总结与sql语句优化篇

SQL语句优化原则,sql语句优化

相关视频:

MySQL优化视频教程—布尔教育

相关专题

更多
数据分析工具有哪些
数据分析工具有哪些

数据分析工具有Excel、SQL、Python、R、Tableau、Power BI、SAS、SPSS和MATLAB等。详细介绍:1、Excel,具有强大的计算和数据处理功能;2、SQL,可以进行数据查询、过滤、排序、聚合等操作;3、Python,拥有丰富的数据分析库;4、R,拥有丰富的统计分析库和图形库;5、Tableau,提供了直观易用的用户界面等等。

674

2023.10.12

SQL中distinct的用法
SQL中distinct的用法

SQL中distinct的语法是“SELECT DISTINCT column1, column2,...,FROM table_name;”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

319

2023.10.27

SQL中months_between使用方法
SQL中months_between使用方法

在SQL中,MONTHS_BETWEEN 是一个常见的函数,用于计算两个日期之间的月份差。想了解更多SQL的相关内容,可以阅读本专题下面的文章。

345

2024.02.23

SQL出现5120错误解决方法
SQL出现5120错误解决方法

SQL Server错误5120是由于没有足够的权限来访问或操作指定的数据库或文件引起的。想了解更多sql错误的相关内容,可以阅读本专题下面的文章。

1084

2024.03.06

sql procedure语法错误解决方法
sql procedure语法错误解决方法

sql procedure语法错误解决办法:1、仔细检查错误消息;2、检查语法规则;3、检查括号和引号;4、检查变量和参数;5、检查关键字和函数;6、逐步调试;7、参考文档和示例。想了解更多语法错误的相关内容,可以阅读本专题下面的文章。

355

2024.03.06

oracle数据库运行sql方法
oracle数据库运行sql方法

运行sql步骤包括:打开sql plus工具并连接到数据库。在提示符下输入sql语句。按enter键运行该语句。查看结果,错误消息或退出sql plus。想了解更多oracle数据库的相关内容,可以阅读本专题下面的文章。

671

2024.04.07

sql中where的含义
sql中where的含义

sql中where子句用于从表中过滤数据,它基于指定条件选择特定的行。想了解更多where的相关内容,可以阅读本专题下面的文章。

564

2024.04.29

sql中删除表的语句是什么
sql中删除表的语句是什么

sql中用于删除表的语句是drop table。语法为drop table table_name;该语句将永久删除指定表的表和数据。想了解更多sql的相关内容,可以阅读本专题下面的文章。

408

2024.04.29

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

Vue3.x 工具篇--十天技能课堂
Vue3.x 工具篇--十天技能课堂

共26课时 | 1.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号