Document usage of static vs dynamic queries

Created on 22 June 2010, almost 15 years ago
Updated 26 March 2025, about 1 month ago

There is currently no documentation about when static queries should be used, vs when the dynamic queries are preferred. This would be expected on either http://drupal.org/node/310069 or somewhere under http://drupal.org/coding-standards.

The current coding style in HEAD seems to be that when the query string ($query) is constant (i.e. a single string without variables or concatenations), a static query is used, and in other cases a dynamic query is used.

📌 Task
Status

Postponed: needs info

Component

Correction/Clarification

Created by

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿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.

  • 🇳🇿New Zealand quietone

    OK, setting back to NW.

  • 🇮🇹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 of select(). Dynamic queries should be used when the query parts vary, when they should be alterable, or when an extendable query class is used.

Production build 0.71.5 2024