`
maosheng
  • 浏览: 553740 次
  • 性别: 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 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 11g SQL 开发指南 part3

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

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

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

    Database Systems Using Oracle A Simplified Guide to SQL and PLSQL, 2nd edition

    Oracle数据库开发英文原版书,内容广泛,结构清楚,是当下流行的数据库开发指导书。

    基于SQL SERVER环境的数据仓库开发.pdf

    "基于SQL SERVER环境的数据仓库开发" 在数据仓库的开发中,SQL Server是一个非常流行的选择,因为它提供了强大且灵活的数据存储和处理功能。本文将深入探讨基于SQL Server环境的数据仓库开发,涵盖数据仓库的概念、...

    Java项目开发与毕业设计指导

    然后SchoolEbay\database目录下的数据库文件附加到Sql Server,具体操作是打开企业管理器,在左边的树状列表中右击数据库,在弹出的快捷菜单中选择“所有任务”|“附加数据库”命令,再选择要附加的数据库文件即可。...

    SQL_SERVER_2008_DBA入门经典 NO.4

    Michael Cierkowski(Maple Valley,WA),目前是Netdesk公司SQL Server管理方面的指导师。Michael在2000成为Microsoft认证专家和培训师。他目前持有MCSD、MCDBA、MCAD、MCSA、MCPD(Windows、Web、Enterprise)和...

    SQL_SERVER_2008_DBA入门经典 NO.2

    Michael Cierkowski(Maple Valley,WA),目前是Netdesk公司SQL Server管理方面的指导师。Michael在2000成为Microsoft认证专家和培训师。他目前持有MCSD、MCDBA、MCAD、MCSA、MCPD(Windows、Web、Enterprise)和...

    SQL_SERVER_2008_DBA入门经典 NO.3

    Michael Cierkowski(Maple Valley,WA),目前是Netdesk公司SQL Server管理方面的指导师。Michael在2000成为Microsoft认证专家和培训师。他目前持有MCSD、MCDBA、MCAD、MCSA、MCPD(Windows、Web、Enterprise)和...

    SQL Server 2008管理员必备指南(超高清PDF)Part3

    《SQL Server 2008管理员必备指南》的特点是按照日常工作的逻辑来编排内容,并含有大量实例操作指导、技巧提示及T-SQL代码,方便管理员日常的工作。 《SQL Server 2008管理员必备指南》适合所有SQL Server用户参考,...

    SQL Server 2008管理员必备指南(超高清PDF)Part1

    《SQL Server 2008管理员必备指南》的特点是按照日常工作的逻辑来编排内容,并含有大量实例操作指导、技巧提示及T-SQL代码,方便管理员日常的工作。 《SQL Server 2008管理员必备指南》适合所有SQL Server用户参考,...

    SQL Server 2008管理员必备指南(超高清PDF)Part2

    《SQL Server 2008管理员必备指南》的特点是按照日常工作的逻辑来编排内容,并含有大量实例操作指导、技巧提示及T-SQL代码,方便管理员日常的工作。 《SQL Server 2008管理员必备指南》适合所有SQL Server用户参考,...

    ASP连接SQL Server数据库的问题研究.pdf

    ASP 连接 SQL Server 数据库的问题研究 本文研究了 ASP 连接 SQL Server 数据库的问题,讨论了 SQL Server 数据库在 ASP.NET 中的连接问题。 SQL Server 是一 种综合性信息数据库,数据库之间的连接是建设网站的...

    PB与SQL Server数据库的连接.pdf

    1. 在PB开发环境中使用专用接口:在PB的Database画板的Objects子窗口中,或者Database Profiles画板中,右击MSS Microsoft SQL Server项,在弹出菜单中选择New Profiles项,直接设置Profile名,选择数据库服务器及欲...

    PHP网站设计中SQL注入的安全防御.pdf

    PHP 是一种广泛应用于网站开发的编程语言,它可以快速地执行动态网页,并具有较好跨平台性和实用性。但是,PHP 的安全性仍然存在一些漏洞,例如 SQL 注入攻击。 SQL 注入的产生主要是因为程序员在编写代码时没有对...

    Oracle Database 11g初学者指南--详细书签版

     Ian Abramson是IOUG(Independent Oracle Users Group)现任总裁,现居加拿大多伦多,他是业界资深技术顾问,为电信、CRM、公用事业和其他行业的客户提供实现方案的专业指导。他的关注方向包括Oracle产品集,以及...

    VB6.0与SQL Server2000的数据库连接方案比较.pdf

    在数据库开发中,VB6.0和SQL Server2000是一对非常popular的组合。VB6.0提供了强大的编程语言功能,而SQL Server2000提供了强大的数据库管理功能。两者的结合可以提供高效、美观的数据库应用程序。 在实际应用中,...

Global site tag (gtag.js) - Google Analytics