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

Database SQL开发指导

    博客分类:
  • DB
 
阅读更多

1. Always define field names. Do not use SELECT * or INSERT INTO table VALUES. However, if it's important enough to save in a text file (ie, it's seed data or a migration script) then it gets explicit field names.

2. Always use the database server's timestamp. Web servers may have disparate times. Reports may come from different servers than the inserted data.

3. When doing reports, the network traffic is usually the biggest bottleneck. If you're going to receive information, it's better to receive in chunks, which will likely be larger than a logical piece. For instance, state reporting -- instead of making 50 connections for states in the US, get them all at once. If the dataset is very large and folks do not want to stare at a blank page while the report is loading, use paging with LIMIT to grab, say, 1000 entries at a time and display them on the screen so people can start looking at the data while the rest is being grabbed.

4. Running a query in a loop is usually a bad idea. If you are executing the same query with different data, consider building a query string using UNION and executing it at the end of the loop, so you can execute multiple queries with only one trip across the network to the database.

5. Do not be afraid of JOINs. They are not necessarily resource intensive, given good indexing. Most of the time a denormalized schema without a join ends up being worse than a normalized one using a join. When there is redundant data, ensuring data integrity takes up more cycles than providing a framework for data integrity in the first place.

6. Limit the use of correlated subqueries; often they can be replaced with a JOIN.

7. Ensure repeated sql statements are written absolutely identical to facilitate efficient reuse: reparsing can often be avoided for each subsequent use.

8. Code the SQL as per exact requirement i.e. no unnecessary columns should be selected, and no unnecessary GROUP BY or ORDER BY clauses used.

DevStd_Database_SQL_V1.0 6 / 7

 

9. Avoid using ‘select *’, when you could select by actual column name(s). It is the same or faster to SELECT by actual column name(s). The larger the table the more likely the savings. E.g. SELECT customer_id, last_name, first_name, street, city FROM customer; rather than: SELECT * FROM customer;

10. Do not perform operations on DB objects referenced in the WHERE clause. For e.g. use “SELECT client, date, amount FROM sales WHERE amount > 0” rather than “SELECT client, date, amount FROM sales WHERE amount!= 0”.

11. Use HAVING only when summary operations applied to columns will be restricted by the clause. Avoid a HAVING clause in SELECT statements when it is only required to filter selected rows after all the rows have been returned. A WHERE clause may be more efficient. For e.g. use “SELECT city FROM country WHERE city!= 'Vancouver' AND city!= 'Toronto' GROUP BY city” rather than “SELECT city FROM country GROUP BY city HAVING city!= 'Vancouver' AND city!= 'Toronto'”

12. Where possible use EXISTS rather than DISTINCT.

13. Where possible use a non-column expression (putting the column on one side of the operator and all the other values on the other). Non-column expressions are often processed earlier thereby speeding the query. For e.g. use WHERE SALES < 1000/(1 + n); rather than “WHERE SALES + (n * SALES) < 1000”. Never do a calculation on an indexed column (For e.g., WHERE salary*5 > :myvalue).

14. Whenever possible, use the UNION statement instead of OR conditions.

15. Avoid the use of NOT IN or HAVING in the WHERE clause. Instead, use the NOT EXISTS clause.

16. Use the Oracle “decode” function to minimize the number of times a table has to be selected.

17. Check whether your query could do without the DISTINCT clause you have in the code.

18. Check whether UNION be replaced with a UNION ALL which improves the response time.

DevStd_Database_SQL_V1.0 7 / 7

 

19. Avoid using negative logic (NOT, <>, !=).

20. When using OR’s in the where clause be sure to use enough parenthesis to ensure defaults for operator precedence are not used.

21. When we use ‘EXISTS’, we do not need to use any column name in the subsequent SQL and just return a constant value for better efficiency. For e.g. SELECT s.sname FROM student s

WHERE EXISTS (SELECT 1 FROM grade_report gr, section WHERE section.section_id = gr.section_id).

22. Ensure all Foreign Key columns are indexed

23. Use Bind Variables instead of static values

分享到:
评论

