Improve get_found_rows query performance (#1940)

This commit is contained in:
FrancescoUK
2018-04-12 22:42:56 +01:00
parent 8dc7855e56
commit 075d4e1aeb
9 changed files with 312 additions and 234 deletions

View File

@@ -322,28 +322,21 @@ class Customer extends Person
*/
public function get_found_rows($search)
{
$this->db->from('customers');
$this->db->join('people', 'customers.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('email', $search);
$this->db->or_like('phone_number', $search);
$this->db->or_like('account_number', $search);
$this->db->or_like('company_name', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
return $this->search($search, 0, 0, 'last_name', 'asc', TRUE);
}
/*
Performs a search on customers
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc')
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc', $count_only = FALSE)
{
$this->db->from('customers');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(customers.person_id) as count');
}
$this->db->from('customers AS customers');
$this->db->join('people', 'customers.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
@@ -355,14 +348,23 @@ class Customer extends Person
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
}
?>

View File

@@ -259,27 +259,21 @@ class Employee extends Person
*/
public function get_found_rows($search)
{
$this->db->from('employees');
$this->db->join('people', 'employees.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('email', $search);
$this->db->or_like('phone_number', $search);
$this->db->or_like('username', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
return $this->search($search, 0, 0, 'last_name', 'asc', TRUE);
}
/*
Performs a search on employees
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc')
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc', $count_only = FALSE)
{
$this->db->from('employees');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(employees.person_id) as count');
}
$this->db->from('employees AS employees');
$this->db->join('people', 'employees.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
@@ -290,14 +284,23 @@ class Employee extends Person
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
/*

View File

@@ -53,27 +53,36 @@ class Expense extends CI_Model
*/
public function get_found_rows($search, $filters)
{
return $this->search($search, $filters)->num_rows();
return $this->search($search, $filters, 0, 0, 'expense_id', 'asc', TRUE);
}
/*
Searches expenses
*/
public function search($search, $filters, $rows = 0, $limit = 0, $sort = 'expense_id', $order = 'asc')
public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'expense_id', $order = 'asc', $count_only = FALSE)
{
$this->db->select('
expenses.expense_id,
MAX(expenses.date) AS date,
MAX(expenses.supplier_name) AS supplier_name,
MAX(expenses.supplier_tax_code) AS supplier_tax_code,
MAX(expenses.amount) AS amount,
MAX(expenses.tax_amount) AS tax_amount,
MAX(expenses.payment_type) AS payment_type,
MAX(expenses.description) AS description,
MAX(employees.first_name) AS first_name,
MAX(employees.last_name) AS last_name,
MAX(expense_categories.category_name) AS category_name
');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(expenses.expense_id) as count');
}
else
{
$this->db->select('
expenses.expense_id,
MAX(expenses.date) AS date,
MAX(expenses.supplier_name) AS supplier_name,
MAX(expenses.supplier_tax_code) AS supplier_tax_code,
MAX(expenses.amount) AS amount,
MAX(expenses.tax_amount) AS tax_amount,
MAX(expenses.payment_type) AS payment_type,
MAX(expenses.description) AS description,
MAX(employees.first_name) AS first_name,
MAX(employees.last_name) AS last_name,
MAX(expense_categories.category_name) AS category_name
');
}
$this->db->from('expenses AS expenses');
$this->db->join('people AS employees', 'employees.person_id = expenses.employee_id', 'LEFT');
$this->db->join('expense_categories AS expense_categories', 'expense_categories.expense_category_id = expenses.expense_category_id', 'LEFT');
@@ -128,14 +137,23 @@ class Expense extends CI_Model
}
$this->db->group_by('expense_id');
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit);
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
/*

View File

@@ -123,37 +123,47 @@ class Expense_category extends CI_Model
}
/*
Perform a search on expense_category
Gets rows
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'category_name', $order='asc')
{
$this->db->from('expense_categories');
$this->db->group_start();
$this->db->like('category_name', $search);
$this->db->or_like('category_description', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
public function get_found_rows($search)
{
$this->db->from('expense_categories');
return $this->search($search, 0, 0, 'category_name', 'asc', TRUE);
}
/*
Perform a search on expense_category
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'category_name', $order='asc', $count_only = FALSE)
{
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(expense_categories.expense_category_id) as count');
}
$this->db->from('expense_categories AS expense_categories');
$this->db->group_start();
$this->db->like('category_name', $search);
$this->db->or_like('category_description', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
// get_found_rows case
if($count_only == TRUE)
{
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
}
?>

View File

@@ -198,48 +198,52 @@ class Giftcard extends CI_Model
return $suggestions;
}
/*
Performs a search on giftcards
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'giftcard_number', $order = 'asc')
{
$this->db->from('giftcards');
$this->db->join('people', 'giftcards.person_id = people.person_id', 'left');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->or_like('giftcard_number', $search);
$this->db->or_like('giftcards.person_id', $search);
$this->db->group_end();
$this->db->where('giftcards.deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
/*
Gets gift cards
*/
public function get_found_rows($search)
{
$this->db->from('giftcards');
$this->db->join('people', 'giftcards.person_id = people.person_id', 'left');
return $this->search($search, 0, 0, 'giftcard_number', 'asc', TRUE);
}
/*
Performs a search on giftcards
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'giftcard_number', $order = 'asc', $count_only = FALSE)
{
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(giftcards.giftcard_id) as count');
}
$this->db->from('giftcards AS giftcards');
$this->db->join('people AS person', 'giftcards.person_id = person.person_id', 'left');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->or_like('giftcard_number', $search);
$this->db->like('person.first_name', $search);
$this->db->or_like('person.last_name', $search);
$this->db->or_like('CONCAT(person.first_name, " ", person.last_name)', $search);
$this->db->or_like('giftcards.giftcard_number', $search);
$this->db->or_like('giftcards.person_id', $search);
$this->db->group_end();
$this->db->where('giftcards.deleted', 0);
return $this->db->get()->num_rows();
// get_found_rows case
if($count_only == TRUE)
{
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
/*

View File

@@ -97,58 +97,66 @@ class Item extends CI_Model
*/
public function get_found_rows($search, $filters)
{
return $this->search($search, $filters)->num_rows();
return $this->search($search, $filters, 0, 0, 'items.name', 'asc', TRUE);
}
/*
Perform a search on items
*/
public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'items.name', $order = 'asc')
public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'items.name', $order = 'asc', $count_only = FALSE)
{
$this->db->select('MAX(items.name) as name');
$this->db->select('MAX(items.category) as category');
$this->db->select('MAX(items.supplier_id) as supplier_id');
$this->db->select('MAX(items.item_number) as item_number');
$this->db->select('MAX(items.description) as description');
$this->db->select('MAX(items.cost_price) as cost_price');
$this->db->select('MAX(items.unit_price) as unit_price');
$this->db->select('MAX(items.reorder_level) as reorder_level');
$this->db->select('MAX(items.receiving_quantity) as receiving_quantity');
$this->db->select('items.item_id as item_id');
$this->db->select('MAX(items.pic_filename) as pic_filename');
$this->db->select('MAX(items.allow_alt_description) as allow_alt_description');
$this->db->select('MAX(items.is_serialized) as is_serialized');
$this->db->select('MAX(items.deleted) as deleted');
$this->db->select('MAX(items.custom1) as custom1');
$this->db->select('MAX(items.custom2) as custom2');
$this->db->select('MAX(items.custom3) as custom3');
$this->db->select('MAX(items.custom4) as custom4');
$this->db->select('MAX(items.custom5) as custom5');
$this->db->select('MAX(items.custom6) as custom6');
$this->db->select('MAX(items.custom7) as custom7');
$this->db->select('MAX(items.custom8) as custom8');
$this->db->select('MAX(items.custom9) as custom9');
$this->db->select('MAX(items.custom10) as custom10');
$this->db->select('MAX(suppliers.person_id) as person_id');
$this->db->select('MAX(suppliers.company_name) as company_name');
$this->db->select('MAX(suppliers.agency_name) as agency_name');
$this->db->select('MAX(suppliers.account_number) as account_number');
$this->db->select('MAX(suppliers.deleted) as deleted');
$this->db->select('MAX(inventory.trans_id) as trans_id');
$this->db->select('MAX(inventory.trans_items) as trans_items');
$this->db->select('MAX(inventory.trans_user) as trans_user');
$this->db->select('MAX(inventory.trans_date) as trans_date');
$this->db->select('MAX(inventory.trans_comment) as trans_comment');
$this->db->select('MAX(inventory.trans_location) as trans_location');
$this->db->select('MAX(inventory.trans_inventory) as trans_inventory');
if($filters['stock_location_id'] > -1)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('MAX(item_quantities.item_id) as qty_item_id');
$this->db->select('MAX(item_quantities.location_id) as location_id');
$this->db->select('MAX(item_quantities.quantity) as quantity');
$this->db->select('COUNT(items.item_id) as count');
}
else
{
$this->db->select('items.item_id as item_id');
$this->db->select('MAX(items.name) as name');
$this->db->select('MAX(items.category) as category');
$this->db->select('MAX(items.supplier_id) as supplier_id');
$this->db->select('MAX(items.item_number) as item_number');
$this->db->select('MAX(items.description) as description');
$this->db->select('MAX(items.cost_price) as cost_price');
$this->db->select('MAX(items.unit_price) as unit_price');
$this->db->select('MAX(items.reorder_level) as reorder_level');
$this->db->select('MAX(items.receiving_quantity) as receiving_quantity');
$this->db->select('MAX(items.pic_filename) as pic_filename');
$this->db->select('MAX(items.allow_alt_description) as allow_alt_description');
$this->db->select('MAX(items.is_serialized) as is_serialized');
$this->db->select('MAX(items.deleted) as deleted');
$this->db->select('MAX(items.custom1) as custom1');
$this->db->select('MAX(items.custom2) as custom2');
$this->db->select('MAX(items.custom3) as custom3');
$this->db->select('MAX(items.custom4) as custom4');
$this->db->select('MAX(items.custom5) as custom5');
$this->db->select('MAX(items.custom6) as custom6');
$this->db->select('MAX(items.custom7) as custom7');
$this->db->select('MAX(items.custom8) as custom8');
$this->db->select('MAX(items.custom9) as custom9');
$this->db->select('MAX(items.custom10) as custom10');
$this->db->select('MAX(suppliers.person_id) as person_id');
$this->db->select('MAX(suppliers.company_name) as company_name');
$this->db->select('MAX(suppliers.agency_name) as agency_name');
$this->db->select('MAX(suppliers.account_number) as account_number');
$this->db->select('MAX(suppliers.deleted) as deleted');
$this->db->select('MAX(inventory.trans_id) as trans_id');
$this->db->select('MAX(inventory.trans_items) as trans_items');
$this->db->select('MAX(inventory.trans_user) as trans_user');
$this->db->select('MAX(inventory.trans_date) as trans_date');
$this->db->select('MAX(inventory.trans_comment) as trans_comment');
$this->db->select('MAX(inventory.trans_location) as trans_location');
$this->db->select('MAX(inventory.trans_inventory) as trans_inventory');
if($filters['stock_location_id'] > -1)
{
$this->db->select('MAX(item_quantities.item_id) as qty_item_id');
$this->db->select('MAX(item_quantities.location_id) as location_id');
$this->db->select('MAX(item_quantities.quantity) as quantity');
}
}
$this->db->from('items as items');
@@ -221,15 +229,23 @@ class Item extends CI_Model
// avoid duplicated entries with same name because of inventory reporting multiple changes on the same item in the same date range
$this->db->group_by('items.item_id');
// order by name of item
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
else
{
// order by name of item by default
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
/*

View File

@@ -199,12 +199,26 @@ class Item_kit extends CI_Model
return $suggestions;
}
/*
Gets rows
*/
public function get_found_rows($search)
{
return $this->search($search, 0, 0, 'name', 'asc', TRUE);
}
/*
Perform a search on items
*/
public function search($search, $rows=0, $limit_from=0, $sort='name', $order='asc')
public function search($search, $rows = 0, $limit_from = 0, $sort = 'name', $order = 'asc', $count_only = FALSE)
{
$this->db->from('item_kits');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(item_kits.item_kit_id) as count');
}
$this->db->from('item_kits AS item_kits');
$this->db->like('name', $search);
$this->db->or_like('description', $search);
@@ -214,29 +228,22 @@ class Item_kit extends CI_Model
$this->db->or_like('item_kit_id', str_ireplace('KIT ', '', $search));
}
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
return $this->db->get();
}
public function get_found_rows($search)
{
$this->db->from('item_kits');
$this->db->like('name', $search);
$this->db->or_like('description', $search);
//KIT #
if(stripos($search, 'KIT ') !== FALSE)
else
{
$this->db->or_like('item_kit_id', str_ireplace('KIT ', '', $search));
}
$this->db->order_by($sort, $order);
return $this->db->get()->num_rows();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
}
?>

