[英]Slow query - how to improve query performance in mysql
我有一個包含 449383 條記錄的表。 當我執行以下查詢時,需要花費大量時間才能獲得 32643 條記錄。
這是查詢:
SELECT alloc_id, cr_number, profile_id, table_id, provider_id,
user_id, team_id, trans_reason_id, trans_reason, data_id,
trans_fr_provider_id, trans_to_provider_id, from_office_team_id,
to_office_team_id, trans_fr_resp_officer_id,
trans_to_resp_officer_id, fr_team, to_team, fr_officer,
to_officer, start_date, end_date, alloc_date, request_type,
tr_id, desc_id, desc, bd_status_id, active, display_order,
created_date, created_by, created_by_user_id, modified_date,
modified_by, modified_by_user_id, deleted, deleted_date,
deleted_by, deleted_by_user_id, locked, version
FROM cm_alloc_spg
WHERE team_id IS NOT NULL
AND end_date IS NULL AND locked = 0
LIMIT 1000000;
執行計划:
id select_type table type possible_keys key key_len ref rows Extra
--- ----------- ------------ ---- ------------- ------ ------- ----- ------ ----------------------------------
1 SIMPLE cm_alloc_spg ref team_idx,ed_idx,loc_idx ed_idx 4 const 228838 Using index condition; Using where
創建查詢結構:
CREATE TABLE `cm_alloc_spg` (
`alloc_id` INT(10) NOT NULL AUTO_INCREMENT,
`cr_number` VARCHAR(50) DEFAULT NULL,
`profile_id` INT(11) NOT NULL,
`table_id` INT(11) NOT NULL,
`provider_id` INT(10) DEFAULT NULL,
`user_id` INT(11) DEFAULT NULL,
`team_id` INT(11) DEFAULT NULL,
`trans_reason_id` INT(11) DEFAULT NULL,
`trans_reason` VARCHAR(400) DEFAULT NULL,
`data_id` INT(11) NOT NULL,
`trans_fr_provider_id` INT(11) DEFAULT NULL,
`trans_to_provider_id` INT(11) DEFAULT NULL,
`from_office_team_id` INT(10) DEFAULT NULL,
`to_office_team_id` INT(10) DEFAULT NULL,
`trans_fr_resp_officer_id` INT(10) DEFAULT NULL,
`trans_to_resp_officer_id` INT(10) DEFAULT NULL,
`fr_team` VARCHAR(100) DEFAULT NULL,
`to_team` VARCHAR(100) DEFAULT NULL,
`fr_officer` VARCHAR(100) DEFAULT NULL,
`to_officer` VARCHAR(100) DEFAULT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE DEFAULT NULL,
`alloc_date` DATE DEFAULT NULL,
`request_type` VARCHAR(40) DEFAULT NULL,
`tr_id` INT(11) DEFAULT NULL,
`desc_id` INT(10) DEFAULT NULL,
`desc` VARCHAR(700) DEFAULT NULL,
`bd_status_id` INT(11) DEFAULT NULL,
`active` TINYINT(4) DEFAULT 0,
`display_order` INT(4) NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`created_by` VARCHAR(100) NOT NULL,
`created_by_user_id` INT(11) NOT NULL,
`modified_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`modified_by` VARCHAR(100) NOT NULL,
`modified_by_user_id` INT(11) NOT NULL,
`deleted` TINYINT(4) NOT NULL DEFAULT 0,
`deleted_date` TIMESTAMP NULL DEFAULT NULL,
`deleted_by` VARCHAR(100) DEFAULT NULL,
`deleted_by_user_id` INT(11) DEFAULT NULL,
`locked` TINYINT(4) NOT NULL DEFAULT 0,
`version` INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`alloc_id`),
KEY `fk_profile_id_idx` (`profile_id`),
KEY `fk_user_id_idx` (`user_id`),
KEY `team_idx` (`team_id`),
KEY `cr_number_idx` (`cr_number`),
KEY `tab_idx` (`table_id`),
KEY `ed_idx` (`end_date`),
KEY `loc_idx` (`locked`),
KEY `alloc_date_idx` (`alloc_date`),
KEY `tr_id_idx` (`tr_id`),
KEY `del_idx` (`deleted`),
KEY `to_off_team_id_idx` (`to_office_team_id`),
KEY `trns_res_offcr_id_idx` (`trans_to_resp_officer_id`),
KEY `active_idx` (`active`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
如何提高查詢效率?
我認為您應該使用分頁,如果您有大量記錄,我建議不要獲取所有數據。
SELECT alloc_id,cr_number,PROFILE_ID,table_id的,PROVIDER_ID,USER_ID,TEAM_ID,trans_reason_id,trans_reason,data_id,trans_fr_provider_id,trans_to_provider_id,from_office_team_id,to_office_team_id,trans_fr_resp_officer_id,trans_to_resp_officer_id,fr_team,to_team,fr_officer,to_officer,日期,結束日期,alloc_date,request_type,tr_id , desc_id, desc, bd_status_id, active, display_order, created_date, created_by, created_by_user_id, modified_date, modified_by, modified_by_user_id,deleted,deleted_date,deleted_by,deleted_by_user_id,locked,version from cm_alloc_spg WHERE team_iddate is NOT NULL AND_LOCK 0,100;
限制子句語法索引,沒有要檢索的行
Gupta,你可以做一件事,在這個表中創建 team_id、end_data 和鎖定索引。 您可以通過示例輕松制作任何屬性索引
在 cm_alloc_spg(team_id) 上創建索引 team_id_1;
它將使您的查詢速度提高 30 倍以上。 評論任何幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.