相关推荐

    Oracle database 11g SQL开发指南 part4

    清华大学出版社出版的《Oracle database 11g SQL开发指南》,本书是由著名的Oracle大师Joson Price著。 学习通过编写SQL语句并构建PL/SQL程序来访问Oracle数据库。完全涵盖了最新版本Oracle数据库的功能和技术,指导...

    Oracle Database 10g SQL开发指南 中文目录

    《Oracle Database 10g SQL开发指南》是一本专注于Oracle 10g数据库系统中SQL语言使用的指导书籍。本指南涵盖了SQL的基础知识以及在Oracle环境中的一些高级特性。 首先,书中介绍的是关系数据库和SQL的基本概念。...

    Oracle database 11g SQL 开发指南 part2

    清华大学出版社出版的《Oracle database 11g SQL开发指南》,本书是由著名的Oracle大师Joson Price著。 学习通过编写SQL语句并构建PL/SQL程序来访问Oracle数据库。完全涵盖了最新版本Oracle数据库的功能和技术,指导...

    Oracle database 11g SQL开发指南 part1

    清华大学出版社出版的《Oracle database 11g SQL开发指南》,本书是由著名的Oracle大师Joson Price著。 学习通过编写SQL语句并构建PL/SQL程序来访问Oracle数据库。完全涵盖了最新版本Oracle数据库的功能和技术,指导...

    《oracle database 10g sql 开发指南》源码

    这些知识点是Oracle Database 10g SQL开发的核心内容,通过阅读本书源码,读者可以深入理解并掌握这些概念,提升数据库开发和管理技能。实践操作是学习的关键,因此,书中提供的源码对于学习者来说是极其宝贵的资源...

    Oracle Database SQL Fundamentals II.pdf

    综上所述,Oracle Database SQL Fundamentals II是一本为Oracle数据库开发者和管理员准备的深入学习教材,覆盖了SQL语言在Oracle数据库中应用的各个方面,并且包含了实践操作的指导和高级概念的介绍。

    Oracle database 11g SQL 开发指南 part3

    清华大学出版社出版的《Oracle database 11g SQL开发指南》,本书是由著名的Oracle大师Joson Price著。 学习通过编写SQL语句并构建PL/SQL程序来访问Oracle数据库。完全涵盖了最新版本Oracle数据库的功能和技术,指导...

    Oracle Database 10g 开发指南.zip

    《Oracle Database 10g 开发指南》是一本深度探讨Oracle数据库系统开发的书籍,尤其适合于对数据库技术有热情的初学者以及寻求提升的高级用户。Oracle数据库系统是全球广泛应用的关系型数据库管理系统,尤其在企业级...

    SQL Anywhere Database Guid

    1. **安装与配置**:指导用户如何在不同平台上安装和配置 SQL Anywhere 数据库系统,包括桌面环境、移动设备和服务器环境。 2. **数据库管理**:详细介绍如何创建、维护和管理数据库,包括备份、恢复、安全性设置等...

    ANSI/ISO/IEC International Standard(IS) Database Language SQL Part 4 Persistent Stored Modules (SQL Psm)

    综上所述,ISO/IEC 9075-4:1999(E)标准提供了关于如何在SQL数据库环境中使用持久存储模块的重要指导。这些模块不仅增强了SQL语言的功能,还提供了更高的灵活性和性能,对于构建复杂的数据管理系统至关重要。通过遵循...

    SQL开发指南

    ### SQL开发指南:深入...综上所述,《SQL开发指南》不仅全面覆盖了SQL的基础知识和开发技巧,还深入探讨了数据库管理、数据操纵、数据集成以及高级主题,为读者提供了丰富的资源和指导,帮助他们在SQL领域取得成功。

    WINCE_SQL_SERVER.rar_ce DATABASE_sql wince_winCE SQL_wince sql

    标题 "WINCE_SQL_SERVER.rar_ce DATABASE_sql wince_winCE SQL_wince sql" 概括了一套关于在Windows CE (Windows Embedded Compact Edition) 系统环境下,如何使用 Microsoft SQL Server Compact Edition (SQL ...

    Database_sql_

    本压缩包"Database_sql_"可能包含了关于使用SQL进行数据库开发的相关资料,如教程、示例代码或数据库设计文档。 1. SQL基础: SQL的基础包括数据定义(DDL,Data Definition Language),如CREATE TABLE用于创建表...

    SQL Server 2008实验指导书

    总的来说,这份《SQL Server 2008 实验指导书》为读者提供了实践性的学习路径,通过逐步操作,使读者能够熟练掌握SQL Server 2008的基本管理和操作技能,为深入学习数据库管理和开发奠定了坚实的基础。在每个实验...

    SQLDeveloper工具/MySQL/SQLServer驱动程序

    MySQL驱动通常对应JDBC(Java Database Connectivity)驱动,以jar文件形式存在,而SQL Server则可能需要ODBC(Open Database Connectivity)驱动或者JDBC驱动。 总结起来,SQLDeveloper工具结合MySQL和SQL Server...

    使用Access作为SQL Server数据库的前端开发工具.pdf

    使用Access作为SQL Server数据库的前端开发工具 在本文中,我们将介绍如何使用Microsoft Access作为SQL Server数据库的前端开发工具。通过使用ODBC连接,Access可以与SQL Server数据库建立连接,从而实现对SQL ...

    SQL Server 2000 Web应用开发指南

    《SQL Server 2000 Web应用开发指南》是一本专为开发者设计的教程,旨在帮助读者掌握如何利用SQL Server 2000构建...无论你是初学者还是有经验的开发者,这本书都能提供宝贵的指导,助你在Web开发的道路上更进一步。

    DATABASE ADMINISTRATION SQL SERVER STANDARDS

    ### 数据库管理与SQL Server标准 #### SQL Server命名规范与标准 **1.0 数据库、文件及文件路径** - **数据库命名规范:** ...这些指导方针不仅有助于提高数据库系统的性能和稳定性,还能促进团队间的协作与沟通。

    Oracle Database 11g SQL Tuning Workshop Activity Guide

    Oracle Database 11g SQL Tuning Workshop Activity Guide 是一个专为 Oracle 数据库管理员、开发人员和技术专业人士设计的培训材料。此指南旨在帮助用户理解并掌握如何优化 Oracle 11g 中的 SQL 查询性能,提高...

    VCPP-SQL_Server-DataBase.rar_VC++ SQL _sql server 图片_vc连接sql ser

    标题中的"VCPP-SQL_Server-DataBase.rar_VC++ SQL _sql server 图片_vc连接sql ser"表明这是一个关于使用VC++通过SQL Server API(如ODBC或ADO)来连接和操作SQL Server数据库的资源包,其中可能包含代码示例和逐步...

Global site tag (gtag.js) - Google Analytics