#196862 by Damien Tournoud, et al: Replace COUNT(*) queries with SELECT 1 ... LIMIT 1 queries when all that's required is a check for whether rows exist.

merge-requests/26/head
Angie Byron 2009-05-16 15:23:16 +00:00
parent 8b63d832de
commit f577c125e8
11 changed files with 29 additions and 26 deletions

View File

@ -272,7 +272,8 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
function block_admin_configure_validate($form, &$form_state) { function block_admin_configure_validate($form, &$form_state) {
if ($form_state['values']['module'] == 'block') { 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.')); 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) { 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.')); form_set_error('info', t('Please ensure that each block description is unique.'));
} }
} }

View File

@ -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 ($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. // 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'])); $has_blocks = (bool) db_query_range('SELECT 1 FROM {block} WHERE theme = :theme', array(':theme' => $form_state['values']['admin_theme']), 0, 1)->fetchField();
if (!$result) { if (!$has_blocks) {
block_initialize_theme_blocks($form_state['values']['admin_theme']); 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) { function block_initialize_theme_blocks($theme) {
// Initialize theme's blocks if none already registered. // 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'); $default_theme = variable_get('theme_default', 'garland');
$regions = system_region_list($theme); $regions = system_region_list($theme);
$result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $default_theme); $result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $default_theme);

View File

@ -555,7 +555,7 @@ function node_types_rebuild() {
function node_type_save($info) { function node_type_save($info) {
$is_existing = FALSE; $is_existing = FALSE;
$existing_type = !empty($info->old_type) ? $info->old_type : $info->type; $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); $type = node_type_set_defaults($info);
$fields = array( $fields = array(
@ -3085,8 +3085,8 @@ function node_assign_owner_action_form($context) {
} }
function node_assign_owner_action_validate($form, $form_state) { 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(); $exists = (bool) db_query_range('SELECT 1 FROM {users} WHERE name = :name', array(':name' => $form_state['values']['owner_name']), 0, 1)->fetchField();
if (intval($count) != 1) { if (!$exists) {
form_set_error('owner_name', t('Please enter a valid username.')); form_set_error('owner_name', t('Please enter a valid username.'));
} }
} }

View File

@ -15,8 +15,8 @@ function path_admin_overview($keys = NULL) {
// Add the filter form above the overview table. // Add the filter form above the overview table.
$output = drupal_get_form('path_admin_filter_form', $keys); $output = drupal_get_form('path_admin_filter_form', $keys);
// Enable language column if locale is enabled or if we have any alias with language // 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(); $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', array(':language' => ''), 0, 1)->fetchField();
$multilanguage = (module_exists('locale') || $count); $multilanguage = (module_exists('locale') || $alias_exists);
$header = array( $header = array(
array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'), array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),

View File

@ -10,7 +10,7 @@
* Implementation of hook_install(). * Implementation of hook_install().
*/ */
function php_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 // 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 // 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 // reliable method to identify the format in an uninstall hook or in

View File

@ -511,7 +511,8 @@ function profile_category_access($account, $category) {
return TRUE; return TRUE;
} }
else { 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;
} }
} }

View File

@ -110,7 +110,8 @@ function profile_browse() {
*/ */
function profile_autocomplete($field, $string) { function profile_autocomplete($field, $string) {
$matches = array(); $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( $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, ':fid' => $field,
':value' => $string .'%', ':value' => $string .'%',

View File

@ -1199,8 +1199,8 @@ function hook_file_move($file, $source) {
*/ */
function hook_file_references($file) { function hook_file_references($file) {
// If upload.module is still using a file, do not let other modules delete it. // 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(); $file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($count) { if ($file_used) {
// Return the name of the module and how many references it has to the file. // Return the name of the module and how many references it has to the file.
return array('upload' => $count); return array('upload' => $count);
} }

View File

@ -282,8 +282,8 @@ function upload_file_load($files) {
*/ */
function upload_file_references($file) { function upload_file_references($file) {
// If upload.module is still using a file, do not let other modules delete it. // 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(); $file_used = (bool) db_query_range('SELECT 1 FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($count) { if ($file_used) {
// Return the name of the module and how many references it has to the file. // Return the name of the module and how many references it has to the file.
return array('upload' => $count); return array('upload' => $count);
} }

View File

@ -731,16 +731,13 @@ function user_admin_role() {
function user_admin_role_validate($form, &$form_state) { function user_admin_role_validate($form, &$form_state) {
if ($form_state['values']['name']) { if ($form_state['values']['name']) {
if ($form_state['values']['op'] == t('Save role')) { if ($form_state['values']['op'] == t('Save role')) {
$existing_role = db_query("SELECT COUNT(*) FROM {role} WHERE name = :name AND rid != :rid", $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();
array(':name' => $form_state['values']['name'],
':rid' => $form_state['values']['rid']))
->fetchField();
if ($existing_role) { 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']))); 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')) { 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']))); form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
} }
} }

View File

@ -822,8 +822,8 @@ function user_file_download($filepath) {
*/ */
function user_file_references($file) { function user_file_references($file) {
// Determine if the file is used by this module. // Determine if the file is used by this module.
$count = db_query('SELECT COUNT(*) FROM {users} WHERE picture = :fid', array(':fid' => $file->fid))->fetchField(); $file_used = (bool) db_query_range('SELECT 1 FROM {users} WHERE picture = :fid', array(':fid' => $file->fid), 0, 1)->fetchField();
if ($count) { if ($file_used) {
// Return the name of the module and how many references it has to the file. // Return the name of the module and how many references it has to the file.
return array('user' => $count); return array('user' => $count);
} }
@ -935,7 +935,7 @@ function user_user_validate(&$edit, &$account, $category = NULL) {
if ($error = user_validate_name($edit['name'])) { if ($error = user_validate_name($edit['name'])) {
form_set_error('name', $error); 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']))); 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'])) { if ($error = user_validate_mail($edit['mail'])) {
form_set_error('mail', $error); 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. // Format error message dependent on whether the user is logged in or not.
if ($GLOBALS['user']->uid) { if ($GLOBALS['user']->uid) {
form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $edit['mail']))); form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $edit['mail'])));