SQL语句简单优化

总结笔记

1、避免使用‘*’,直接使用列名字

在解析过程中,会将‘*’一次转化为所有的列名,并通过查询字典完成,消耗更多时间。

2、多表查询时,尽量使用表的别名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q2VSZkFk-1650542099215)(https://secure2.wostatic.cn/static/ohQBsP5YJUwjw4XMBSNZtL/image.png)]

可以更加精确的定义到哪张表的哪个字段,也避免相同列名的产生的问题。

3、Where子句中,把能快速缩小查询范围的语句放在最右边

因为where子句中的语句,是按照从右往左的顺序执行。

在这里插入图片描述

先把“=30”的找到,快速缩小了范围,然后再已经“=30”的范围里面找“>10”的范围。

4、使用“≥”代替“>”

在这里插入图片描述

如果使用“>3”,第一个找到的是3,然后依次比较比3大的数据,但第一个找到的3对结果没有任何作用。

当使用“≥4”时,会先找到4,然后再依次比较比4大的数据,此时4已经是正确答案之一

5、用TRUNCATE代替DELETE

DELETE操作比较安全,因为它删除之前,会先把删除内容放到回滚段中,如果误删可以恢复。

TRUNCATE是把数据直接删除掉,所以当确定数据一定会删除时,可以直接使用TRUNCATE,执行速度更快。

6、多实用COMMIT

当使用COMMIT后,会释放一些资源。

在这里插入图片描述

7、避免在索引列上使用函数

当sal使用了*2后,sal上的索引不会生效,还是会全表查询。

🔗参考链接

https://classroom.devcloud.huaweicloud.com/MOOCCourseChapters/learning/d9eab20e10e44c00bd01c904c876b78b/eb508df9d1f445c0af93f57ea4b5fe57

SQL代码美化程序 SQL Pretty Printer 3.2.8 Copyright 2005-2011, Gudu Software. All Rights Reserved http://www.dpriver.com -------------------------------------------------------- Overview -------- SQL Pretty Printer is a tool that will help you beautify your SQL code. Using hotkey functionality, SQL Pretty Printer can reformat SQL statements for a wide variety of database tools such as Microsoft Query Analyzer, SQL Server Management Studio (SSMS), TOAD and PL/SQL Developer, development environments such as Visual Studio 2003/2005/2008 and Eclipse, and popular editors such as UltraEditor and EditPlus. In addition to beautifying SQL code, SQL Pretty Printer can translate SQL code into C#, Java, PHP, DELPHI and other program languages. SQL Pretty Printer also includes command line functionality, with the ability to format single files, single directories and multiple directories. SQL Pretty Printer is designed to deal with the syntax used by most popular database systems including Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). Output conforms to most of the entry level SQL99 Standard. Add-Ins for SSMS and Visual Studio 2003/2005/2008/2011 are available. APIs for dotnet and COM version are available. features: ** Beautifies SQL statements utilizing highly customizable format options. ** Formats SQL on-the-fly in popular tools and editors using hotkey functionality. ** Minimizes to the system tray for quick access. ** Includes a command line for batch conversion of single files, single directories or directory trees (use the command line API in your own program!) ** Verifies SQL syntax with detailed error information. ** Converts monochrome SQL code into colorful RTF document. ** Converts monochrome SQL code into colorful HTML for easy placement in blogs and forums. ** Converts SQL to various programming languages including C#, Java, DELPHI, PHP and others. ** Currently supports SQL syntax for Microsoft SQL Server, Oracle, IBM DB2, MySQL and Microsoft Access (Informix, Sybase, and PostgreSQL support is currently in development). ** Add-In for SQL Server Management Studio available. ** Add-In for Visual Studio 2003/2005/2008 available. Requirements ------------ Pentium class CPU or higher Windows 95/98/NT/2000/XP/Vista/win7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值