- 🇳🇿New Zealand quietone
The documentation referred to here is for Drupal 7. Is there anything to do here for currently supported versions of Drupal?
- 🇮🇹Italy apaderno Brescia, 🇮🇹
I do not recall any documentation page that gives hints about which type of queries a module should use by default. This issue is still actual.
Reading the comments in this issue, I gather there is no exact consensus on which type of queries should be used by default, though. A documentation page should report what the pros and cons for each query type are, without pushing toward a type or the other one.
- 🇳🇿New Zealand quietone
I took a closer look and this is documented in the note-tip https://www.drupal.org/docs/8/api/database-api/dynamic-queries/introduction-to-dynamic-queries. It has been there since the page was created in 2017. It is also in the Drupal 7 documentation, added in Jan 2013.
If anyone want this is Coding Standards, then an issue should be opened there for discussion.
I think this is fixed.
- 🇮🇹Italy apaderno Brescia, 🇮🇹
I think that what reported in Introduction to Dynamic Queries → (or Introduction to dynamic queries → for Drupal 7) is the minimum which could be said without disagreements.
This issue suggests making the topic clearer, but there are different opinions on what the documentation should say.Neither the Drupal 7 nor the Drupal 8+ documentation guide says that static queries could be used when code does not use extenders, as comment #11 📌 Document usage of static vs dynamic queries Postponed: needs info suggests. That could be a useful addition, if it is not already understood with they should be alterable.
- 🇮🇹Italy apaderno Brescia, 🇮🇹
I changed the tip notes in both the documentation guides.
In 90% of select query use cases you will have a static query. If in a critical performance path, you should use db_query() and friends instead of db_select() for performance reasons. Dynamic queries should be used if the query parts vary (to add
WHERE
conditions depending on the context, for example), if they should be alterable (to check node access permissions, for example), or if a query extender is used (to create a paged query, for example).In 90% of the use cases you will have a static query. When performance is critical,
query()
should be used instead ofselect()
. Dynamic queries should be used when the query parts vary, when they should be alterable, or when an extendable query class is used.