慢速查询-如何在mysql中提高查询性能
我有一张有449383条记录的桌子。当我执行下面的查询时,需要花费大量时间才能获得32643条记录 以下是查询:慢速查询-如何在mysql中提高查询性能,mysql,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, f
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
如何提高查询效率?我认为您应该使用分页,如果您有大量记录,我建议永远不要获取所有数据 选择alloc_id、cr_编号、profile_id、table_id、provider_id、user_id、team_id、trans_reason_id、trans_reason、data_id、, trans\u fr\u provider\u id、trans\u to\u provider\u id、fr\u to\u id、fr\u to\u provider\u id、fr\u id、fr\u to\u团队、fr\u官员、to\u官员、to\u官员、开始日期、结束日期、分配日期、请求类型、tr\u id、描述id、描述id、bd\u状态、活动、显示订单、创建日期、创建日期、修改日期、,修改人、修改人用户id、删除人、删除人日期、删除人、删除人用户id、锁定、来自cm\U alloc\u spg的版本,其中团队id不为空,结束日期为空且锁定=0限制0100
limit子句语法索引,要检索的行数,您可以做一件事,只需在该表中创建团队id、结束数据并锁定索引。 您可以通过以下方式轻松创建任何属性索引: 范例 在cm_alloc_spg(团队id)上创建索引团队id_1 它将使您的查询速度提高30倍以上。
请留言寻求帮助。但您希望得到什么?返回32k记录不会很快。“这需要很多时间”-你应该更精确。你可能什么都做不了。使用的索引似乎是最佳的。大约需要6-7秒。执行查询后,我只得到32643条记录。我想查看我正在使用的所有y记录,但您正在使用带有end_date和locked的where子句,因此它们也需要索引。理想情况下,topicstarter@s.gupta应该制作多列索引
(团队id,end_date,locked)
我已经在团队id上创建了索引,结束日期并锁定column@RaymondNijland是的,你是对的,S.古普塔,请做吧check@S.gupta请对所有三个它将影响的组进行索引。