MySQL ANY_VALUE with fallback to MIN
https://github.com/friendica/friendica/issues/3322
This commit is contained in:
parent
38e7a0f793
commit
1058b28cea
7 changed files with 42 additions and 22 deletions
|
@ -3099,13 +3099,14 @@ use \Friendica\Core\Config;
|
|||
|
||||
$scale = (x($_REQUEST, 'scale') ? intval($_REQUEST['scale']) : false);
|
||||
$scale_sql = ($scale === false ? "" : sprintf("and scale=%d",intval($scale)));
|
||||
$data_sql = ($scale === false ? "" : "data, ");
|
||||
$data_sql = ($scale === false ? "" : "ANY_VALUE(data) AS data,");
|
||||
|
||||
$r = q("select %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
|
||||
`type`, `height`, `width`, `datasize`, `profile`, min(`scale`) as minscale, max(`scale`) as maxscale
|
||||
from photo where `uid` = %d and `resource-id` = '%s' %s
|
||||
group by `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
|
||||
`type`, `height`, `width`, `datasize`, `profile`",
|
||||
$r = q("select %s ANY_VALUE(`resource-id`) AS `resource-id`, ANY_VALUE(`created`) AS `created`,
|
||||
ANY_VALUE(`edited`) AS `edited`, ANY_VALUE(`title`) AS `title`, ANY_VALUE(`desc`) AS `desc`,
|
||||
ANY_VALUE(`album`) AS `album`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
|
||||
ANY_VALUE(`height`) AS `height`, ANY_VALUE(`width`) AS `width`, ANY_VALUE(`datasize`) AS `datasize`,
|
||||
ANY_VALUE(`profile`) AS `profile`, min(`scale`) as minscale, max(`scale`) as maxscale
|
||||
from photo where `uid` = %d and `resource-id` = '%s' %s",
|
||||
$data_sql,
|
||||
intval(local_user()),
|
||||
dbesc($_REQUEST['photo_id']),
|
||||
|
|
|
@ -504,6 +504,14 @@ function dbesc($str) {
|
|||
}
|
||||
}
|
||||
|
||||
function any_value_fallback($sql) {
|
||||
//Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
|
||||
//ANY_VALUE() is available from MySQL 5.7 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
|
||||
//A standard fallback is to use MIN(), or nothing () in old MySQL 5.6-
|
||||
//TODO: Skip this line when we know we are on a platform supporting ANY_VALUE()
|
||||
return str_ireplace('ANY_VALUE(', 'MIN(', $sql);
|
||||
}
|
||||
|
||||
// Function: q($sql,$args);
|
||||
// Description: execute SQL query with printf style args.
|
||||
// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
|
||||
|
@ -514,6 +522,7 @@ function q($sql) {
|
|||
unset($args[0]);
|
||||
|
||||
if ($db && $db->connected) {
|
||||
$sql = any_value_fallback($sql);
|
||||
$stmt = @vsprintf($sql,$args); // Disabled warnings
|
||||
//logger("dba: q: $stmt", LOGGER_ALL);
|
||||
if ($stmt === false)
|
||||
|
@ -550,6 +559,7 @@ function qu($sql) {
|
|||
unset($args[0]);
|
||||
|
||||
if ($db && $db->connected) {
|
||||
$sql = any_value_fallback($sql);
|
||||
$stmt = @vsprintf($sql,$args); // Disabled warnings
|
||||
if ($stmt === false)
|
||||
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
||||
|
|
|
@ -516,8 +516,9 @@ function notifier_run(&$argv, &$argc){
|
|||
$r0 = Diaspora::relay_list();
|
||||
}
|
||||
|
||||
$r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'
|
||||
AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch`, `id` ORDER BY rand()",
|
||||
$r1 = q("SELECT `batch`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`name`) AS `name`, ANY_VALUE(`network`) AS `network`
|
||||
FROM `contact` WHERE `network` = '%s'
|
||||
AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch` ORDER BY rand()",
|
||||
dbesc(NETWORK_DIASPORA),
|
||||
intval($owner['uid']),
|
||||
intval(CONTACT_IS_SHARING)
|
||||
|
|
|
@ -48,20 +48,20 @@ function photo_albums($uid, $update = false) {
|
|||
if (!Config::get('system', 'no_count', false)) {
|
||||
/// @todo This query needs to be renewed. It is really slow
|
||||
// At this time we just store the data in the cache
|
||||
$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`
|
||||
$albums = qu("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
|
||||
FROM `photo`
|
||||
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
|
||||
GROUP BY `album`, `created` ORDER BY `created` DESC",
|
||||
GROUP BY `album` ORDER BY `created` DESC",
|
||||
intval($uid),
|
||||
dbesc('Contact Photos'),
|
||||
dbesc(t('Contact Photos'))
|
||||
);
|
||||
} else {
|
||||
// This query doesn't do the count and is much faster
|
||||
$albums = qu("SELECT DISTINCT(`album`), '' AS `total`
|
||||
$albums = qu("SELECT DISTINCT(`album`), '' AS `total`, ANY_VALUE(`created`) AS `created`
|
||||
FROM `photo`
|
||||
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
|
||||
GROUP BY `album`, `created` ORDER BY `created` DESC",
|
||||
GROUP BY `album` ORDER BY `created` DESC",
|
||||
intval($uid),
|
||||
dbesc('Contact Photos'),
|
||||
dbesc(t('Contact Photos'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue