我有一个视图,它需要更长的时间来执行,所以我需要编写简单的查询而不是视图。
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER
VIEW `view_party_make_subcat_size_map` AS
select distinct `m`.`part_name` AS `material_name`,`pt`.`pk_id` AS `pk_id`,`pms`.`fk_party_id` AS `fk_party_id`,`pms`.`fk_party_type_id` AS `fk_party_type_id`,`k`.`pk_id` AS `fk_make_id`,`pms`.`fk_sub_cat_id` AS `fk_sub_cat_id`,`pms`.`fk_size_id` AS `fk_size_id`,`pms`.`status` AS `status`,`pms`.`fk_financial_year_id` AS `fk_financial_year_id`,`pms`.`fk_company_id` AS `fk_company_id`,`pms`.`quantity` AS `quantity`,`k`.`name` AS `make_name`,`sc`.`name` AS `sub_cat_name`,`pt`.`name` AS `party_name`,`pt`.`fk_city_id` AS `fk_city_id`,`pt`.`fk_state_id` AS `fk_state_id`,`pt`.`company_type` AS `company_type`,`pt`.`fk_area` AS `fk_area`,`hsn`.`hsn_name` AS `hsn_name`,`m`.`fk_hsn_id` AS `fk_hsn_id`,`size`.`name` AS `size_name`,`pt`.`address` AS `party_address`
from (((((((`ma_party_make_subcat_map` `pms`
join `ma_company` `pt` on((`pt`.`pk_id` = `pms`.`fk_party_id`)))
left join `ma_sub_category` `sc` on((`sc`.`pk_id` = `pms`.`fk_sub_cat_id`)))
join `ma_make` `k` on((`k`.`pk_id` = `pms`.`fk_make_id`)))
join `ma_material_make_map` `mm` on((`mm`.`fk_make_id` = `k`.`pk_id`)))
join `ma_material` `m` on((`m`.`pk_id` = `mm`.`fk_material_id`)))
left join `ma_hsn_tax_map` `hsn` on((`m`.`fk_hsn_id` = `hsn`.`fk_hsn_id`)))
join `ma_size` `size` on((`size`.`pk_id` = `pms`.`fk_size_id`)))
where (`pms`.`status` = 2)
我使用此查询创建了一个视图,它的性能太慢,无法优化性能,我需要将其转换为简单的查询并将结果存储在表中
查询有多个连接,导致性能变慢,所以我认为最好使用更简单的查询来从每个表中选择值,然后存储在新表中
我期待这个查询的优化方法以获得更好的性能
我需要将其转换为一个简单的查询并将结果存储在表中
样品
-- create some source table
create table t1 select 1 id, 1 val, now() created_at union select 2, 2, now();
select * from t1;
-- create a view based on this table
create view t2 as select * from t1;
select * from t2;
id | 瓦尔 | 创建于 |
---|---|---|
1 | 1 | 2024-07-05 08:53:54 |
2 | 2 | 2024-07-05 08:53:54 |
id | 瓦尔 | 创建于 |
---|---|---|
1 | 1 | 2024-07-05 08:53:54 |
2 | 2 | 2024-07-05 08:53:54 |
-- create a table with the data returned by the view
create table t3 table t2;
select * from t3;
show create table t3;
id | 瓦尔 | 创建于 |
---|---|---|
1 | 1 | 2024-07-05 08:53:54 |
2 | 2 | 2024-07-05 08:53:54 |
桌子 | 创建表 |
---|---|
t3 | 创建表 `t3` ( `id` bigint NOT NULL DEFAULT '0', `val` bigint NOT NULL DEFAULT '0', `created_at` datetime NOT NULL ) 引擎=InnoDB 默认字符集=utf8mb4 COLLATE=utf8mb4_090 0_ai_ci |
-- create a table with the data returned by the view
-- and specify / adjust its structure
create table t4 ( id int unsigned primary key,
val varchar(10),
grp varchar(10) default 'unknown'
) table t2;
select * from t4;
show create table t4;
grp | id | 瓦尔 | 创建于 |
---|---|---|---|
未知 | 1 | 1 | 2024-07-05 08:53:54 |
未知 | 2 | 2 | 2024-07-05 08:53:54 |
桌子 | 创建表 |
---|---|
t4 | 创建表`t4`( `grp` varchar(10)默认'unknown', KEY (`id`) ) 引擎=InnoDB 默认字符集=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |