-
Notifications
You must be signed in to change notification settings - Fork 7
Description
In the function doc for db_query it says:
Use this function for SELECT queries if it is just a simple query string. If the caller or other modules need to change the query, use db_select() instead.
In the Database API documentation (copied from D7) we have on the Static Queries page:
The most common SELECT queries in Backdrop are static queries using the db_query() function. Static queries are passed to the database nearly verbatim, as in the following example.
$query = db_query("SELECT nid, title FROM {node}"); $records = $query->fetchAll(); foreach ($records as $record) { // Do something. }Only very simple SELECT queries should use the static query mechanism. You should use a dynamic query if you need more complex queries, dynamic query generation or alterability.
It is not clear what is a very simple static query and these issues from the Drupal issue queue suggest there are performance penalties to using db_select unless you absolutely have to:
- https://www.drupal.org/project/drupal/issues/1881146
- https://www.drupal.org/project/documentation/issues/835068 (note @avpaderno has recently contributed to this discussion)
- https://www.drupal.org/project/drupal/issues/1067802
Areas I think it would be useful to cover:
- Is a query with placeholders that refer to variables ok for
db_query? - What about
JOINclauses? - Examples of what some of the edge cases of each might be and why you would go to either
db_queryordb_select