我有一张有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
如何提高查询效率?
答案 0 :(得分:0)
我认为您应该使用分页,如果您有大量记录,建议不要获取所有数据。
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_nzl_id,trans_to_resp_nzl_id,fr_team,to_team,fr_leg,to_leg,start_date,end_date,alloc_date,request_type,by_id,by_id,dested_ed,dested_stat,stat_id,dested_stat,stat_id,dested_stat,stat_id,dested_stat,stat_id, Modify_by_user_id,deleted,deleted_date,delete_by,deleted_by_user_id,锁定,版本FROM cm_alloc_spg WHERE team_id IS NOT NULL AND end_date IS NULL AND lock = 0 LIMIT 0,100;
limit子句语法索引,没有要检索的行
答案 1 :(得分:0)
古普塔,您只需要在表中创建team_id,end_data并锁定 index 即可。 您可以通过以下方式轻松创建任何属性索引 例子
创建索引team_id_1 ON cm_alloc_spg(team_id);
它将使您的查询速度提高30倍以上。 注释掉任何帮助。