arrayfromquery optimizations: move double if from inside loop to outside + use directly mysqli calls to avoid function call overhead for every row retrieved from db

git-svn-id: http://piwigo.org/svn/trunk@27336 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
rvelices 2014-02-11 21:47:44 +00:00
parent 78621a3487
commit 2d9887993c
6 changed files with 192 additions and 131 deletions

View file

@ -133,14 +133,13 @@ SELECT representative_picture_id
if ($conf['display_fromto']) if ($conf['display_fromto'])
{ {
$dates_of_category = array();
if (count($category_ids) > 0) if (count($category_ids) > 0)
{ {
$query = ' $query = '
SELECT SELECT
category_id, category_id,
MIN(date_creation) AS date_creation_min, MIN(date_creation) AS `from`,
MAX(date_creation) AS date_creation_max MAX(date_creation) AS `to`
FROM '.IMAGE_CATEGORY_TABLE.' FROM '.IMAGE_CATEGORY_TABLE.'
INNER JOIN '.IMAGES_TABLE.' ON image_id = id INNER JOIN '.IMAGES_TABLE.' ON image_id = id
WHERE category_id IN ('.implode(',', $category_ids).') WHERE category_id IN ('.implode(',', $category_ids).')
@ -155,14 +154,7 @@ SELECT
).' ).'
GROUP BY category_id GROUP BY category_id
;'; ;';
$result = pwg_query($query); $dates_of_category = query2array($query, 'category_id');
while ($row = pwg_db_fetch_assoc($result))
{
$dates_of_category[ $row['category_id'] ] = array(
'from' => $row['date_creation_min'],
'to' => $row['date_creation_max'],
);
}
} }
} }

View file

@ -760,4 +760,74 @@ function my_error($header, $die)
echo("</pre>"); echo("</pre>");
} }
/**
* Builds an data array from a SQL query.
* Depending on $key_name and $value_name it can return :
*
* - an array of arrays of all fields (key=null, value=null)
* array(
* array('id'=>1, 'name'=>'DSC8956', ...),
* array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an array of a single field (key=null, value='...')
* array('DSC8956', 'DSC8957', ...)
*
* - an associative array of array of all fields (key='...', value=null)
* array(
* 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
* 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an associative array of a single field (key='...', value='...')
* array(
* 'DSC8956' => 1,
* 'DSC8957' => 2,
* ...
* )
*
* @since 2.6
*
* @param string $query
* @param string $key_name
* @param string $value_name
* @return array
*/
function query2array($query, $key_name=null, $value_name=null)
{
$result = pwg_query($query);
$data = array();
if (isset($key_name))
{
if (isset($value_name))
{
while ($row = pwg_db_fetch_assoc($result))
$data[ $row[$key_name] ] = $row[$value_name];
}
else
{
while ($row = pwg_db_fetch_assoc($result))
$data[ $row[$key_name] ] = $row;
}
}
else
{
if (isset($value_name))
{
while ($row = pwg_db_fetch_assoc($result))
$data[] = $row[$value_name];
}
else
{
while ($row = pwg_db_fetch_assoc($result))
$data[] = $row;
}
}
return $data;
}
?> ?>

View file

@ -799,4 +799,74 @@ function my_error($header, $die)
echo("</pre>"); echo("</pre>");
} }
/**
* Builds an data array from a SQL query.
* Depending on $key_name and $value_name it can return :
*
* - an array of arrays of all fields (key=null, value=null)
* array(
* array('id'=>1, 'name'=>'DSC8956', ...),
* array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an array of a single field (key=null, value='...')
* array('DSC8956', 'DSC8957', ...)
*
* - an associative array of array of all fields (key='...', value=null)
* array(
* 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
* 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an associative array of a single field (key='...', value='...')
* array(
* 'DSC8956' => 1,
* 'DSC8957' => 2,
* ...
* )
*
* @since 2.6
*
* @param string $query
* @param string $key_name
* @param string $value_name
* @return array
*/
function query2array($query, $key_name=null, $value_name=null)
{
$result = pwg_query($query);
$data = array();
if (isset($key_name))
{
if (isset($value_name))
{
while ($row = $result->fetch_assoc())
$data[ $row[$key_name] ] = $row[$value_name];
}
else
{
while ($row = $result->fetch_assoc())
$data[ $row[$key_name] ] = $row;
}
}
else
{
if (isset($value_name))
{
while ($row = $result->fetch_assoc())
$data[] = $row[$value_name];
}
else
{
while ($row = $result->fetch_assoc())
$data[] = $row;
}
}
return $data;
}
?> ?>

View file

