即使使用索引,MySQL视图也很慢生成

时间:2018-06-07 11:01:04

标签: mysql

我们目前正在将数据库迁移到一台应该具有更好性能的新服务器。所有索引,表名,视图等都将从旧服务器复制所有内容。

但是,在新服务器上,生成视图的时间要长得多,新服务器需要15秒,而旧服务器则需要0.0021!

我的导出视图具有以下查询

CREATE ALGORITHM=UNDEFINED DEFINER=`easycoun_st`@`localhost` SQL SECURITY 

DEFINER VIEW `view_stocktake_locations`  AS  select `stocktake_locations`.`id` AS `id`,
`stocktake_locations`.`stocktake_id` AS `stocktake_id`,`stocktake_locations`.`store_locations_id` AS `store_locations_id`,`stocktake_locations`.`stocktake_status` AS `stocktake_status`,
`stocktake_locations`.`stocktake_date` AS `stocktake_date`,`stocktake_locations`.`stocktake_value` AS `stocktake_value`,`stocktake_locations`.`is_completed` AS `is_completed`,
(select sum(`stocktake_details`.`quantity` * `stocktake_details`.`unit_of_measure` + `stocktake_details`.`quantity_units`) from `stocktake_details` where `stocktake_details`.`location_id` = `stocktake_locations`.`id`) AS `total_quantity`,
(select sum(`stocktake_details`.`quantity` * `stocktake_details`.`full_cost` + `stocktake_details`.`quantity_units` * `stocktake_details`.`unit_cost`) from `stocktake_details` where `stocktake_details`.`location_id` = `stocktake_locations`.`id`) AS `total_cost`,
(select `store_locations`.`location_name` from `store_locations` where `store_locations`.`id` = `stocktake_locations`.`store_locations_id`) AS `location_name`,(select `store_locations`.`store_id` from `store_locations` 
where `store_locations`.`id` = `stocktake_locations`.`store_locations_id`) AS `store_id` from `stocktake_locations` ;

当我在两台服务器上运行快速EXPLAIN SELECT * FROM view_stocktake_locatons时,我会得到不同的结果。 new serverold server

正如我所说,所有键,索引等完全相同。我看不出为什么在生成结果时存在这样的时间差异的原因,并且无法理解为什么新服务器以派生表而不是主表开始。

0 个答案:

没有答案