Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature main 8700 improve slow query #9997

Merged
merged 13 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/v1/_submissions/PKPBackendSubmissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$genres = $genreDao->getByContextId($context->getId())->toArray();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::submission()->getSchemaMap()->mapManyToSubmissionsList($submissions, $userGroups, $genres)->values(),
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -259,7 +259,7 @@ public function assigned(Request $illuminateRequest): JsonResponse
$genres = $genreDao->getByContextId($context->getId())->toArray();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::submission()->getSchemaMap()->mapManyToSubmissionsList($submissions, $userGroups, $genres)->values(),
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -326,7 +326,7 @@ public function reviews(Request $illuminateRequest): JsonResponse
$genres = $genreDao->getByContextId($context->getId())->toArray();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::submission()->getSchemaMap()->mapManyToSubmissionsList($submissions, $userGroups, $genres)->values(),
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -384,7 +384,7 @@ public function getReviewAssignments(Request $illuminateRequest): JsonResponse
$reviewAssignments = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::reviewAssignment()->getSchemaMap()->mapMany($reviewAssignments)->values(),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/announcements/PKPAnnouncementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$announcements = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::announcement()->getSchemaMap()->summarizeMany($announcements)->values(),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/dois/PKPDoiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$dois = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(0)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::doi()->getSchemaMap()->summarizeMany($dois)->values(),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/emailTemplates/PKPEmailTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$emailTemplates = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::emailTemplate()->getSchemaMap()->summarizeMany($emailTemplates),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/highlights/HighlightsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$highlights = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::highlight()->getSchemaMap()->summarizeMany($highlights)->values(),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/institutions/PKPInstitutionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$institutions = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::institution()->getSchemaMap()->summarizeMany($institutions->values())->values(),
], Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/sections/SectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$sections = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::section()->getSchemaMap()->summarizeMany($sections)->values(),
], Response::HTTP_OK);
}
Expand Down
6 changes: 3 additions & 3 deletions api/v1/submissions/PKPSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function getMany(Request $illuminateRequest): JsonResponse
$genres = $genreDao->getByContextId($context->getId())->toArray();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::submission()->getSchemaMap()->summarizeMany($submissions, $userGroups, $genres)->values(),
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -900,7 +900,7 @@ public function getPublications(Request $illuminateRequest): JsonResponse
$genres = $genreDao->getByContextId($submission->getData('contextId'))->toArray();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::publication()->getSchemaMap($submission, $userGroups, $genres)->summarizeMany($publications, $anonymize)->values(),
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -1357,7 +1357,7 @@ public function getContributors(Request $illuminateRequest): JsonResponse
$authors = $collector->getMany();

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => Repo::author()->getSchemaMap()->summarizeMany($authors)->values(),
], Response::HTTP_OK);
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1/users/PKPUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getMany(Request $request): JsonResponse
}

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => $items,
], Response::HTTP_OK);
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public function getReviewers(Request $request): JsonResponse
}

return response()->json([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
'itemsMax' => $collector->getCount(),
'items' => $items,
], Response::HTTP_OK);
}
Expand Down
3 changes: 1 addition & 2 deletions classes/announcement/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->get('a.' . $this->primaryKeyColumn)
->count();
->getCountForPagination();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/author/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->count();
->getCountForPagination();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/category/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function newDataObject(): Category
*/
public function getCount(Collector $query): int
{
return $query->getQueryBuilder()->count();
return $query->getQueryBuilder()->getCountForPagination();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function getItems($request)
*/
public function getItemsMax()
{
return $this->_getCollector()->offset(null)->limit(null)->getCount();
return $this->_getCollector()->getCount();
}

/**
Expand Down
23 changes: 16 additions & 7 deletions classes/db/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace PKP\db;

use Generator;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
use PKP\cache\CacheManager;
use PKP\core\JSONMessage;
Expand Down Expand Up @@ -80,8 +81,8 @@ public function retrieve($sql, $params = [], $callHooks = true)
/**
* Execute a SELECT SQL statement, returning rows in the range supplied.
*
* @param string $sql the SQL statement
* @param array $params parameters for the SQL statement
* @param string|Builder $sql the SQL statement
* @param array $params parameters for the SQL statement, params is used only when $sql is a string
* @param DBResultRange $dbResultRange object describing the desired range
*
* @deprecated 3.4
Expand All @@ -102,27 +103,35 @@ public function retrieveRange($sql, $params = [], $dbResultRange = null, $callHo
}

if ($dbResultRange && $dbResultRange->isValid()) {
$sql .= ' LIMIT ' . (int) $dbResultRange->getCount();
$limit = (int) $dbResultRange->getCount();
$offset = (int) $dbResultRange->getOffset();
$offset += max(0, $dbResultRange->getPage() - 1) * (int) $dbResultRange->getCount();
$sql .= ' OFFSET ' . $offset;
if ($sql instanceof Builder) {
$sql->limit($limit)->offset($offset);
} else {
$sql .= " LIMIT {$limit} OFFSET {$offset}";
}
}

return DB::cursor(DB::raw($sql)->getValue(DB::connection()->getQueryGrammar()), $params);
return $sql instanceof Builder ? $sql->get() : DB::cursor(DB::raw($sql)->getValue(DB::connection()->getQueryGrammar()), $params);
}

/**
* Count the number of records in the supplied SQL statement (with optional bind parameters parameters)
*
* @param string $sql SQL query to be counted
* @param array $params Optional SQL query bind parameters
* @param string|Builder $sql SQL query to be counted
* @param array $params Optional SQL query bind parameters, only used when the $sql argument is a string
*
* @deprecated 3.4
*
* @return int
*/
public function countRecords($sql, $params = [])
{
// In case a Laravel Builder has been received, drop its SELECT and ORDER BY clauses for optimization purposes
if ($sql instanceof Builder) {
return $sql->getCountForPagination();
}
$result = $this->retrieve('SELECT COUNT(*) AS row_count FROM (' . $sql . ') AS count_subquery', $params);
return $result->current()->row_count;
}
Expand Down
13 changes: 7 additions & 6 deletions classes/db/DAOResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace PKP\db;

use APP\submission\DAO;
use Countable;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use PKP\core\ItemIterator;
Expand Down Expand Up @@ -46,7 +48,7 @@ class DAOResultFactory extends ItemIterator
public $records;

/**
* @var string|null Fetch SQL
* @var string|Builder|null Fetch SQL
*/
public $sql;

Expand Down Expand Up @@ -76,8 +78,8 @@ class DAOResultFactory extends ItemIterator
* @param object $dao DAO class for factory
* @param string $functionName The function to call on $dao to create an object
* @param array $idFields an array of primary key field names that uniquely identify a result row in the record set. Should be data object _data array key, not database column name
* @param string $sql Optional SQL query used to generate paged result set. Necessary when total row counts will be needed (e.g. when paging). WARNING: New code should not use this.
* @param array $params Optional parameters for SQL query used to generate paged result set. Necessary when total row counts will be needed (e.g. when paging). WARNING: New code should not use this.
* @param string|Builder|null $sql Optional SQL query used to generate paged result set. Necessary when total row counts will be needed (e.g. when paging). WARNING: New code should not use this.
* @param array $params Optional parameters for SQL query used to generate paged result set. Necessary when total row counts will be needed (e.g. when paging), only used when the $sql argument is a string. WARNING: New code should not use this.
* @param ?DBResultRange $rangeInfo Optional pagination information. WARNING: New code should not use this.
*/
public function __construct($records, $dao, $functionName, $idFields = [], $sql = null, $params = [], $rangeInfo = null)
Expand Down Expand Up @@ -207,9 +209,8 @@ public function eof()
if ($this->records == null) {
return true;
}
/** @var DAOResultIterator */
$records = $this->records;
return !$records->valid();

return $this->records instanceof Countable ? !count($this->records) : !$this->records->valid();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/decision/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->count();
->getCountForPagination();
}

/**
Expand Down
4 changes: 1 addition & 3 deletions classes/doi/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->select('d.' . $this->primaryKeyColumn)
->get()
->count();
->getCountForPagination();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/doi/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public function isAssigned(int $doiId, string $pubObjectType): bool
Repo::doi()::TYPE_PUBLICATION => Repo::publication()
->getCollector()
->filterByDoiIds([$doiId])
->getIds()
->count(),
->getQueryBuilder()
->getCountForPagination() > 0,
default => false,
};
}
Expand Down
3 changes: 1 addition & 2 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->get()
->count();
->getCountForPagination();
}

/**
Expand Down
Loading