@ -933,7 +933,7 @@ SELECT element_id
FROM '.CADDIE_TABLE.' FROM '.CADDIE_TABLE.'
WHERE user_id = '.$user['id'].' WHERE user_id = '.$user['id'].'
;'; ;';
$in_caddie = array_from_query($query, 'element_id'); $in_caddie = query2array($query, null, 'element_id');
$caddiables = array_diff($elements_id, $in_caddie); $caddiables = array_diff($elements_id, $in_caddie);
@ -1165,13 +1165,11 @@ SELECT param, value
function conf_update_param($param, $value) function conf_update_param($param, $value)
{ {
$query = ' $query = '
SELECT SELECT param
param,
value
FROM '.CONFIG_TABLE.' FROM '.CONFIG_TABLE.'
WHERE param = \''.$param.'\' WHERE param = \''.$param.'\'
;'; ;';
$params = array_from_query($query, 'param'); $params = query2array($query, null, 'param');
if (count($params) == 0) if (count($params) == 0)
{ {
@ -1243,69 +1241,6 @@ function prepend_append_array_items($array, $prepend_str, $append_str)
return $array; return $array;
} }
/**
* Builds an data array from a SQL query.
* Depending on $key_name and $value_name it can return :
*
* - an array of arrays of all fields (key=null, value=null)
* array(
* array('id'=>1, 'name'=>'DSC8956', ...),
* array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an array of a single field (key=null, value='...')
* array('DSC8956', 'DSC8957', ...)
*
* - an associative array of array of all fields (key='...', value=null)
* array(
* 'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
* 'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
* ...
* )
*
* - an associative array of a single field (key='...', value='...')
* array(
* 'DSC8956' => 1,
* 'DSC8957' => 2,
* ...
* )
*
* @since 2.6
*
* @param string $query
* @param string $key_name
* @param string $value_name
* @return array
*/
function query2array($query, $key_name=null, $value_name=null)
{
$result = pwg_query($query);
$data = array();
while ($row = pwg_db_fetch_assoc($result))
{
if (isset($value_name))
{
$value = $row[ $value_name ];
}
else
{
$value = $row;
}
if (isset($key_name))
{
$data[ $row[$key_name] ] = $value;
}
else
{
$data[] = $value;
}
}
return $data;
}
/** /**
* creates an simple hashmap based on a SQL query. * creates an simple hashmap based on a SQL query.
* choose one to be the key, another one to be the value. * choose one to be the key, another one to be the value.

View file

@ -70,7 +70,7 @@ SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter
).' ).'
GROUP BY tag_id GROUP BY tag_id
;'; ;';
$tag_counters = simple_hash_from_query($query, 'tag_id', 'counter'); $tag_counters = query2array($query, 'tag_id', 'counter');
if ( empty($tag_counters) ) if ( empty($tag_counters) )
{ {
@ -233,7 +233,7 @@ SELECT id
} }
$query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by); $query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by);
return array_from_query($query, 'id'); return query2array($query, null, 'id');
} }
/** /**
@ -320,13 +320,7 @@ SELECT *
WHERE '. implode( ' WHERE '. implode( '
OR ', $where_clauses); OR ', $where_clauses);
$result = pwg_query($query); return query2array($query);
$tags = array();
while ($row = pwg_db_fetch_assoc($result))
{
$tags[] = $row;
}
return $tags;
} }
?> ?>

View file

@ -293,7 +293,7 @@ SELECT id
"\n AND" "\n AND"
); );
$subcat_ids = array_from_query($query, 'id'); $subcat_ids = query2array($query,null, 'id');
$subcat_ids[] = $page['category']['id']; $subcat_ids[] = $page['category']['id'];
$where_sql = 'category_id IN ('.implode(',',$subcat_ids).')'; $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
// remove categories from forbidden because just checked above // remove categories from forbidden because just checked above
@ -325,7 +325,7 @@ SELECT DISTINCT(image_id)
'.$conf['order_by'].' '.$conf['order_by'].'
;'; ;';
$page['items'] = array_from_query($query, 'image_id'); $page['items'] = query2array($query,null, 'image_id');
} }
} }
// special sections // special sections
@ -416,7 +416,7 @@ SELECT image_id
$page = array_merge( $page = array_merge(
$page, $page,
array( array(
'items' => array_from_query($query, 'image_id'), 'items' => query2array($query,null, 'image_id'),
) )
); );
@ -463,7 +463,7 @@ SELECT DISTINCT(id)
array( array(
'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">' 'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
.l10n('Recent photos').'</a>', .l10n('Recent photos').'</a>',
'items' => array_from_query($query, 'id'), 'items' => query2array($query,null, 'id'),
) )
); );
} }
@ -502,7 +502,7 @@ SELECT DISTINCT(id)
array( array(
'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">' 'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
.$conf['top_number'].' '.l10n('Most visited').'</a>', .$conf['top_number'].' '.l10n('Most visited').'</a>',
'items' => array_from_query($query, 'id'), 'items' => query2array($query,null, 'id'),
) )
); );
} }
@ -528,7 +528,7 @@ SELECT DISTINCT(id)
array( array(
'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">' 'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
.$conf['top_number'].' '.l10n('Best rated').'</a>', .$conf['top_number'].' '.l10n('Best rated').'</a>',
'items' => array_from_query($query, 'id'), 'items' => query2array($query,null, 'id'),
) )
); );
} }
@ -551,7 +551,7 @@ SELECT DISTINCT(id)
array( array(
'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">' 'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
.l10n('Random photos').'</a>', .l10n('Random photos').'</a>',
'items' => array_from_query($query, 'id'), 'items' => query2array($query,null, 'id'),
) )
); );
} }