`
alex_gao
  • 浏览: 81026 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

优化SQL查询的15个方式(转载)

 
阅读更多

15 Ways to Optimize Your SQL Queries



Previous article was on 10 Ways To Destroy A SQL Database that sort of teaches you what mistakes many company might make on their database that will eventually lead to a database destroy. In this article,  you will get to know 15 ways to optimize your SQL queries. Many ways are common to optimize a query while others are less obvious.

Indexes

Index your column is a common way to optimize your search result. Nonetheless, one must fully understand how does indexing work in each database in order to fully utilize indexes. On the other hand, useless and simply indexing without understanding how it work might just do the opposite.

Symbol Operator

Symbol operator such as >,<,=,!=, etc. are very helpful in our query. We can optimize some of our query with symbol operator provided the column is indexed. For example,

1 SELECT * FROM TABLE WHERE COLUMN > 16

Now, the above query is not optimized due to the fact that the DBMS will have to look for the value 16 THEN scan forward to value 16 and below. On the other hand, a optimized value will be

1 SELECT * FROM TABLE WHERE COLUMN >= 15

This way the DBMS might jump straight away to value 15 instead. It’s pretty much the same way how we find a value 15 (we scan through and target ONLY 15) compare to a value smaller than 16 (we have to determine whether the value is smaller than 16; additional operation).

Wildcard

In SQL, wildcard is provided for us with ‘%’ symbol. Using wildcard will definitely slow down your query especially for table that are really huge. We can optimize our query with wildcard by doing a postfix wildcard instead of pre or full wildcard.

1 #Full wildcard
2 SELECT * FROM TABLE WHERE COLUMN LIKE '%hello%';
3 #Postfix wildcard
4 SELECT * FROM TABLE WHERE COLUMN LIKE  'hello%';
5 #Prefix wildcard
6 SELECT * FROM TABLE WHERE COLUMN LIKE  '%hello';

That column must be indexed for such optimize to be applied.

P.S: Doing a full wildcard in a few million records table is equivalence to killing the database.

NOT Operator

Try to avoid NOT operator in SQL. It is much faster to search for an exact match (positive operator) such as using the LIKE, IN, EXIST or = symbol operator instead of a negative operator such as NOT LIKE, NOT IN, NOT EXIST or != symbol. Using a negative operator will cause the search to find every single row to identify that they are ALL not belong or exist within the table. On the other hand, using a positive operator just stop immediately once the result has been found. Imagine you have 1 million record in a table. That’s bad.

COUNT VS EXIST

Some of us might use COUNT operator to determine whether a particular data exist

1 SELECT COLUMN FROM TABLE WHERE COUNT(COLUMN) > 0

Similarly, this is very bad query since count will search for all record exist on the table to determine the numeric value of field ‘COLUMN’. The better alternative will be to use the EXIST operator where it will stop once it found the first record. Hence, it exist.

Wildcard VS Substr

Most developer practiced Indexing. Hence, if a particular COLUMN has been indexed, it is best to use wildcard instead of substr.

1 #BAD
2 SELECT * FROM TABLE WHERE  substr ( COLUMN, 1, 1 ) ='value'.

The above will substr every single row in order to seek for the single character ‘value’. On the other hand,

1 #BETTER
2 SELECT * FROM TABLE WHERE  COLUMN = 'value%'.

Wildcard query will run faster if the above query is searching for all rows that contain ‘value’ as the first character. Example,

1 #SEARCH FOR ALL ROWS WITH THE FIRST CHARACTER AS 'E'
2 SELECT * FROM TABLE WHERE  COLUMN = 'E%'.

Index Unique Column

Some database such as MySQL search better with column that are unique and indexed. Hence, it is best to remember to index those columns that are unique. And if the column is truly unique, declare them as one. However, if that particular column was never used for searching purposes, it gives no reason to index that particular column although it is given unique.

Max and Min Operators

Max and Min operators look for the maximum or minimum value in a column. We can further optimize this by placing a indexing on that particular columnMisleading We can use Max or Min on columns that already established such Indexes. But if that particular column is frequently use, having an index should help speed up such searching and at the same time speed max and min operators. This makes searching for maximum or minimum value faster. Deliberate having an index just to speed up Max and Min is always not advisable. Its like sacrifice the whole forest for a merely a tree.

Data Types

Use the most efficient (smallest) data types possible. It is unnecessary and sometimes dangerous to provide a huge data type when a smaller one will be more than sufficient to optimize your structure. Example, using the smaller integer types if possible to get smaller tables. MEDIUMINT is often a better choice than INT because a MEDIUMINT column uses 25% less space. On the other hand, VARCHAR will be better than longtext to store an email or small details.

Primary Index

The primary column that is used for indexing should be made as short as possible. This makes identification of each row easy and efficient by the DBMS.

String indexing

It is unnecessary to index the whole string when a prefix or postfix of the string can be indexed instead. Especially if the prefix or postfix of the string provides a unique identifier for the string, it is advisable to perform such indexing. Shorter indexes are faster, not only because they require less disk space, but because they also give you more hits in the index cache, and thus fewer disk seeks.

Limit The Result

Another common way of optimizing your query is to minimize the number of row return. If a table have a few billion records and a search query without limitation will just break the database with a simple SQL query such as this.

1 SELECT * FROM TABLE

Hence, don’t be lazy and try to limit the result turn which is both efficient and can help minimize the damage of an SQL injection attack.

1 SELECT * FROM TABLE WHERE 1 LIMIT 10

Use Default Value

If you are using MySQL, take advantage of the fact that columns have default values. Insert values explicitly only when the value to be inserted differs from the default. This reduces the parsing that MySQL must do and improves the insert speed.

In Subquery

Some of us will use a subquery within the IN operator such as this.

1 SELECT * FROM TABLE WHERE COLUMN IN (SELECT COLUMN FROM TABLE)

Doing this is very expensive because SQL query will evaluate the outer query first before proceed with the inner query. Instead we can use this instead.

1 SELECT * FROM TABLE, (SELECT COLUMN FROM TABLE) asdummytable WHERE dummytable.COLUMN = TABLE.COLUMN;

Using dummy table is better than using an IN operator to do a subquery. Alternative, an exist operator is also better.

Utilize Union instead of OR

Indexes lose their speed advantage when using them in OR-situations in MySQL at least. Hence, this will not be useful although indexes is being applied

1 SELECT * FROM TABLE WHERE COLUMN_A = 'value' OR COLUMN_B = 'value'

On the other hand, using Union such as this will utilize Indexes.

1 SELECT * FROM TABLE WHERE COLUMN_A = 'value'
2 UNION
3 SELECT * FROM TABLE WHERE COLUMN_B = 'value'

Hence, run faster.

Summary

Definitely, these optimization tips doesn’t guarantee that your queries won’t become your system bottleneck. It will require much more benchmarking and profiling to further optimize your SQL queries. However, the above simple optimization can be utilize by anyone that might just help save some colleague rich bowl while you learn to write good queries. (its either you or your team leader/manager)

http://hungred.com/useful-information/ways-optimize-sql-queries/

分享到:
评论

相关推荐

    sqlserver数据库优化总结的资料

    在提供的压缩文件中,"SQLSERVER 2005管理与开发 优化SQL Server数据库(转载).mht"可能是关于SQL Server 2005的管理与优化的综合文章,包含了很多实践经验和技巧;"SQL优化.xlsx"可能是对SQL查询优化的实例或数据...

    SQL笔试题(转载的)

    13. **性能优化**:包括查询优化、索引优化、存储过程优化等,以提升数据库的响应速度和整体性能。 这些基本概念和操作构成了SQL Server的基础知识框架。通过《2011 SQL笔试题》这样的练习材料,学习者可以测试并...

    sqlserver学习资料(转载!)

    - 视图:虚拟表,基于一个或多个表的查询结果,提供安全性和简化复杂查询。 - 存储过程:预编译的T-SQL代码块,可多次执行,提升性能并封装业务逻辑。 6. 触发器: - 用于在INSERT、UPDATE或DELETE操作后自动...

    SQL大总结——转载经典——价值过亿

    14. **性能优化**:SQL性能优化涉及索引策略、查询优化、内存管理和硬件配置等多个方面,是数据库管理员的重要工作。 《SQL大总结》文档很可能是对这些知识点的详细讲解和实例演示,读者可以通过学习来提升自己的...

    【转载】浅谈基于索引的SQL语句优化方法

    本文主要探讨的是基于索引的SQL语句优化方法,旨在帮助读者深入理解如何利用索引来提高查询效率。索引是数据库管理系统中的一个重要概念,它能显著加速数据检索过程,尤其是在处理大量数据时。 一、索引的基本原理 ...

    本人提供SQL语句大全(转载) 12009年04月28日 星期二 19:35SQL语句大全(转载)

    从给定的文件信息中,我们可以提炼出一系列与SQL语句相关的知识点,这些知识点涵盖了SQL的基本操作、查询优化以及在不同数据库系统中的表现差异。以下是对这些知识点的详细阐述: ### 1. SQL语句大全 文件标题和...

    摘录转载sql语句大全

    SQL是Structured Query ...这些技巧是优化SQL查询性能的关键,理解并应用它们能够显著提升数据库操作的效率,减少不必要的资源消耗。对于数据库管理员和开发者来说,掌握这些知识对于构建高性能的数据库应用至关重要。

    Excel表导入sqlserver(转载的,已试过成功)

    本篇文章基于一个成功的案例进行讲解,主要介绍如何通过编程的方式实现Excel数据的批量导入。该方法不仅能够提高工作效率,还能够减少因人工操作带来的错误。 #### 二、技术栈 - **开发环境**:本例使用的是.NET ...

    秒杀应用的MySQL数据库优化 (转载)

    秒杀应用的MySQL数据库优化是一个重要的议题,尤其是在高并发、数据处理速度要求极高的场景下。这类应用常常面临巨大的压力,如瞬间涌入的大量请求、数据读写速度、以及资源的有效利用。本篇文章将深入探讨如何针对...

    图解access to sql2000 转换,acc 2 sql 成功分享!新势力网络原创,转载请注明出处,谢谢!

    这个向导是SQL Server提供的工具,用于导入和导出数据,对于Access到SQL Server的转换非常有用。 在向导的下一步中,选择数据源,即Access数据库。在这个过程中,要确保选择正确的驱动程序,即"Microsoft Access(....

    海量数据装载工具SQL Loader技术分析.pdf

    整个过程包括以下几个步骤:SqlLdr命令启动、读取控制文件、读取数据文件、根据规则转载数据,最后记录日志文件。 对于SQL Loader的性能提升技术措施,文章提出了相关的技术细节。这些措施可能包括对控制文件语法的...

    工资管理系统

    这份文档可能会涵盖SQL查询优化、性能监控、错误调试等内容,对于深入理解和应用SQL在工资管理系统中的角色大有裨益。 总的来说,一个完善的工资管理系统依赖于高效、安全的SQL数据库设计和管理。通过熟练掌握SQL,...

    【转载】oracle笔试4.doc

    Oracle数据库是世界上最流行的数据库管理系统之一,它在企业级应用中广泛应用。以下是对题目中提到的知识点的详细解释: ...在处理复杂的查询时,了解如何正确使用这些工具可以帮助优化性能,提高数据管理的效率。

    《转载》oracle1000问

    - SQL优化:使用EXPLAIN PLAN分析执行计划,通过索引、连接优化、子查询优化提升性能。 - 会话管理:通过设置合适的初始化参数调整会话资源,如PGA、SGA等。 - 分区(Partitioning):大表可以按特定规则分区,...

    最好的asp CMS系统科讯CMSV7.0全功能SQL商业版,KesionCMS V7.0最新商业全能版-免费下载

    15、提供API整合接口,可整合主流论坛、博客等软件,轻松实现多个程序紧密结合。 可同时整合动网论坛、oblog等第三方软件,轻松实现会员一站通,帮助用户轻松实现“cms + 论坛 + 博客”的多功能门户方案。 16...

    【RPA之家转载视频教程7】在UiPath中排序数据表-如何在UiPath中对数据表进行排序-RPA技术中心.rar

    2. 使用SQL查询:如果熟悉SQL语言,可以通过ExecuteSQLQuery活动执行排序查询。这需要将DataTable转换为SQL兼容的格式,然后使用SQL的ORDER BY子句进行排序。 3. 使用 LINQ:UiPath支持使用LINQ(Language ...

    db2学习代码例子(代码为转载网上)

    1. `DECLARE CURSOR statement .doc`:这部分内容可能详细介绍了DB2中的声明游标(DECLARE CURSOR)语句,它用于定义一个游标,允许程序按需逐行处理查询结果。在DB2中,游标是处理大量数据的有效工具,特别是在需要...

    hibernate官方入门教程 (转载)

    7. **Criteria查询**: 除了传统的SQL语句,Hibernate还提供了Criteria API进行动态查询,无需编写SQL,更加面向对象。 8. **HQL(Hibernate Query Language)**: Hibernate特有的查询语言,类似于SQL,但针对对象...

Global site tag (gtag.js) - Google Analytics