SQL语句效率问题的几点总结
转贴处:http://blog.csdn.net/yaowenbin528/archive/2009/10/26/4728713.aspx 1. SQL优化的原则是: 将一次操作需要读取的BLOCK数减到最低,即在最短的时间达到最大的数据吞吐量。 调整不良SQL通常可以从以下几点切入: 检查不良的SQL,考虑其写法是否还有可
转贴处:http://blog.csdn.net/yaowenbin528/archive/2009/10/26/4728713.aspx
1. SQL优化的原则是:
将一次操作需要读取的BLOCK数减到最低,即在最短的时间达到最大的数据吞吐量。
调整不良SQL通常可以从以下几点切入:
检查不良的SQL,考虑其写法是否还有可优化内容
检查子查询 考虑SQL子查询是否可以用简单连接的方式进行重新书写
检查优化索引的使用
考虑数据库的优化器
2. 避免出现SELECT * FROM table 语句,要明确查出的字段。
3. 在一个SQL语句中,如果一个where条件过滤的数据库记录越多, 这样查询时只进行索引扫描,不读取数据块。
4. 查询时尽可能使用索引覆盖。即对SELECT的字段建立复合索引,
这样查询时只进行索引扫描,不读取数据块。
5. 在判断有无符合条件的记录时建议不要用SELECT COUNT (*)和select top 1 语句。
6. 使用内层限定原则,在拼写SQL语句时,将查询条件分解、分类,并尽量在SQL语句的最里层进行限定,以减少数据的处理量。
7. 应绝对避免在order by子句中使用表达式。
8. 如果需要从关联表读数据,关联的表一般不要超过7个。
9. 小心使用 IN 和 OR,需要注意In集合中的数据量。建议集合中的数据不超过200个。
10. 用 代替,>用>=代替,
11. 在查询时尽量减少对多余数据的读取包括多余的列与多余的行。
12. 对于复合索引要注意,例如在建立复合索引时列的顺序是F1,F2,F3,
则在where或order by子句中这些字段出现的顺序要与建立索引时的字段顺序一致,
且必须包含第 一列。只能是F1或F1,F2或F1,F2,F3。否则不会用到该索引。
13. 多表关联查询时,写法必须遵循以下原则,这样做有利于建立索引,提高查询效率。
格式如下
select sum(table1.je) from table1 table1, table2 table2,
table3 table3 where (table1的等值条件(=)) and (table1的非等值条件)
and (table2与table1的关联条件) and (table2的等值条件) and (table2的非等值条件)
and (table3与table2的关联条件) and (table3的等值条件) and (table3的非等值件)。
注:关于多表查询时from 后面表的出现顺序对效率的影响还有待研究。
14. 子查询问题。对于能用连接方式或者视图方式实现的功能,不要用子查询。
例如:select name from customer where
customer_id in ( select customer_id from order where money>1000)。
应该用如下语句代替:select name from customer
inner join order on customer.customer_id=order.customer_id
where order.money>100。
15. 在WHERE 子句中,避免对列的四则运算,
特别是where 条件的左边,严禁使用运算与函数对列进行处理。
比如有些地方 substring 可以用like代替。
16. 如果在语句中有not in(in)操作,
应考虑用not exists(exists)来重写,最好的办法是使用外连接实现。
17. 对一个业务过程的处理,应该使事物的开始与结束之间的时间间隔越短越好,
原则上做到数据库的读操作在前面完成,数据库写操作在后面完成,避免交叉。
18. 请小心不要对过多的列使用列函数和order by,group by等,谨慎使用disti软件开发t。
19. 用union all 代替 union,数据库执行union操作,
首先先分别执行union两端的查询,
将其放在临时表中,然后在对其进行排序,过滤重复的记录。
当已知的业务逻辑决定query A和query B中不会有重复记录时,
应该用union all代替union,以提高查询效率。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
