From 2795e6255ca8e9902b06ccf40160cbbe3abdb516 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Wed, 8 Apr 2026 19:49:51 +0530 Subject: [PATCH] Add medicine search DB indexes and prefix query optimization --- app/Controllers/Opd_prescription.php | 4 +- ...-04-08-000041_AddMedicineSearchIndexes.php | 84 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 app/Database/Migrations/2026-04-08-000041_AddMedicineSearchIndexes.php diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index fe71e03..1a87958 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -4519,9 +4519,9 @@ class Opd_prescription extends BaseController $builder = $this->db->table($table)->select($select); if ($q !== '') { $builder->groupStart() - ->like($nameField, $q); + ->like($nameField, $q, 'after'); if ($formField !== null) { - $builder->orLike($formField, $q); + $builder->orLike($formField, $q, 'after'); } $builder->groupEnd(); } diff --git a/app/Database/Migrations/2026-04-08-000041_AddMedicineSearchIndexes.php b/app/Database/Migrations/2026-04-08-000041_AddMedicineSearchIndexes.php new file mode 100644 index 0000000..f22c0dc --- /dev/null +++ b/app/Database/Migrations/2026-04-08-000041_AddMedicineSearchIndexes.php @@ -0,0 +1,84 @@ +db->query('SHOW TABLES LIKE ?', [$table])->getResultArray(); + + return ! empty($rows); + } + + private function fieldExists(string $table, string $field): bool + { + if (! $this->tableExists($table)) { + return false; + } + + $rows = $this->db->query('SHOW COLUMNS FROM `' . $table . '` LIKE ?', [$field])->getResultArray(); + + return ! empty($rows); + } + + private function indexExists(string $table, string $indexName): bool + { + if (! $this->tableExists($table)) { + return false; + } + + $rows = $this->db->query('SHOW INDEX FROM `' . $table . '` WHERE Key_name = ?', [$indexName])->getResultArray(); + + return ! empty($rows); + } + + private function addIndexIfMissing(string $table, string $indexName, string $columns): void + { + if (! $this->tableExists($table) || $this->indexExists($table, $indexName)) { + return; + } + + $this->db->query('ALTER TABLE `' . $table . '` ADD INDEX `' . $indexName . '` (' . $columns . ')'); + } + + public function up(): void + { + if (! $this->tableExists('opd_med_master')) { + return; + } + + if ($this->fieldExists('opd_med_master', 'item_name')) { + $this->addIndexIfMissing('opd_med_master', 'idx_opd_med_master_item_name', '`item_name`'); + } + + if ($this->fieldExists('opd_med_master', 'formulation')) { + $this->addIndexIfMissing('opd_med_master', 'idx_opd_med_master_formulation', '`formulation`'); + } + + if ($this->fieldExists('opd_med_master', 'item_name') && $this->fieldExists('opd_med_master', 'formulation')) { + $this->addIndexIfMissing('opd_med_master', 'idx_opd_med_master_item_form', '`item_name`,`formulation`'); + } + } + + public function down(): void + { + if (! $this->tableExists('opd_med_master')) { + return; + } + + $indexes = [ + 'idx_opd_med_master_item_name', + 'idx_opd_med_master_formulation', + 'idx_opd_med_master_item_form', + ]; + + foreach ($indexes as $index) { + if ($this->indexExists('opd_med_master', $index)) { + $this->db->query('ALTER TABLE `opd_med_master` DROP INDEX `' . $index . '`'); + } + } + } +}