Skip to content

Commit

Permalink
Merge pull request #3799 from jonasraoni/feature/stable-3_3_0/8700-im…
Browse files Browse the repository at this point in the history
…prove-slow-query

Feature/stable 3 3 0/8700 improve slow query
  • Loading branch information
jonasraoni authored May 30, 2024
2 parents 3dfe7b6 + e8a0b1e commit 7c61f93
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 118 deletions.
36 changes: 19 additions & 17 deletions classes/article/ArticleGalleyDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,19 @@ function getExportable($contextId, $pubIdType = null, $title = null, $author = n
$params[] = $pubIdSettingValue;
}

$result = $this->retrieveRange(
$sql = 'SELECT g.*
FROM publication_galleys g
LEFT JOIN publications p ON (p.publication_id = g.publication_id)
LEFT JOIN publication_settings ps ON (ps.publication_id = p.publication_id)
LEFT JOIN submissions s ON (s.submission_id = p.submission_id)
LEFT JOIN submission_files sf ON (g.submission_file_id = sf.submission_file_id)
' . ($pubIdType != null?' LEFT JOIN publication_galley_settings gs ON (g.galley_id = gs.galley_id)':'')
. ($title != null?' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)':'')
. ($author != null?' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \''.IDENTITY_SETTING_GIVENNAME.'\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \''.IDENTITY_SETTING_FAMILYNAME.'\')
':'')
. ($pubIdSettingName != null?' LEFT JOIN publication_galley_settings gss ON (g.galley_id = gss.galley_id AND gss.setting_name = ?)':'') .'
$baseSql = '
FROM publication_galleys g
LEFT JOIN publications p ON (p.publication_id = g.publication_id)
LEFT JOIN publication_settings ps ON (ps.publication_id = p.publication_id)
LEFT JOIN submissions s ON (s.submission_id = p.submission_id)
LEFT JOIN submission_files sf ON (g.submission_file_id = sf.submission_file_id)
' . ($pubIdType != null?' LEFT JOIN publication_galley_settings gs ON (g.galley_id = gs.galley_id)':'')
. ($title != null?' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)':'')
. ($author != null?' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \''.IDENTITY_SETTING_GIVENNAME.'\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \''.IDENTITY_SETTING_FAMILYNAME.'\')
':'')
. ($pubIdSettingName != null?' LEFT JOIN publication_galley_settings gss ON (g.galley_id = gss.galley_id AND gss.setting_name = ?)':'') .'
WHERE
s.status = ? AND s.context_id = ?
' . ($pubIdType != null?' AND gs.setting_name = ? AND gs.setting_value IS NOT NULL':'')
Expand All @@ -333,12 +332,15 @@ function getExportable($contextId, $pubIdType = null, $title = null, $author = n
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED)?' AND gss.setting_value IS NULL':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED)?' AND gss.setting_value = ?':'')
. (($pubIdSettingName != null && is_null($pubIdSettingValue))?' AND (gss.setting_value IS NULL OR gss.setting_value = \'\')':'') .'
GROUP BY g.galley_id
ORDER BY p.date_published DESC, p.publication_id DESC, g.galley_id DESC',
GROUP BY g.galley_id
';

$result = $this->retrieveRange(
"SELECT g.* {$baseSql} ORDER BY p.date_published DESC, p.publication_id DESC, g.galley_id DESC",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo);
}
}
74 changes: 40 additions & 34 deletions classes/article/AuthorDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,52 @@ function getAuthorsAlphabetizedByJournal($journalId = null, $initial = null, $ra
$initialSql .= ')';
}

$baseSql = '
FROM authors a
JOIN user_groups ug ON (a.user_group_id = ug.user_group_id)
JOIN publications p ON (p.publication_id = a.publication_id)
JOIN submissions s ON (s.current_publication_id = p.publication_id)
LEFT JOIN author_settings agl ON (a.author_id = agl.author_id AND agl.setting_name = ? AND agl.locale = ?)
LEFT JOIN author_settings agpl ON (a.author_id = agpl.author_id AND agpl.setting_name = ? AND agpl.locale = s.locale)
LEFT JOIN author_settings afl ON (a.author_id = afl.author_id AND afl.setting_name = ? AND afl.locale = ?)
LEFT JOIN author_settings afpl ON (a.author_id = afpl.author_id AND afpl.setting_name = ? AND afpl.locale = s.locale)
JOIN (
SELECT
MIN(aa.author_id) as author_id,
CONCAT(
' . ($includeEmail ? 'aa.email, \' \', ' : '') . '
ac.setting_value,
\' \'
' . $sqlColumnsAuthorSettings . '
) as names
FROM authors aa
JOIN publications pp ON (pp.publication_id = aa.publication_id)
LEFT JOIN publication_settings ppss ON (ppss.publication_id = pp.publication_id)
JOIN submissions ss ON (ss.submission_id = pp.submission_id AND ss.current_publication_id = pp.publication_id AND ss.status = ' . STATUS_PUBLISHED . ')
JOIN journals j ON (ss.context_id = j.journal_id)
JOIN issues i ON (ppss.setting_name = ? AND ppss.setting_value = CAST(i.issue_id AS CHAR(20)) AND i.published = 1)
LEFT JOIN author_settings ac ON (ac.author_id = aa.author_id AND ac.setting_name = \'country\')
' . $sqlJoinAuthorSettings . '
WHERE j.enabled = 1
' . (isset($journalId) ? ' AND j.journal_id = ?' : '')
. $initialSql . '
GROUP BY names
) as t1 ON (t1.author_id = a.author_id)
';

$result = $this->retrieveRange(
$sql = 'SELECT a.*, ug.show_title, s.locale,
"SELECT
a.*, ug.show_title, s.locale,
COALESCE(agl.setting_value, agpl.setting_value) AS author_given,
CASE WHEN agl.setting_value <> \'\' THEN afl.setting_value ELSE afpl.setting_value END AS author_family
FROM authors a
JOIN user_groups ug ON (a.user_group_id = ug.user_group_id)
JOIN publications p ON (p.publication_id = a.publication_id)
JOIN submissions s ON (s.current_publication_id = p.publication_id)
LEFT JOIN author_settings agl ON (a.author_id = agl.author_id AND agl.setting_name = ? AND agl.locale = ?)
LEFT JOIN author_settings agpl ON (a.author_id = agpl.author_id AND agpl.setting_name = ? AND agpl.locale = s.locale)
LEFT JOIN author_settings afl ON (a.author_id = afl.author_id AND afl.setting_name = ? AND afl.locale = ?)
LEFT JOIN author_settings afpl ON (a.author_id = afpl.author_id AND afpl.setting_name = ? AND afpl.locale = s.locale)
JOIN (
SELECT
MIN(aa.author_id) as author_id,
CONCAT(
' . ($includeEmail ? 'aa.email, \' \', ' : '') . '
ac.setting_value,
\' \'
' . $sqlColumnsAuthorSettings . '
) as names
FROM authors aa
JOIN publications pp ON (pp.publication_id = aa.publication_id)
LEFT JOIN publication_settings ppss ON (ppss.publication_id = pp.publication_id)
JOIN submissions ss ON (ss.submission_id = pp.submission_id AND ss.current_publication_id = pp.publication_id AND ss.status = ' . STATUS_PUBLISHED . ')
JOIN journals j ON (ss.context_id = j.journal_id)
JOIN issues i ON (ppss.setting_name = ? AND ppss.setting_value = CAST(i.issue_id AS CHAR(20)) AND i.published = 1)
LEFT JOIN author_settings ac ON (ac.author_id = aa.author_id AND ac.setting_name = \'country\')
' . $sqlJoinAuthorSettings . '
WHERE j.enabled = 1
' . (isset($journalId) ? ' AND j.journal_id = ?' : '')
. $initialSql . '
GROUP BY names
) as t1 ON (t1.author_id = a.author_id)
ORDER BY author_family, author_given',
CASE WHEN agl.setting_value <> '' THEN afl.setting_value ELSE afpl.setting_value END AS author_family
{$baseSql}
ORDER BY
author_family, author_given",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo);
}
}

