diff --git a/htdocs/comm/action/list.php b/htdocs/comm/action/list.php
index 7d1eff2586c..9aebf9d3ed9 100644
--- a/htdocs/comm/action/list.php
+++ b/htdocs/comm/action/list.php
@@ -517,19 +517,23 @@ $parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
-$sql .= $db->order($sortfield, $sortorder);
-
-$nbtotalofrecords = ''; // TODO We can set and use an optimized request in $sqlforcount with no fields and no useless join to calculate nb of records
+// Count total nb of records
+$nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
- /* This old method to get and count full list returns all record so use a high amount of memory.
+ /* This old and fast method to get and count full list returns all record so use a high amount of memory.
$resql = $db->query($sql);
$nbtotalofrecords = $db->num_rows($resql);
*/
- /* The new method does not consume memory on mysql (not tested on pgsql) */
- $resql = $db->query($sql, 0, 'auto', 1);
+ /* The slow method does not consume memory on mysql (not tested on pgsql) */
+ /*$resql = $db->query($sql, 0, 'auto', 1);
while ($db->fetch_object($resql)) {
$nbtotalofrecords++;
- }
+ }*/
+ /* This fast and low memory method to get and count full list converts the sql into a sql count */
+ $sqlforcount = preg_replace('/^SELECT[a-z0-9\._\s\(\),]+FROM/i', 'SELECT COUNT(*) as nbtotalofrecords FROM', $sql);
+ $resql = $db->query($sqlforcount);
+ $objforcount = $db->fetch_object($resql);
+ $nbtotalofrecords = $objforcount->nbtotalofrecords;
if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
$page = 0;
$offset = 0;
@@ -537,527 +541,519 @@ if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
$db->free($resql);
}
-// if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
-if (is_numeric($nbtotalofrecords) && ($limit > $nbtotalofrecords || empty($limit))) {
- $num = $nbtotalofrecords;
-} else {
- if ($limit) {
- $sql .= $db->plimit($limit + 1, $offset);
- }
-
- $resql = $db->query($sql);
- if (!$resql) {
- dol_print_error($db);
- exit;
- }
-
- $num = $db->num_rows($resql);
+// Complete request and execute it with limit
+$sql .= $db->order($sortfield, $sortorder);
+if ($limit) {
+ $sql .= $db->plimit($limit + 1, $offset);
}
-dol_syslog("comm/action/list.php", LOG_DEBUG);
$resql = $db->query($sql);
-if ($resql) {
- $actionstatic = new ActionComm($db);
- $societestatic = new Societe($db);
+if (!$resql) {
+ dol_print_error($db);
+ exit;
+}
- $num = $db->num_rows($resql);
+$num = $db->num_rows($resql);
- $arrayofselected = is_array($toselect) ? $toselect : array();
- // Local calendar
- $newtitle = '
';
- //$newtitle=$langs->trans($title);
+$actionstatic = new ActionComm($db);
+$societestatic = new Societe($db);
- $tabactive = 'cardlist';
+$num = $db->num_rows($resql);
- $head = calendars_prepare_head($param);
+$arrayofselected = is_array($toselect) ? $toselect : array();
- print '';
+
+$db->free($resql);
// End of page
llxFooter();
diff --git a/htdocs/modulebuilder/template/myobject_list.php b/htdocs/modulebuilder/template/myobject_list.php
index 259b4570615..d087bf272db 100644
--- a/htdocs/modulebuilder/template/myobject_list.php
+++ b/htdocs/modulebuilder/template/myobject_list.php
@@ -344,43 +344,45 @@ $sql .= $hookmanager->resPrint;
$sql = preg_replace('/,\s*$/', '', $sql);
*/
-$sql .= $db->order($sortfield, $sortorder);
-
// Count total nb of records
$nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
- /* This old method to get and count full list returns all record so use a high amount of memory.
+ /* This old and fast method to get and count full list returns all record so use a high amount of memory.
$resql = $db->query($sql);
$nbtotalofrecords = $db->num_rows($resql);
*/
- /* The new method does not consume memory on mysql (not tested on pgsql) */
- $resql = $db->query($sql, 0, 'auto', 1);
+ /* The slow method does not consume memory on mysql (not tested on pgsql) */
+ /*$resql = $db->query($sql, 0, 'auto', 1);
while ($db->fetch_object($resql)) {
$nbtotalofrecords++;
- }
+ }*/
+ /* This fast and low memory method to get and count full list convert the sql into a sql count */
+ $sqlforcount = preg_replace('/^SELECT[a-z0-9\._\s\(\),]+FROM/i', 'SELECT COUNT(*) as nbtotalofrecords FROM', $sql);
+ $resql = $db->query($sqlforcount);
+ $objforcount = $db->fetch_object($resql);
+ $nbtotalofrecords = $objforcount->nbtotalofrecords;
if (($page * $limit) > $nbtotalofrecords) { // if total of record found is smaller than page * limit, goto and load page 0
$page = 0;
$offset = 0;
}
$db->free($resql);
}
-// if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
-if (is_numeric($nbtotalofrecords) && ($limit > $nbtotalofrecords || empty($limit))) {
- $num = $nbtotalofrecords;
-} else {
- if ($limit) {
- $sql .= $db->plimit($limit + 1, $offset);
- }
- $resql = $db->query($sql);
- if (!$resql) {
- dol_print_error($db);
- exit;
- }
-
- $num = $db->num_rows($resql);
+// Complete request and execute it with limit
+$sql .= $db->order($sortfield, $sortorder);
+if ($limit) {
+ $sql .= $db->plimit($limit + 1, $offset);
}
+$resql = $db->query($sql);
+if (!$resql) {
+ dol_print_error($db);
+ exit;
+}
+
+$num = $db->num_rows($resql);
+
+
// Direct jump if only one record found
if ($num == 1 && !empty($conf->global->MAIN_SEARCH_DIRECT_OPEN_IF_ONLY_ONE) && $search_all && !$page) {
$obj = $db->fetch_object($resql);