Fewer codes but with many database queries or many codes but with fewer database queries? [closed]
There are two options I want to take one of them:
- Writing fewer codes but with many database queries.
- Writing many codes but with fewer database queries.
Dependence on the database in everything, or if you found ways but not the best to make you avoid using the database. Which one do you prefer?
Use as few queries as possible, to execute queries your script needs to contact the SQL server every time which is much slower than the execution of code.
Always go for the less database queries - it reduces load, and it's faster. Of course, there are exceptions to this rule (I can't think of any, though). In general, try to use as few database calls as possible by utilising JOIN
s, etcetera.
Few simple queries is better than 1 difficult query. Sometimes few simple queries can be united to 1 simple query - it's normal. But very often I see strange queries with 5-10 JOINS and sub-selects, and only reason - BECAUSE I CAN DO THIS IN ONE QUERY!!1!
.
Also, results of simple queries often can be cached, so all you need is fetch rest of simple queries. Not all results can be cached, so difficult queries looses here.
Also, I don't like JOIN
s (because JOIN
= SLOW QUERY
very often).
精彩评论