Skip to content

Commit

Permalink
Merge pull request #854 from plural/remove-sql-calc-found-rows
Browse files Browse the repository at this point in the history
Remove sql calc found rows & optimize decklist search
  • Loading branch information
plural authored Oct 10, 2024
2 parents c70fa4b + 563fe4f commit 321ae1c
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 234 deletions.
2 changes: 2 additions & 0 deletions src/AppBundle/Controller/DecklistsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ public function listAction(string $type, int $page = 1, Request $request, Entity
$this->denyAccessUnlessGranted('ROLE_MODERATOR');
$result = $decklistManager->trashed($start, $limit);
$pagetitle = "Trashed decklists";
$pagedescription = '';
break;
case 'restored':
$this->denyAccessUnlessGranted('ROLE_MODERATOR');
$result = $decklistManager->restored($start, $limit);
$pagetitle = "Restored decklists";
$pagedescription = '';
break;
case 'popular':
default:
Expand Down
50 changes: 29 additions & 21 deletions src/AppBundle/Controller/SocialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,20 @@ public function unfollowAction(User $following, Request $request, EntityManagerI
]));
}

private function getLimitedQueryRowsWithCounts(EntityManagerInterface $entityManager, string $baseQuery, int $start, int $limit, array $params) {

$dbh = $entityManager->getConnection();

$rows = $dbh->executeQuery("$baseQuery LIMIT $start, $limit", $params)->fetchAll(\PDO::FETCH_ASSOC);

$count = $dbh->executeQuery("SELECT COUNT(*) FROM ($baseQuery) AS t", $params)->fetch(\PDO::FETCH_NUM)[0];

return [
"rows" => $rows,
"count" => $count
];
}

/**
* @param int $page
* @param Request $request
Expand All @@ -1136,10 +1150,9 @@ public function usercommentsAction(int $page, Request $request, EntityManagerInt
}
$start = ($page - 1) * $limit;

$dbh = $entityManager->getConnection();

$comments = $dbh->executeQuery(
"SELECT SQL_CALC_FOUND_ROWS
$results = $this->getLimitedQueryRowsWithCounts(
$entityManager,
"SELECT
c.id,
c.text,
c.date_creation,
Expand All @@ -1150,15 +1163,10 @@ public function usercommentsAction(int $page, Request $request, EntityManagerInt
from comment c
join decklist d on c.decklist_id=d.id
where c.user_id=?
order by date_creation desc
limit $start, $limit",
[
$user->getId(),
]
)
->fetchAll(\PDO::FETCH_ASSOC);

$maxcount = $dbh->executeQuery("SELECT FOUND_ROWS()")->fetch(\PDO::FETCH_NUM)[0];
order by date_creation desc",
$start, $limit, [ $user->getId() ] );
$comments = $results['rows'];
$maxcount = $results['count'];

// pagination : calcul de nbpages // currpage // prevpage // nextpage
// à partir de $start, $limit, $count, $maxcount, $page
Expand Down Expand Up @@ -1218,8 +1226,9 @@ public function commentsAction(int $page, Request $request, EntityManagerInterfa

$dbh = $entityManager->getConnection();

$comments = $dbh->executeQuery(
"SELECT SQL_CALC_FOUND_ROWS
$results = $this->getLimitedQueryRowsWithCounts(
$entityManager,
"SELECT
c.id,
c.text,
c.date_creation,
Expand All @@ -1232,12 +1241,11 @@ public function commentsAction(int $page, Request $request, EntityManagerInterfa
from comment c
join decklist d on c.decklist_id=d.id
join user u on c.user_id=u.id
order by date_creation desc
limit $start, $limit",
[]
)->fetchAll(\PDO::FETCH_ASSOC);

$maxcount = $dbh->executeQuery("SELECT FOUND_ROWS()")->fetch(\PDO::FETCH_NUM)[0];
order by date_creation desc",
$start, $limit,
[]);
$comments = $results['rows'];
$maxcount = $results['count'];

// pagination : calcul de nbpages // currpage // prevpage // nextpage
// à partir de $start, $limit, $count, $maxcount, $page
Expand Down
Loading

0 comments on commit 321ae1c

Please sign in to comment.