设为首页 收藏本站
查看: 711|回复: 0

[经验分享] Database SQL开发指导

[复制链接]

尚未签到

发表于 2016-11-11 08:59:34 | 显示全部楼层 |阅读模式
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-298683-1-1.html 上篇帖子: 把ACCESS转成SQL数据库 下篇帖子: SQL AUTO INCREMENT 字段
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表