Upload
inovia
View
30
Download
0
Embed Size (px)
Citation preview
8th of October 2014
Zen SQLBest practices to query and database design
by Clément Prévost
8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:a. Database schemab. Query
8th of October 2014
State of the art
● ORM (Doctrine, ActiveRecord, ...)○ Hides the database schema and queries, follow your
applicative coding best practices
● Query Builder (Zend\Db\Sql, …)○ Hides the queries, not the schema
● Raw (Migration scripts, performance critical task, Analytics)○ DIY
8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:a. Database schemab. Query
8th of October 2014
a) Database schema best practices
A. KISS ○ Beware of overengineering○ Don’t let optimizations get in the way of simplicity
B. Explicitness is the key○ Ban abbreviations, “status” column and add
comments
C. Your business WILL evolve, same with your schema○ Never be afraid to refactor, rename and move stuff
around
8th of October 2014
Case study: Database schema BP
8th of October 2014
1
3
2
1
2
3
3
8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:a. Database schemab. Query
8th of October 2014
b) Query best practices
A. Comments and documentation, Avoid deep nesting, Reuse external libraries
B. Know your enemy: Long vs. complex queries
C. Tools: IDE, Database specificity, SQL capabilities
8th of October 2014
8th of October 2014
8th of October 2014
8th of October 2014
Key things to remember
● Work it step by step○ Have a perfect schema, then work on beautiful
queries
● Know your tools, learn advanced SQL
● You already know how○ Good coding practices also apply to SQL (queries
AND schema)
8th of October 2014
Questions ?For online questions, please leave a comment on the article.
8th of October 2014
Join the community !(in Paris)
Social networks :● Follow us on Twitter : https://twitter.com/steamlearn● Like us on Facebook : https://www.facebook.com/steamlearn
SteamLearn is an Inovia initiative : inovia.fr
You wish to be in the audience ? Contact us at [email protected]
8th of October 2014
Sources1. http://www.slideshare.net/Samuel90/software-development-vs-software-maintenance2. http://code.tutsplus.com/tutorials/top-15-best-practices-for-writing-super-readable-code--net-
81183. http://www.postgresql.org/docs/9.3/static/view-pg-available-extensions.html4. http://postgres-bits.herokuapp.com/5. http://www.postgresql.org/docs/9.3/static/index.html