From 3894af4db2142693dc438c6d708e148f9010823a Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Mon, 3 Jun 2019 22:45:00 +0100 Subject: [PATCH] Optimise database temp tables indexes (#2409) --- application/models/Customer.php | 2 +- application/models/Sale.php | 10 ++++++---- application/models/reports/Summary_payments.php | 6 +++--- application/models/reports/Summary_report.php | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/application/models/Customer.php b/application/models/Customer.php index 983e2c6b5..1532f9913 100644 --- a/application/models/Customer.php +++ b/application/models/Customer.php @@ -100,7 +100,7 @@ class Customer extends Person { // create a temporary table to contain all the sum and average of items $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_temp') . - ' (INDEX(sale_id)) + ' (INDEX(sale_id)) ENGINE=MEMORY ( SELECT sales.sale_id AS sale_id, diff --git a/application/models/Sale.php b/application/models/Sale.php index e649a8ff3..264e96ab3 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -27,7 +27,8 @@ class Sale extends CI_Model // NOTE: temporary tables are created to speed up searches due to the fact that they are ortogonal to the main query // create a temporary table to contain all the payments per sale $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') . - '( + ' (PRIMARY KEY(sale_id), INDEX(sale_id)) + ( SELECT payments.sale_id AS sale_id, IFNULL(SUM(payments.payment_amount), 0) AS sale_payment_amount, GROUP_CONCAT(CONCAT(payments.payment_type, " ", payments.payment_amount) SEPARATOR ", ") AS payment_type @@ -57,7 +58,8 @@ class Sale extends CI_Model // create a temporary table to contain all the sum of taxes per sale item $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . - '( + ' (INDEX(sale_id), INDEX(item_id)) ENGINE=MEMORY + ( SELECT sales_items_taxes.sale_id AS sale_id, sales_items_taxes.item_id AS item_id, sales_items_taxes.line AS line, @@ -172,7 +174,7 @@ class Sale extends CI_Model // create a temporary table to contain all the sum of taxes per sale item $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . - ' (INDEX(sale_id), INDEX(item_id)) + ' (INDEX(sale_id), INDEX(item_id)) ENGINE=MEMORY ( SELECT sales_items_taxes.sale_id AS sale_id, sales_items_taxes.item_id AS item_id, @@ -1113,7 +1115,7 @@ class Sale extends CI_Model // create a temporary table to contain all the sum of taxes per sale item $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . - ' (INDEX(sale_id), INDEX(item_id)) + ' (INDEX(sale_id), INDEX(item_id)) ENGINE=MEMORY ( SELECT sales_items_taxes.sale_id AS sale_id, sales_items_taxes.item_id AS item_id, diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index 7c160e76b..2e9c7eb3c 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -137,7 +137,7 @@ class Summary_payments extends Summary_report $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sumpay_taxes_temp') . - ' (INDEX(sale_id)) + ' (INDEX(sale_id)) ENGINE=MEMORY ( SELECT sales.sale_id, SUM(sales_taxes.sale_tax_amount) AS total_taxes FROM ' . $this->db->dbprefix('sales') . ' AS sales @@ -149,7 +149,7 @@ class Summary_payments extends Summary_report ); $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sumpay_items_temp') . - ' (INDEX(sale_id)) + ' (INDEX(sale_id)) ENGINE=MEMORY ( SELECT sales.sale_id, '. $trans_amount . ' FROM ' . $this->db->dbprefix('sales') . ' AS sales ' @@ -166,7 +166,7 @@ class Summary_payments extends Summary_report . ' AS sumpay_taxes WHERE sumpay_items.sale_id = sumpay_taxes.sale_id),0)'); $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sumpay_payments_temp') . - ' (INDEX(sale_id)) + ' (INDEX(sale_id)) ENGINE=MEMORY ( SELECT sales.sale_id, COUNT(sales.sale_id) AS number_payments, SUM(sales_payments.payment_amount) AS total_payments FROM ' . $this->db->dbprefix('sales') . ' AS sales diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php index 0915ef3fd..870024c02 100644 --- a/application/models/reports/Summary_report.php +++ b/application/models/reports/Summary_report.php @@ -40,7 +40,7 @@ abstract class Summary_report extends Report // create a temporary table to contain all the sum of taxes per sale item $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . - ' (INDEX(sale_id), INDEX(item_id)) + ' (INDEX(sale_id), INDEX(item_id)) ENGINE=MEMORY ( SELECT sales_items_taxes.sale_id AS sale_id, sales_items_taxes.item_id AS item_id,