mirror of
https://github.com/grocy/grocy.git
synced 2025-08-19 20:11:16 +00:00
Improve Performance (#927)
* Stock-Overview: Reduce amount of database queries and FindObjectInArray()-calls * Speed-up stock_current by improving products_resolved and creating indices. * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
committed by
GitHub
parent
7510c677f1
commit
42dc55625a
65
migrations/0105.sql
Normal file
65
migrations/0105.sql
Normal file
@@ -0,0 +1,65 @@
|
||||
CREATE VIEW uihelper_stock_current_overview_including_opened
|
||||
AS
|
||||
SELECT
|
||||
p.id,
|
||||
sc.amount_opened AS amount_opened,
|
||||
p.tare_weight AS tare_weight,
|
||||
p.enable_tare_weight_handling AS enable_tare_weight_handling,
|
||||
sc.amount AS amount,
|
||||
sc.product_id AS product_id,
|
||||
sc.best_before_date AS best_before_date,
|
||||
EXISTS(SELECT id FROM stock_missing_products_including_opened WHERE id = sc.product_id) AS product_missing,
|
||||
(SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_stock) AS qu_unit_name,
|
||||
(SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_stock) AS qu_unit_name_plural,
|
||||
p.name AS product_name,
|
||||
(SELECT name FROM product_groups WHERE product_groups.id = product_group_id) AS product_group_name,
|
||||
EXISTS(SELECT * FROM shopping_list WHERE shopping_list.product_id = sc.product_id) AS on_shopping_list,
|
||||
sc.factor_purchase_amount AS factor_purchase_amount,
|
||||
(SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name,
|
||||
(SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name_plural,
|
||||
sc.is_aggregated_amount,
|
||||
sc.amount_opened_aggregated
|
||||
FROM (
|
||||
SELECT *
|
||||
FROM stock_current
|
||||
WHERE best_before_date IS NOT NULL
|
||||
UNION
|
||||
SELECT id, 0, 0, 0, 0, null, 0, 0, 0
|
||||
FROM stock_missing_products_including_opened
|
||||
WHERE id NOT IN (SELECT product_id FROM stock_current)
|
||||
) sc
|
||||
LEFT JOIN products p
|
||||
ON sc.product_id = p.id;
|
||||
|
||||
CREATE VIEW uihelper_stock_current_overview
|
||||
AS
|
||||
SELECT
|
||||
p.id,
|
||||
sc.amount_opened AS amount_opened,
|
||||
p.tare_weight AS tare_weight,
|
||||
p.enable_tare_weight_handling AS enable_tare_weight_handling,
|
||||
sc.amount AS amount,
|
||||
sc.product_id AS product_id,
|
||||
sc.best_before_date AS best_before_date,
|
||||
EXISTS(SELECT id FROM stock_missing_products_including_opened WHERE id = sc.product_id) AS product_missing,
|
||||
(SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_stock) AS qu_unit_name,
|
||||
(SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_stock) AS qu_unit_name_plural,
|
||||
p.name AS product_name,
|
||||
(SELECT name FROM product_groups WHERE product_groups.id = product_group_id) AS product_group_name,
|
||||
EXISTS(SELECT * FROM shopping_list WHERE shopping_list.product_id = sc.product_id) AS on_shopping_list,
|
||||
sc.factor_purchase_amount AS factor_purchase_amount,
|
||||
(SELECT name FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name,
|
||||
(SELECT name_plural FROM quantity_units WHERE quantity_units.id = p.qu_id_purchase) AS qu_purchase_unit_name_plural,
|
||||
sc.is_aggregated_amount,
|
||||
sc.amount_opened_aggregated
|
||||
FROM (
|
||||
SELECT *
|
||||
FROM stock_current
|
||||
WHERE best_before_date IS NOT NULL
|
||||
UNION
|
||||
SELECT id, 0, 0, 0, 0, null, 0, 0, 0
|
||||
FROM stock_missing_products
|
||||
WHERE id NOT IN (SELECT product_id FROM stock_current)
|
||||
) sc
|
||||
LEFT JOIN products p
|
||||
ON sc.product_id = p.id;
|
||||
29
migrations/0106.sql
Normal file
29
migrations/0106.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
CREATE INDEX ix_products_performance1 ON products (
|
||||
parent_product_id
|
||||
);
|
||||
|
||||
CREATE INDEX ix_products_performance2 ON products (
|
||||
CASE WHEN parent_product_id IS NULL THEN id ELSE parent_product_id END,
|
||||
active
|
||||
);
|
||||
|
||||
CREATE INDEX ix_stock_performance1 ON stock (
|
||||
product_id,
|
||||
open,
|
||||
best_before_date,
|
||||
amount
|
||||
);
|
||||
|
||||
DROP VIEW products_resolved;
|
||||
CREATE VIEW products_resolved
|
||||
AS
|
||||
SELECT
|
||||
CASE
|
||||
WHEN p.parent_product_id IS NULL THEN
|
||||
p.id
|
||||
ELSE
|
||||
p.parent_product_id
|
||||
END AS parent_product_id,
|
||||
p.id as sub_product_id
|
||||
FROM products p
|
||||
WHERE p.active = 1;
|
||||
Reference in New Issue
Block a user