Expand Down
25 changes: 15 additions & 10 deletions classes/issue/IssueDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,15 @@ function getBySubmissionId($articleId, $journalId = null) {
* @return DAOResultFactory
*/
function getIssues($journalId, $rangeInfo = null) {
$baseSql = '
FROM issues i WHERE journal_id = ?
';
$result = $this->retrieveRange(
$sql = 'SELECT i.* FROM issues i WHERE journal_id = ? ORDER BY current DESC, date_published DESC',
"SELECT i.* {$baseSql} ORDER BY current DESC, date_published DESC",
$params = [(int) $journalId], $rangeInfo
);

return new DAOResultFactory($result, $this, '_returnIssueFromRow', [], $sql, $params, $rangeInfo); // Counted in ExportableIssuesListGridHandler
return new DAOResultFactory($result, $this, '_returnIssueFromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in ExportableIssuesListGridHandler
}

/**
Expand Down Expand Up @@ -626,24 +629,26 @@ function getExportable($contextId, $pubIdType = null, $pubIdSettingName = null,
$params[] = $pubIdSettingValue;
}

$result = $this->retrieveRange(
$sql = 'SELECT i.*
$baseSql = '
FROM issues i
LEFT JOIN custom_issue_orders o ON (o.issue_id = i.issue_id)
' . ($pubIdType != null?' LEFT JOIN issue_settings ist ON (i.issue_id = ist.issue_id)':'')
. ($pubIdSettingName != null?' LEFT JOIN issue_settings iss ON (i.issue_id = iss.issue_id AND iss.setting_name = ?)':'') .'
LEFT JOIN custom_issue_orders o ON (o.issue_id = i.issue_id)
' . ($pubIdType != null?' LEFT JOIN issue_settings ist ON (i.issue_id = ist.issue_id)':'')
. ($pubIdSettingName != null?' LEFT JOIN issue_settings iss ON (i.issue_id = iss.issue_id AND iss.setting_name = ?)':'') .'
WHERE
i.published = 1 AND i.journal_id = ?
' . ($pubIdType != null?' AND ist.setting_name = ? AND ist.setting_value IS NOT NULL':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED)?' AND iss.setting_value IS NULL':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED)?' AND iss.setting_value = ?':'')
. (($pubIdSettingName != null && is_null($pubIdSettingValue))?' AND (iss.setting_value IS NULL OR iss.setting_value = \'\')':'')
.' ORDER BY i.date_published DESC',
. (($pubIdSettingName != null && is_null($pubIdSettingValue))?' AND (iss.setting_value IS NULL OR iss.setting_value = \'\')':'') . '
';

$result = $this->retrieveRange(
"SELECT i.* {$baseSql} ORDER BY i.date_published DESC",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_returnIssueFromRow', [], $sql, $params, $rangeInfo);
return new DAOResultFactory($result, $this, '_returnIssueFromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions classes/services/queryBuilders/GalleyQueryBuilder.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ public function filterByPublicationIds($publicationIds) {
public function getCount() {
return $this
->getQuery()
->select('g.galley_id')
->get()
->count();
->getCountForPagination();
}

/**
* @copydoc PKP\Services\QueryBuilders\Interfaces\EntityQueryBuilderInterface::getCount()
* @copydoc PKP\Services\QueryBuilders\Interfaces\EntityQueryBuilderInterface::getIds()
*/
public function getIds() {
return $this
Expand All @@ -58,7 +56,7 @@ public function getIds() {
}

/**
* @copydoc PKP\Services\QueryBuilders\Interfaces\EntityQueryBuilderInterface::getCount()
* @copydoc PKP\Services\QueryBuilders\Interfaces\EntityQueryBuilderInterface::getQuery()
*/
public function getQuery() {
$this->columns = ['*'];
Expand Down
4 changes: 1 addition & 3 deletions classes/services/queryBuilders/IssueQueryBuilder.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ public function offsetBy($offset) {
public function getCount() {
return $this
->getQuery()
->select('i.issue_id')
->get()
->count();
->getCountForPagination();
}

/**
Expand Down
50 changes: 26 additions & 24 deletions classes/submission/SubmissionDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,36 @@ function getExportable($contextId, $pubIdType = null, $title = null, $author = n
$params[] = $pubIdSettingValue;
}

$baseSql = '
FROM submissions s
LEFT JOIN publications p ON s.current_publication_id = p.publication_id
LEFT JOIN publication_settings ps ON p.publication_id = ps.publication_id'
. ($issueId ? ' LEFT JOIN publication_settings psi ON p.publication_id = psi.publication_id AND psi.setting_name = \'issueId\' AND psi.locale = \'\'' : '')
. ($pubIdType != null?' LEFT JOIN publication_settings pspidt ON (p.publication_id = pspidt.publication_id)':'')
. ($title != null?' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)':'')
. ($author != null?' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \''.IDENTITY_SETTING_GIVENNAME.'\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \''.IDENTITY_SETTING_FAMILYNAME.'\')
':'')
. ($pubIdSettingName != null?' LEFT JOIN submission_settings pss ON (s.submission_id = pss.submission_id AND pss.setting_name = ?)':'') . '
WHERE s.status = ?
AND s.context_id = ?'
. ($pubIdType != null?' AND pspidt.setting_name = ? AND pspidt.setting_value IS NOT NULL':'')
. ($title != null?' AND (pst.setting_name = ? AND pst.setting_value LIKE ?)':'')
. ($author != null?' AND (asgs.setting_value LIKE ? OR asfs.setting_value LIKE ?)':'')
. ($issueId != null?' AND psi.setting_value = ?':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED)?' AND pss.setting_value IS NULL':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED)?' AND pss.setting_value = ?':'')
. (($pubIdSettingName != null && is_null($pubIdSettingValue))?' AND (pss.setting_value IS NULL OR pss.setting_value = \'\')':'') . '
GROUP BY s.submission_id
';

$result = $this->retrieveRange(
$sql = 'SELECT s.*
FROM submissions s
LEFT JOIN publications p ON s.current_publication_id = p.publication_id
LEFT JOIN publication_settings ps ON p.publication_id = ps.publication_id'
. ($issueId ? ' LEFT JOIN publication_settings psi ON p.publication_id = psi.publication_id AND psi.setting_name = \'issueId\' AND psi.locale = \'\'' : '')
. ($pubIdType != null?' LEFT JOIN publication_settings pspidt ON (p.publication_id = pspidt.publication_id)':'')
. ($title != null?' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)':'')
. ($author != null?' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \''.IDENTITY_SETTING_GIVENNAME.'\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \''.IDENTITY_SETTING_FAMILYNAME.'\')
':'')
. ($pubIdSettingName != null?' LEFT JOIN submission_settings pss ON (s.submission_id = pss.submission_id AND pss.setting_name = ?)':'')
. ' WHERE s.status = ?
AND s.context_id = ?'
. ($pubIdType != null?' AND pspidt.setting_name = ? AND pspidt.setting_value IS NOT NULL':'')
. ($title != null?' AND (pst.setting_name = ? AND pst.setting_value LIKE ?)':'')
. ($author != null?' AND (asgs.setting_value LIKE ? OR asfs.setting_value LIKE ?)':'')
. ($issueId != null?' AND psi.setting_value = ?':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED)?' AND pss.setting_value IS NULL':'')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED)?' AND pss.setting_value = ?':'')
. (($pubIdSettingName != null && is_null($pubIdSettingValue))?' AND (pss.setting_value IS NULL OR pss.setting_value = \'\')':'')
. ' GROUP BY s.submission_id
ORDER BY MAX(p.date_published) DESC, s.submission_id DESC',
"SELECT s.* {$baseSql} ORDER BY MAX(p.date_published) DESC, s.submission_id DESC",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted via paging in CrossRef export
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted via paging in CrossRef export
}
}
18 changes: 9 additions & 9 deletions classes/subscription/IndividualSubscriptionDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,21 @@ function getAll($rangeInfo = null) {
function getByJournalId($journalId, $status = null, $searchField = null, $searchMatch = null, $search = null, $dateField = null, $dateFrom = null, $dateTo = null, $rangeInfo = null) {
$userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
$params = array_merge($userDao->getFetchParameters(), [(int) $journalId]);
$result = $this->retrieveRange(
$sql = 'SELECT s.*,
' . $userDao->getFetchColumns() . '
$baseSql = '
FROM subscriptions s
JOIN subscription_types st ON (s.type_id = st.type_id)
JOIN users u ON (s.user_id = u.user_id)
' . $userDao->getFetchJoins() . '
JOIN subscription_types st ON (s.type_id = st.type_id)
JOIN users u ON (s.user_id = u.user_id)
' . $userDao->getFetchJoins() . '
WHERE st.institutional = 0
AND s.journal_id = ? ' .
parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params) . ' ' .
$userDao->getOrderBy() .', s.subscription_id',
parent::_generateSearchSQL($status, $searchField, $searchMatch, $search, $dateField, $dateFrom, $dateTo, $params) . '
';
$result = $this->retrieveRange(
'SELECT s.*, ' . $userDao->getFetchColumns() . $baseSql . $userDao->getOrderBy() . ', s.subscription_id',
$params,
$rangeInfo
);
return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription grid paging
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in subscription grid paging
}

/**
Expand Down
29 changes: 16 additions & 13 deletions classes/subscription/InstitutionalSubscriptionDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,28 @@ function getByJournalId($journalId, $status = null, $searchField = null, $search
break;
}


$result = $this->retrieveRange(
$sql = 'SELECT DISTINCT s.*, iss.institution_name, iss.mailing_address, iss.domain,
' . $userDao->getFetchColumns() .'
FROM subscriptions s
JOIN subscription_types st ON (s.type_id = st.type_id)
JOIN users u ON (s.user_id = u.user_id)
JOIN institutional_subscriptions iss ON (s.subscription_id = iss.subscription_id)
' . $userDao->getFetchJoins() . '
' . $ipRangeSql1 . '
WHERE st.institutional = 1
$baseSql = '
FROM subscriptions s
JOIN subscription_types st ON (s.type_id = st.type_id)
JOIN users u ON (s.user_id = u.user_id)
JOIN institutional_subscriptions iss ON (s.subscription_id = iss.subscription_id)
' . $userDao->getFetchJoins() . '
' . $ipRangeSql1 . '
WHERE st.institutional = 1
' . $ipRangeSql2 . '
AND s.journal_id = ?
' . $searchSql . ' ORDER BY iss.institution_name ASC, s.subscription_id',
' . $searchSql . '
';

$result = $this->retrieveRange(
'SELECT DISTINCT s.*, iss.institution_name, iss.mailing_address, iss.domain, ' . $userDao->getFetchColumns() . "
{$baseSql}
ORDER BY iss.institution_name ASC, s.subscription_id",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription grid paging
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in subscription grid paging
}

/**
Expand Down
7 changes: 5 additions & 2 deletions classes/subscription/SubscriptionTypeDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,14 @@ function deleteByJournal($journalId) {
* @return object DAOResultFactory containing matching SubscriptionTypes
*/
function getByJournalId($journalId, $rangeInfo = null) {
$baseSql = '
FROM subscription_types WHERE journal_id = ?
';
$result = $this->retrieveRange(
$sql = 'SELECT * FROM subscription_types WHERE journal_id = ? ORDER BY seq',
"SELECT * {$baseSql} ORDER BY seq",
$params = [(int) $journalId],
$rangeInfo);
return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo); // Counted in subscription type grid paging
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in subscription type grid paging
}

/**
Expand Down

0 comments on commit 7c61f93

Please sign in to comment.