sql - need help in optimizing mysql query -
sql - need help in optimizing mysql query -
i writing mysql query below
select `user_master`.`first_name`, `city_name`, `user_master`.`last_name`, `user_master`.`user_master_id`, `account_management_master`.`account_name`, `donation_receipt_info`.`receipt_temple_id`, date(dt) dt, sum(`donation_receipt_info`.`amount`) amount (`donation_receipt_info`) bring together `donation_receipt_master` on donation_receipt_master`.`receipt_id`=`donation_receipt_info`.`receipt_id` bring together `account_management_master` on `account_management_master`.`account_id`=`donation_receipt_info`.`account_id` bring together `user_master` on `user_master`.`user_master_id`=`donation_receipt_master`.`user_master_id` bring together `user_address_info` on `user_address_info`.`user_master_id`=`user_master`.`user_master_id` bring together `city_master` on `city_master`.`city_id`=`user_address_info`.`city_id` `donation_receipt_info`.`temple_id` = '1' grouping `donation_receipt_info`.`receipt_id`, `donation_receipt_info`.`account_id`
the table donation_receipt_info , master have approx 42k results query taking way much time of 5 6 minutes execute in mysql itself. can please help me optimize query, help or suggestion helpful thanks.
first, query impossible read. should format , larn utilize table aliases:
select um.first_name, city_name, um.last_name, um.user_master_id, amm.account_name, dri.receipt_temple_id, date(dt) dt, sum(dri.amount) amount donation_receipt_info dri bring together donation_receipt_master drm on drm.receipt_id = dri.receipt_id bring together account_management_master amm on amm.account_id = dri.account_id bring together user_master um on um.user_master_id = drm.user_master_id bring together user_address_info uai on uai.user_master_id = um.user_master_id bring together city_master cm on cm.city_id = uai.city_id dri.temple_id = '1' grouping dri.receipt_id, dri.account_id;
next. tables have obvious indexes? is, each table appears have id , these should declared keys (primary keys preferably). instance, city_master(city_id)
.
next, there should index on donation_receipt_info(temple_id, receipt_id, account_id)
. should help where
. note: if temple_id
integer, where
clause should expressed where dri.temple_id = 1
-- no quotes. don't want mysql confused , decide not utilize index.
these changes help. 5-6 minutes seems long time such query.
mysql sql join
Comments
Post a Comment