Advancing Engineering with AI through the Next Generation of Strategic Projec...
Steam Learn: Zen SQL
1. 8th of October 2014
Zen SQL
Best practices to query and database design
by Clément Prévost
2. 8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:
a. Database schema
b. Query
3. 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 (ZendDbSql, …)
○ Hides the queries, not the schema
● Raw (Migration scripts, performance critical task,
Analytics)
○ DIY
4. 8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:
a. Database schema
b. Query
5. a) Database schema best practices
8th of October 2014
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
8. 8th of October 2014
The Zen way to SQL
● State of the art
● Readability best practices:
a. Database schema
b. Query
9. 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
13. 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)
14. 8th of October 2014
Questions ?
For online questions, please leave a comment on the article.
15. 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
learn@steamhouse.fr
16. 8th of October 2014
Sources
1. http://www.slideshare.net/Samuel90/software-development-vs-software-maintenance
2. http://code.tutsplus.com/tutorials/top-15-best-practices-for-writing-super-readable-code--net-
8118
3. http://www.postgresql.org/docs/9.3/static/view-pg-available-extensions.html
4. http://postgres-bits.herokuapp.com/
5. http://www.postgresql.org/docs/9.3/static/index.html