#196862 by Damien Tournoud: SELECT COUNT(*) are expensive queries, remove them when possible. From: damz --- block/block.admin.inc | 7 +++++-- block/block.module | 7 ++++--- node/node.module | 6 +++--- path/path.admin.inc | 4 ++-- php/php.install | 2 +- profile/profile.module | 3 ++- profile/profile.pages.inc | 3 ++- system/system.api.php | 4 ++-- upload/upload.module | 4 ++-- user/user.admin.inc | 7 ++----- user/user.module | 8 ++++---- 11 files changed, 29 insertions(+), 26 deletions(-) diff --git modules/block/block.admin.inc modules/block/block.admin.inc index 84acfce..6054749 100644 --- modules/block/block.admin.inc +++ modules/block/block.admin.inc @@ -272,7 +272,8 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) { function block_admin_configure_validate($form, &$form_state) { if ($form_state['values']['module'] == 'block') { - if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {box} WHERE bid <> %d AND info = '%s'", $form_state['values']['delta'], $form_state['values']['info']))) { + $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE bid <> :bid AND info = :info', array(':bid' => $form_state['values']['delta'], ':info' => $form_state['values']['info']), 0, 1)->fetchField(); + if (empty($form_state['values']['info']) || $box_exists) { form_set_error('info', t('Please ensure that each block description is unique.')); } } @@ -301,7 +302,9 @@ function block_add_block_form(&$form_state) { } function block_add_block_form_validate($form, &$form_state) { - if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {box} WHERE info = '%s'", $form_state['values']['info']))) { + $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE info = :info', array(':info' => $form_state['values']['info']), 0, 1)->fetchField(); + + if (empty($form_state['values']['info']) || $box_exists) { form_set_error('info', t('Please ensure that each block description is unique.')); } } diff --git modules/block/block.module modules/block/block.module index cb0be34..58f0e03 100644 --- modules/block/block.module +++ modules/block/block.module @@ -494,8 +494,8 @@ function block_system_themes_form_submit(&$form, &$form_state) { } if ($form_state['values']['admin_theme'] && $form_state['values']['admin_theme'] != variable_get('admin_theme', 0)) { // If we're changing themes, make sure the theme has its blocks initialized. - $result = db_result(db_query("SELECT COUNT(*) FROM {block} WHERE theme = '%s'", $form_state['values']['admin_theme'])); - if (!$result) { + $has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $form_state['values']['admin_theme']), 0, 1)->fetchField(); + if (!$has_blocks) { block_initialize_theme_blocks($form_state['values']['admin_theme']); } } @@ -515,7 +515,8 @@ function block_system_themes_form_submit(&$form, &$form_state) { */ function block_initialize_theme_blocks($theme) { // Initialize theme's blocks if none already registered. - if (!(db_result(db_query("SELECT COUNT(*) FROM {block} WHERE theme = '%s'", $theme)))) { + $has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $theme), 0, 1)->fetchField(); + if (!$has_blocks) { $default_theme = variable_get('theme_default', 'garland'); $regions = system_region_list($theme); $result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $default_theme); diff --git modules/node/node.module modules/node/node.module index a4c7cf8..7e683c3 100644 --- modules/node/node.module +++ modules/node/node.module @@ -555,7 +555,7 @@ function node_types_rebuild() { function node_type_save($info) { $is_existing = FALSE; $existing_type = !empty($info->old_type) ? $info->old_type : $info->type; - $is_existing = db_query("SELECT COUNT(*) FROM {node_type} WHERE type = :type", array(':type' => $existing_type))->fetchField(); + $is_existing = (bool) db_query_range('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $existing_type), 0, 1)->fetchField(); $type = node_type_set_defaults($info); $fields = array( @@ -3085,8 +3085,8 @@ function node_assign_owner_action_form($context) { } function node_assign_owner_action_validate($form, $form_state) { - $count = db_query('SELECT COUNT(*) FROM {users} WHERE name = :name', array(':name' => $form_state['values']['owner_name']))->fetchField(); - if (intval($count) != 1) { + $exists = (bool) db_query_range('SELECT 1 FROM {users} WHERE name = :name', array(':name' => $form_state['values']['owner_name']), 0, 1)->fetchField(); + if (!$exists) { form_set_error('owner_name', t('Please enter a valid username.')); } } diff --git modules/path/path.admin.inc modules/path/path.admin.inc index ae34711..b1d6c75 100644 --- modules/path/path.admin.inc +++ modules/path/path.admin.inc @@ -15,8 +15,8 @@ function path_admin_overview($keys = NULL) { // Add the filter form above the overview table. $output = drupal_get_form('path_admin_filter_form', $keys); // Enable language column if locale is enabled or if we have any alias with language - $count = db_query("SELECT COUNT(*) FROM {url_alias} WHERE language <> ''")->fetchField(); - $multilanguage = (module_exists('locale') || $count); + $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', array(':language' => ''), 0, 1)->fetchField(); + $multilanguage = (module_exists('locale') || $alias_exists); $header = array( array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'), diff --git modules/php/php.install modules/php/php.install index bb695ca..3a4291b 100644 --- modules/php/php.install +++ modules/php/php.install @@ -10,7 +10,7 @@ * Implementation of hook_install(). */ function php_install() { - $format_exists = db_query("SELECT COUNT(*) FROM {filter_format} WHERE name = 'PHP code'")->fetchField(); + $format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', array(':name' => 'PHP code'), 0, 1)->fetchField(); // Add a PHP code text format, if it does not exist. Do this only for the // first install (or if the format has been manually deleted) as there is no // reliable method to identify the format in an uninstall hook or in diff --git modules/profile/profile.module modules/profile/profile.module index a7fc22a..26d5e21 100644 --- modules/profile/profile.module +++ modules/profile/profile.module @@ -511,7 +511,8 @@ function profile_category_access($account, $category) { return TRUE; } else { - return user_edit_access($account) && db_result(db_query("SELECT COUNT(*) FROM {profile_field} WHERE category = '%s' AND visibility <> %d", $category, PROFILE_HIDDEN)); + $category_visible = (bool) db_query_range('SELECT 1 FROM {profile_field} WHERE category = :category AND visibility <> :visibility', array(':category' => $category, ':visibility' => PROFILE_HIDDEN), 0, 1)->fetchField(); + return user_edit_access($account) && $category_visible; } } diff --git modules/profile/profile.pages.inc modules/profile/profile.pages.inc index 71394c5..8c3d434 100644 --- modules/profile/profile.pages.inc +++ modules/profile/profile.pages.inc @@ -110,7 +110,8 @@ function profile_browse() { */ function profile_autocomplete($field, $string) { $matches = array(); - if (db_result(db_query("SELECT COUNT(*) FROM {profile_field} WHERE fid = %d AND autocomplete = 1", $field))) { + $autocomplete_field = (bool) db_query_range("SELECT 1 FROM {profile_field} WHERE fid = :fid AND autocomplete = 1", array(':fid' => $field), 0, 1)->fetchField(); + if ($autocomplete_field) { $result = db_query_range("SELECT value FROM {profile_value} WHERE fid = :fid AND LOWER(value) LIKE LOWER(:value) GROUP BY value ORDER BY value ASC", array( ':fid' => $field, ':value' => $string .'%', diff --git modules/system/system.api.php modules/system/system.api.php index fd4058f..cbfcaf5 100644 --- modules/system/system.api.php +++ modules/system/system.api.php @@ -1199,8 +1199,8 @@ function hook_file_move($file, $source) { */ function hook_file_references($file) { // If upload.module is still using a file, do not let other modules delete it. - $count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField(); - if ($count) { + $file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField(); + if ($file_used) { // Return the name of the module and how many references it has to the file. return array('upload' => $count); } diff --git modules/upload/upload.module modules/upload/upload.module index e3fae30..71c366b 100644 --- modules/upload/upload.module +++ modules/upload/upload.module @@ -282,8 +282,8 @@ function upload_file_load($files) { */ function upload_file_references($file) { // If upload.module is still using a file, do not let other modules delete it. - $count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField(); - if ($count) { + $file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField(); + if ($file_used) { // Return the name of the module and how many references it has to the file. return array('upload' => $count); } diff --git modules/user/user.admin.inc modules/user/user.admin.inc index 7d3757d..1eb5b05 100644 --- modules/user/user.admin.inc +++ modules/user/user.admin.inc @@ -731,16 +731,13 @@ function user_admin_role() { function user_admin_role_validate($form, &$form_state) { if ($form_state['values']['name']) { if ($form_state['values']['op'] == t('Save role')) { - $existing_role = db_query("SELECT COUNT(*) FROM {role} WHERE name = :name AND rid != :rid", - array(':name' => $form_state['values']['name'], - ':rid' => $form_state['values']['rid'])) - ->fetchField(); + $existing_role = (bool) db_query_range("SELECT 1 FROM {role} WHERE name = :name AND rid != :rid", array(':name' => $form_state['values']['name'], ':rid' => $form_state['values']['rid']), 0, 1)->fetchField(); if ($existing_role) { form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name']))); } } elseif ($form_state['values']['op'] == t('Add role')) { - if (db_query("SELECT COUNT(*) FROM {role} WHERE name = :name", array(':name' => $form_state['values']['name']))->fetchField()) { + if ((bool) db_query_range('SELECT 1 FROM {role} WHERE name = :name', array(':name' => $form_state['values']['name']), 0, 1)->fetchField()) { form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name']))); } } diff --git modules/user/user.module modules/user/user.module index 6a01833..b1b371a 100644 --- modules/user/user.module +++ modules/user/user.module @@ -822,8 +822,8 @@ function user_file_download($filepath) { */ function user_file_references($file) { // Determine if the file is used by this module. - $count = db_query('SELECT COUNT(*) FROM {users} WHERE picture = :fid', array(':fid' => $file->fid))->fetchField(); - if ($count) { + $file_used = (bool) db_query_range('SELECT 1 FROM {users} WHERE picture = :fid', array(':fid' => $file->fid), 0, 1)->fetchField(); + if ($file_used) { // Return the name of the module and how many references it has to the file. return array('user' => $count); } @@ -935,7 +935,7 @@ function user_user_validate(&$edit, &$account, $category = NULL) { if ($error = user_validate_name($edit['name'])) { form_set_error('name', $error); } - elseif (db_query("SELECT COUNT(*) FROM {users} WHERE uid != :uid AND LOWER(name) = LOWER(:name)", array(':uid' => $uid, ':name' => $edit['name']))->fetchField() > 0) { + elseif ((bool) db_query_range("SELECT 1 FROM {users} WHERE uid != :uid AND LOWER(name) = LOWER(:name)", array(':uid' => $uid, ':name' => $edit['name']), 0, 1)->fetchField()) { form_set_error('name', t('The name %name is already taken.', array('%name' => $edit['name']))); } } @@ -944,7 +944,7 @@ function user_user_validate(&$edit, &$account, $category = NULL) { if ($error = user_validate_mail($edit['mail'])) { form_set_error('mail', $error); } - elseif (db_query("SELECT COUNT(*) FROM {users} WHERE uid != :uid AND LOWER(mail) = LOWER(:mail)", array(':uid' => $uid, ':mail' => $edit['mail']))->fetchField() > 0) { + elseif ((bool) db_query_range("SELECT 1 FROM {users} WHERE uid != :uid AND LOWER(mail) = LOWER(:mail)", array(':uid' => $uid, ':mail' => $edit['mail']), 0, 1)->fetchField()) { // Format error message dependent on whether the user is logged in or not. if ($GLOBALS['user']->uid) { form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $edit['mail'])));