NEW Add method hintindex() in database handlers.

This commit is contained in:
Laurent Destailleur 2022-04-06 17:32:56 +02:00
parent a528cd46bd
commit 091ca31b6c
3 changed files with 44 additions and 11 deletions

View File

@ -598,6 +598,9 @@ if (!empty($search_categ_cus) && $search_categ_cus != '-1') {
}
$sql .= ', '.MAIN_DB_PREFIX.'facture as f';
if ($sortfield == "f.datef") {
$sql .= $db->hintindex('idx_facture_datef');
}
if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (f.rowid = ef.fk_object)";
}
@ -846,14 +849,6 @@ $parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListHaving', $parameters, $object); // Note that $action and $object may have been modified by hook
$sql .= empty($hookmanager->resPrint) ? "" : " HAVING 1=1 ".$hookmanager->resPrint;
$sql .= ' ORDER BY ';
$listfield = explode(',', $sortfield);
$listorder = explode(',', $sortorder);
foreach ($listfield as $key => $value) {
$sql .= $listfield[$key].' '.($listorder[$key] ? $listorder[$key] : 'DESC').',';
}
$sql .= ' f.rowid DESC ';
$nbtotalofrecords = '';
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
/* This old and fast method to get and count full list returns all record so use a high amount of memory.
@ -870,8 +865,12 @@ if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
$sqlforcount = preg_replace('/GROUP BY.*$/', '', $sqlforcount);
$resql = $db->query($sqlforcount);
$objforcount = $db->fetch_object($resql);
$nbtotalofrecords = $objforcount->nbtotalofrecords;
if ($resql) {
$objforcount = $db->fetch_object($resql);
$nbtotalofrecords = $objforcount->nbtotalofrecords;
} else {
dol_print_error($db);
}
if (($page * $limit) > $nbtotalofrecords) { // if total of record found is smaller than page * limit, goto and load page 0
$page = 0;
@ -880,7 +879,17 @@ if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
$db->free($resql);
}
$sql .= $db->plimit($limit + 1, $offset);
// Complete request and execute it with limit
$sql .= ' ORDER BY ';
$listfield = explode(',', $sortfield);
$listorder = explode(',', $sortorder);
foreach ($listfield as $key => $value) {
$sql .= $listfield[$key].' '.($listorder[$key] ? $listorder[$key] : 'DESC').',';
}
$sql .= ' f.rowid DESC ';
if ($limit) {
$sql .= $db->plimit($limit + 1, $offset);
}
$resql = $db->query($sql);

View File

@ -96,6 +96,17 @@ abstract class DoliDB implements Database
return '(CASE WHEN '.$test.' THEN '.$resok.' ELSE '.$resko.' END)';
}
/**
* Return SQL string to force an index
*
* @param string $nameofindex Name of index
* @return string SQL string
*/
public function hintindex($nameofindex)
{
return '';
}
/**
* Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
* Function to use to build INSERT, UPDATE or WHERE predica

View File

@ -168,6 +168,18 @@ class DoliDBMysqli extends DoliDB
}
/**
* Return SQL string to force an index
*
* @param string $nameofindex Name of index
* @return string SQL string
*/
public function hintindex($nameofindex)
{
return " FORCE INDEX(".preg_replace('/[^a-z0-9_]/', '', $nameofindex).")";
}
/**
* Convert a SQL request in Mysql syntax to native syntax
*
@ -180,6 +192,7 @@ class DoliDBMysqli extends DoliDB
return $line;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Select a database