I have a view and its taking longer time to execute so i need to write simple query to rather than view.
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)
I created a view using this query and its perfomance is too slow to optimize the performance i need to convert this into a simple query and store the result in a table
the query is having multiple joins and making the perfomance slower so i thought it is good to go with simpler query to select values from each table and then store in a new table
I am expecting optimised way for this query for better performance
2
Answers
A sample
`id` bigint NOT NULL DEFAULT ‘0’,
`val` bigint NOT NULL DEFAULT ‘0’,
`created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
`grp` varchar(10) DEFAULT ‘unknown’,
`id` int unsigned NOT NULL,
`val` varchar(10) DEFAULT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
fiddle
The
CREATE TABLE
andSELECT
can be done in a single statementCreating a table just to run a query will not be efficient.
Let’s see one of your problematic
SELECTs
against theVIEW
, plusEXPLAIN SELECT
. We may be able to work with that to help improve the performance.What version of MySQL are you running?