View File

@@ -114,13 +114,13 @@ class Sale extends CI_Model
*/
public function get_found_rows($search, $filters)
{
return $this->search($search, $filters)->num_rows();
return $this->search($search, $filters, 0, 0, 'sales.sale_time', 'desc', TRUE);
}
/**
* Get the sales data for the takings (sales/manage) view
*/
public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'sale_time', $order = 'desc')
public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'sales.sale_time', $order = 'desc', $count_only = FALSE)
{
// Pick up only non-suspended records
$where = 'sales.sale_status = 0 AND ';
@@ -185,27 +185,35 @@ class Sale extends CI_Model
)'
);
$this->db->select('
sales.sale_id AS sale_id,
MAX(DATE(sales.sale_time)) AS sale_date,
MAX(sales.sale_time) AS sale_time,
MAX(sales.invoice_number) AS invoice_number,
MAX(sales.quote_number) AS quote_number,
SUM(sales_items.quantity_purchased) AS items_purchased,
MAX(CONCAT(customer_p.first_name, " ", customer_p.last_name)) AS customer_name,
MAX(customer.company_name) AS company_name,
' . "
IFNULL($sale_subtotal, $sale_total) AS subtotal,
$tax AS tax,
IFNULL($sale_total, $sale_subtotal) AS total,
$sale_cost AS cost,
(IFNULL($sale_subtotal, $sale_total) - $sale_cost) AS profit,
IFNULL($sale_total, $sale_subtotal) AS amount_due,
MAX(payments.sale_payment_amount) AS amount_tendered,
(MAX(payments.sale_payment_amount) - IFNULL($sale_total, $sale_subtotal)) AS change_due,
" . '
MAX(payments.payment_type) AS payment_type
');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(sales.sale_id) as count');
}
else
{
$this->db->select('
sales.sale_id AS sale_id,
MAX(DATE(sales.sale_time)) AS sale_date,
MAX(sales.sale_time) AS sale_time,
MAX(sales.invoice_number) AS invoice_number,
MAX(sales.quote_number) AS quote_number,
SUM(sales_items.quantity_purchased) AS items_purchased,
MAX(CONCAT(customer_p.first_name, " ", customer_p.last_name)) AS customer_name,
MAX(customer.company_name) AS company_name,
' . "
IFNULL($sale_subtotal, $sale_total) AS subtotal,
$tax AS tax,
IFNULL($sale_total, $sale_subtotal) AS total,
$sale_cost AS cost,
(IFNULL($sale_subtotal, $sale_total) - $sale_cost) AS profit,
IFNULL($sale_total, $sale_subtotal) AS amount_due,
MAX(payments.sale_payment_amount) AS amount_tendered,
(MAX(payments.sale_payment_amount) - IFNULL($sale_total, $sale_subtotal)) AS change_due,
" . '
MAX(payments.payment_type) AS payment_type
');
}
$this->db->from('sales_items AS sales_items');
$this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner');
@@ -268,15 +276,25 @@ class Sale extends CI_Model
$this->db->like('payments.payment_type', $this->lang->line('sales_check'));
}
$this->db->group_by('sale_id');
$this->db->order_by($sort, $order);
$this->db->group_by('sales.sale_id');
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
else
{
// order by sale time by default
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
/**

View File

@@ -230,29 +230,21 @@ class Supplier extends Person
*/
public function get_found_rows($search)
{
$this->db->from('suppliers');
$this->db->join('people', 'suppliers.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('company_name', $search);
$this->db->or_like('agency_name', $search);
$this->db->or_like('email', $search);
$this->db->or_like('phone_number', $search);
$this->db->or_like('account_number', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
return $this->search($search, 0, 0, 'last_name', 'asc', TRUE);
}
/*
Perform a search on suppliers
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc')
public function search($search, $rows = 0, $limit_from = 0, $sort = 'last_name', $order = 'asc', $count_only = FALSE)
{
$this->db->from('suppliers');
// get_found_rows case
if($count_only == TRUE)
{
$this->db->select('COUNT(suppliers.person_id) as count');
}
$this->db->from('suppliers AS suppliers');
$this->db->join('people', 'suppliers.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
@@ -266,14 +258,22 @@ class Supplier extends Person
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
// get_found_rows case
if($count_only == TRUE)
{
$this->db->limit($rows, $limit_from);
return $this->db->get()->row_array()['count'];
}
else
{
$this->db->order_by($sort, $order);
return $this->db->get();
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
}
}
?>