-1
我已经在MySQLMySQL视图的查询需要很长加载
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`
SQL SECURITY DEFINER VIEW `inventory_view` AS
select (case when isnull(`a`.`items_sold`) then 0 else `a`.`items_sold` end) AS `sold_qty`,
(case when isnull(`a`.`items_bought`) then 0 else `a`.`items_bought` end) AS `bought_qty`,
(case when isnull(`a`.`credit`) then 0 else `a`.`credit` end) AS `credit_amount`,
(case when isnull(`a`.`debit`) then 0 else `a`.`debit` end) AS `debit_amount`,
(case when isnull(`tv`.`count`) then 0 else `tv`.`count` end) AS `tranfers`,
(case when isnull(`a`.`inhand`) then 0 else `a`.`inhand` end) AS `balance`,
(case when isnull(`tv`.`count`) then `a`.`inhand` when isnull(`a`.`inhand`) then `tv`.`count` else (`a`.`inhand` + `tv`.`count`) end) AS `in_hand`,
(case when isnull(`a`.`company_id`) then `tv`.`fk_company` else `a`.`company_id` end) AS `company`,
(case when isnull(`a`.`branch_id`) then `tv`.`fk_branch` else `a`.`branch_id` end) AS `branch`,
(case when isnull(`a`.`item_code`) then convert(`tv`.`fk_item_code` using utf8) else convert(`a`.`item_code` using utf8) end) AS `item`,
(case when isnull(`a`.`operator`) then `tv`.`user` else `a`.`operator` end) AS `fk_operator`
from (`transfer_view` `tv` left join `inventory_main` `a` on(((convert(`tv`.`fk_item_code` using utf8) = convert(`a`.`item_code` using utf8)) and (`a`.`operator` = `tv`.`user`))))
union
select (case when isnull(`a`.`items_sold`) then 0 else `a`.`items_sold` end) AS `sold_qty`,
(case when isnull(`a`.`items_bought`) then 0 else `a`.`items_bought` end) AS `bought_qty`,
(case when isnull(`a`.`credit`) then 0 else `a`.`credit` end) AS `credit_amount`,
(case when isnull(`a`.`debit`) then 0 else `a`.`debit` end) AS `debit_amount`,
(case when isnull(`tv`.`count`) then 0 else `tv`.`count` end) AS `tranfers`,
(case when isnull(`a`.`inhand`) then 0 else `a`.`inhand` end) AS `balance`,
(case when isnull(`tv`.`count`) then `a`.`inhand` when isnull(`a`.`inhand`) then `tv`.`count` else (`a`.`inhand` + `tv`.`count`) end) AS `in_hand`,
(case when isnull(`a`.`company_id`) then `tv`.`fk_company` else `a`.`company_id` end) AS `company`,
(case when isnull(`a`.`branch_id`) then `tv`.`fk_branch` else `a`.`branch_id` end) AS `branch`,
(case when isnull(`a`.`item_code`) then convert(`tv`.`fk_item_code` using utf8) else convert(`a`.`item_code` using utf8) end) AS `item`,
(case when isnull(`a`.`operator`) then `tv`.`user` else `a`.`operator` end) AS `fk_operator`
from (`inventory_main` `a` left join `transfer_view` `tv` on(((convert(`tv`.`fk_item_code` using utf8) = convert(`a`.`item_code` using utf8)) and (`a`.`operator` = `tv`.`user`))));
它有上千条记录,并执行很慢创建的视图。我运行的任何查询大约需要50秒。
你能帮我找到错误还是建议一个更好的方法来创建视图?
不要将整件事发布在一行上。添加换行符并缩进它,以便我们可以看到它在做什么。 – Barmar
查询本身并没有告诉我们任何事情。请包括select的解释结果以及受影响表中所有索引和索引列的列表。您还需要告诉我们您希望从报告中获得什么(样本数据和基于样本数据的预期输出会有很大的帮助)。 – Shadow
视图有什么作用?这个查询的目的是什么?你需要指定你需要帮助的方式,以便人们能够理解你,而不是有你的观点。我们在你的生活中并不是无所不知的。 – 2016-12-15 19:10:08