From 0cfd16d8db7dc1f233ff6977137fd92d807c52c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Sat, 8 Jun 2024 19:35:28 +0200 Subject: [PATCH] Allow tweaking of the index that MySQL use when searching products (#29934) * Allow tweaking of the index that MySQL use when searching products Sometimes when working with large dataset with many entities the MySQL engine does not pick-up the right index. This is especially true when doing a search over the reference because the LIKE '%%' prevent the uk_product_label from being chosen. This PR aims to improve this situation by doing the following : - Adding a simple index on entity column. - Adding an hidden constant to force the index being used when searching products. Performances over a 8M products dataset shows a reduction of 2/3 execution time. * Add missing global $conf * Fix wrong usage of getDolGlobalString() * Finalise fix * Add more indexes * Rework indexes --- htdocs/core/class/html.form.class.php | 5 +++++ htdocs/install/mysql/migration/19.0.0-20.0.0.sql | 2 ++ htdocs/install/mysql/tables/llx_product.key.sql | 1 + 3 files changed, 8 insertions(+) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 3bfdb67b65b..5c49edc0848 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2961,6 +2961,11 @@ class Form } $sql .= " FROM ".$this->db->prefix()."product as p"; + + if (getDolGlobalString('MAIN_SEARCH_PRODUCT_FORCE_INDEX')) { + $sql .= " USE INDEX (" . $this->db->sanitize(getDolGlobalString('MAIN_PRODUCT_FORCE_INDEX')) . ")"; + } + // Add from (left join) from hooks $parameters = array(); $reshook = $hookmanager->executeHooks('selectProductsListFrom', $parameters); // Note that $action and $object may have been modified by hook diff --git a/htdocs/install/mysql/migration/19.0.0-20.0.0.sql b/htdocs/install/mysql/migration/19.0.0-20.0.0.sql index 2e0a154641a..72e23e0398c 100644 --- a/htdocs/install/mysql/migration/19.0.0-20.0.0.sql +++ b/htdocs/install/mysql/migration/19.0.0-20.0.0.sql @@ -388,3 +388,5 @@ ALTER TABLE llx_chargesociales ADD INDEX idx_chargesociales_fk_user (fk_user); -- table paiementcharge indexes ALTER TABLE llx_paiementcharge ADD INDEX idx_paiementcharge_fk_charge (fk_charge); + +ALTER TABLE llx_product ADD INDEX idx_product_entity_fk_product_type (entity, fk_product_type); diff --git a/htdocs/install/mysql/tables/llx_product.key.sql b/htdocs/install/mysql/tables/llx_product.key.sql index c0526a8f2cc..29cf6eff240 100644 --- a/htdocs/install/mysql/tables/llx_product.key.sql +++ b/htdocs/install/mysql/tables/llx_product.key.sql @@ -31,6 +31,7 @@ ALTER TABLE llx_product ADD INDEX idx_product_fk_country (fk_country); ALTER TABLE llx_product ADD INDEX idx_product_fk_user_author (fk_user_author); ALTER TABLE llx_product ADD INDEX idx_product_fk_barcode_type (fk_barcode_type); ALTER TABLE llx_product ADD INDEX idx_product_fk_project (fk_project); +ALTER TABLE llx_product ADD INDEX idx_product_entity_fk_product_type (entity, fk_product_type); ALTER TABLE llx_product ADD UNIQUE INDEX uk_product_barcode (barcode, fk_barcode_type, entity); ALTER TABLE llx_product ADD CONSTRAINT fk_product_fk_unit FOREIGN KEY (fk_unit) REFERENCES llx_c_units (rowid); ALTER TABLE llx_product ADD CONSTRAINT fk_product_finished FOREIGN KEY (finished) REFERENCES llx_c_product_nature (code);