如何进一步优化此查询

时间:2017-03-09 03:48:07

标签: mysql

  

我尽我所能,但无法获得最少的执行时间。

SELECT e.code_assigned, 
       Count(DISTINCT tcp.id) AS total_qa, 
       Count(DISTINCT tcp.encounter_uid) AS total_records,
       Sum(IF(tcp.qa_disagree = '1', tcd.weight, 0)) AS error_points,
       Sum(IF(tcp.qa_disagree = '1',1,0)) AS total_disagree,
       Sum(tcd.weight) AS total_qa_weight,
       Count(DISTINCT CASE WHEN tcp.qa_disagree = '1' THEN tcp.encounter_uid end) AS total_disagree_record

FROM tbl_encounter_detail AS e 
STRAIGHT_JOIN tbl_event_cpt_pro AS tcp ON tcp.encounter_uid = e.encounter_uid AND tcp.work_item_uid = e.work_item_uid    
JOIN tbl_encounter as ed ON tcp.encounter_uid = ed.encounter_uid
LEFT JOIN tbl_code_weight AS tcd ON tcd.code = tcp.procedure_num    

WHERE  e.qa_complete = 1 
GROUP  BY code_assigned

UNION ALL

SELECT e.code_assigned, 
       Count(DISTINCT tcf.id) AS total_qa,
       Count(DISTINCT tcf.encounter_uid) AS total_records,
       Sum(IF(tcf.qa_disagree = '1', tcd.weight, 0)) AS error_points,
       Sum(IF(tcf.qa_disagree = '1',1,0)) AS total_disagree,
       Sum(tcd.weight) AS total_qa_weight,                    
       Count(DISTINCT CASE WHEN tcf.qa_disagree = '1' THEN tcf.encounter_uid end) AS total_disagree_record

FROM tbl_encounter_detail AS e 
STRAIGHT_JOIN  tbl_event_cpt_fac AS tcf ON tcf.encounter_uid = e.encounter_uid AND tcf.work_item_uid = e.work_item_uid   
JOIN tbl_encounter as ed ON tcf.encounter_uid = ed.encounter_uid
LEFT JOIN tbl_code_weight AS tcd ON tcd.code = tcf.procedure_num

WHERE e.qa_complete = 1 
GROUP BY code_assigned
  

此查询阻碍了性能(这是我能做的最少)我不知道为什么我无法对其进行更优化。真正阻碍它的是什么?请帮忙。

new explain plan

  

for tbl_event_cpt_pro和tbl_event_cpt_fac

CREATE TABLE `tbl_event_cpt_pro` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`encounter_uid` int(11) NOT NULL,
`procedure_num` varchar(10) NOT NULL,
`procedure_qty` int(11) NOT NULL DEFAULT '1',
`procedure_modifier1` varchar(10) DEFAULT NULL,
`procedure_modifier2` varchar(10) DEFAULT NULL,
`procedure_provider` varchar(75) DEFAULT NULL,
`mid_provider` varchar(75) DEFAULT NULL,
`userid` varchar(50) NOT NULL,
`user_time_entered` datetime NOT NULL,
`qa_disagree` tinyint(1) NOT NULL DEFAULT '0',
`qa_procedure_num` varchar(10) DEFAULT NULL,
`qa_procedure_qty` int(5) DEFAULT NULL,
`qa_procedure_modifier` varchar(10) DEFAULT NULL,
`qa_procedure_provider` varchar(75) DEFAULT NULL,
`qa_note` varchar(255) DEFAULT NULL,
`qa_userid` varchar(50) DEFAULT NULL,
`qa_user_time_entered` datetime DEFAULT NULL,
`qa_reason` varchar(75) DEFAULT NULL,
`work_item_uid` int(11) NOT NULL,
`user_time_entered_utc` datetime DEFAULT NULL,
`cosigning_physician` varchar(75) DEFAULT NULL,
`procedure_date` date DEFAULT NULL COMMENT 'Date procedure was
 performed. May not be date of service/admit date.',
`charge_code_select` varchar(20) DEFAULT NULL COMMENT 'If the
procedure CPT has multiple charge codes, this indicates the one chosen    by coder',
`specialist` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Indicates   Specialist outpatient procedure - ref IU and Sturdy',
`dos` date DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_enc_uid` (`encounter_uid`),
 KEY `procedure_provider` (`procedure_provider`),
 KEY `mid_provider` (`mid_provider`),
 KEY `qa_procedure_provider` (`qa_procedure_provider`),
 KEY `userid` (`userid`),
 KEY `qa_userid` (`qa_userid`),
 KEY `work_item_uid` (`work_item_uid`),
 KEY `work_item_uid_2` (`work_item_uid`,`encounter_uid`),
 KEY `cosigning_physician` (`cosigning_physician`),
 KEY `code_index` (`procedure_num`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_1` FOREIGN KEY (`encounter_uid`)   REFERENCES `tbl_encounter` (`encounter_uid`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_10` FOREIGN KEY (`work_item_uid`) REFERENCES `tbl_work_item` (`work_item_uid`) ON DELETE CASCADE,
 CONSTRAINT `tbl_event_cpt_pro_ibfk_11` FOREIGN KEY (`userid`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_12` FOREIGN KEY (`work_item_uid`,  `encounter_uid`) REFERENCES `tbl_encounter_detail` (`work_item_uid`, `encounter_uid`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_13` FOREIGN KEY (`cosigning_physician`) REFERENCES `tbl_provider` (`provider_lov`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_2` FOREIGN KEY (`procedure_provider`) REFERENCES `tbl_provider` (`provider_lov`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_3` FOREIGN KEY (`mid_provider`) REFERENCES `tbl_provider` (`provider_lov`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_4` FOREIGN KEY (`qa_procedure_provider`) REFERENCES `tbl_provider` (`provider_lov`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_5` FOREIGN KEY (`userid`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_6` FOREIGN KEY (`qa_userid`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_7` FOREIGN KEY (`work_item_uid`) REFERENCES `tbl_work_item` (`work_item_uid`),
 CONSTRAINT `tbl_event_cpt_pro_ibfk_9` FOREIGN KEY (`encounter_uid`) REFERENCES `tbl_encounter` (`encounter_uid`) ON DELETE CASCADE

  

for tbl_encounter_detail

 CREATE TABLE `tbl_encounter_detail` (
 `enc_detail_uid` int(11) NOT NULL AUTO_INCREMENT,
 `encounter_uid` int(11) NOT NULL,
 `work_item_uid` int(11) NOT NULL,
 `code_assigned` varchar(50) DEFAULT NULL,
 `coding_start_time` datetime DEFAULT NULL,
 `code_complete_ts` datetime DEFAULT NULL,
 `code_complete` int(1) NOT NULL DEFAULT '0',
 `suspend_active` int(1) DEFAULT NULL,
 `qa_flag` int(1) NOT NULL DEFAULT '0',
 `qa_complete` int(1) NOT NULL DEFAULT '0',
 `qa_start_time` datetime DEFAULT NULL,
 `qa_complete_ts` datetime DEFAULT NULL,
 `qa_assigned` varchar(50) DEFAULT NULL,
 `dataentry_complete` int(1) NOT NULL DEFAULT '0',
 `dataentry_start_time` datetime DEFAULT NULL,
 `dataentry_complete_ts` datetime DEFAULT NULL,
 `dataentry_assigned` varchar(50) DEFAULT NULL,
 `encounter_status` varchar(30) NOT NULL,
 `dataentry_assigned_tec` varchar(50) DEFAULT NULL COMMENT 'Data entry user for Technical component',
 `dataentry_complete_tec` int(1) NOT NULL DEFAULT '0' COMMENT 'Data Entry complete for Technical component; used only for split data entry clients',
 `dataentry_assigned_pf` varchar(50) DEFAULT NULL COMMENT 'Data entry user for Profee component',
 `dataentry_complete_pf` int(1) NOT NULL DEFAULT '0' COMMENT 'Data Entry complete for Profee component',
 `qa_feedback` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Set when the QA reviewer has feedback on disagreements',
 `qa_seen_by_coder_utc` datetime DEFAULT NULL COMMENT 'Set when the coder acknowledges QA feedback on this encounter',
 `qa_dataentry` tinyint(1) DEFAULT '0' COMMENT 'Set if data entry requested QA',
 `code_complete_pf` tinyint(1) DEFAULT '0' COMMENT 'When LiveCode and split data entry are active, this indicates Profee coding is done; otherwise ignored',
 `qa_requested` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Whether QA was requested by Coder or Data Entry (vs random sample)',
 PRIMARY KEY (`enc_detail_uid`),
 UNIQUE KEY `uniq_wi_enc` (`work_item_uid`,`encounter_uid`),
 KEY `idx_enc_uid` (`encounter_uid`),
 KEY `idx_code_assigned` (`code_assigned`),
 KEY `encounter_status` (`encounter_status`),
 KEY `qa_assigned` (`qa_assigned`),
 KEY `dataentry_assigned` (`dataentry_assigned`),
 KEY `dataentry_assigned_tec` (`dataentry_assigned_tec`),
 KEY `dataentry_assigned_pf` (`dataentry_assigned_pf`),
 KEY `idx_qa_complete` (`qa_complete`),
 KEY `idx_composite` (`qa_complete`,`code_assigned`,`encounter_uid`,`work_item_uid`),
 CONSTRAINT `tbl_encounter_detail_ibfk_1` FOREIGN KEY (`encounter_uid`) REFERENCES `tbl_encounter` (`encounter_uid`),
 CONSTRAINT `tbl_encounter_detail_ibfk_2` FOREIGN KEY (`work_item_uid`) REFERENCES `tbl_work_item` (`work_item_uid`),
 CONSTRAINT `tbl_encounter_detail_ibfk_3` FOREIGN KEY (`code_assigned`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_encounter_detail_ibfk_4` FOREIGN KEY (`qa_assigned`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_encounter_detail_ibfk_5` FOREIGN KEY (`dataentry_assigned`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_encounter_detail_ibfk_6` FOREIGN KEY (`dataentry_assigned_tec`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_encounter_detail_ibfk_7` FOREIGN KEY (`dataentry_assigned_pf`) REFERENCES `tbl_user` (`user_email_address`),
 CONSTRAINT `tbl_encounter_detail_ibfk_8` FOREIGN KEY (`encounter_status`) REFERENCES `tbl_encounter_status` (`id`)

  

for tbl_encounter

CREATE TABLE `tbl_encounter` (
`encounter_uid` int(11) NOT NULL AUTO_INCREMENT,
`patient_name` varchar(75) NOT NULL,
`patient_sex` varchar(1) NOT NULL,
`patient_dob` date NOT NULL,
`encounter_dos` date NOT NULL,
`batch_control` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_location` varchar(50) CHARACTER SET latin1 DEFAULT NULL,
`facility_id` varchar(75) NOT NULL,
`patient_mrn` varchar(20) NOT NULL,
`patient_fin` varchar(20) NOT NULL,
`encounter_lwbs` tinyint(1) DEFAULT NULL,
`encounter_ama` tinyint(1) DEFAULT NULL,
`encounter_reason_for_visit` varchar(300) DEFAULT NULL,
`encounter_chief_complaint` varchar(2048) DEFAULT NULL,
`encounter_ins_type` varchar(50) CHARACTER SET latin1 DEFAULT NULL,
`encounter_ins_1` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_ins_2` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_primary_type` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_primary_class` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_secondary_type` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_secondary_class` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_disch_diagnosis` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_disch_disp` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_type` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_assign_provider1` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`encounter_assign_provider2` varchar(75) DEFAULT NULL COMMENT 'Obsolete - should be dropped in future',
`encounter_admit_mode` varchar(75) CHARACTER SET latin1 DEFAULT NULL,
`load_time` datetime NOT NULL,
`enc_admit` datetime DEFAULT NULL COMMENT 'Datetime of inpatient admission.',
`import_loc` varchar(200) DEFAULT NULL COMMENT 'Source of record: typically file name and line number',
`import_line` bigint(20) unsigned DEFAULT NULL,
`attend_provider_uid` int(10) unsigned DEFAULT NULL,
`disch_time_utc` datetime DEFAULT NULL,
`last_adt_recorded_utc` datetime DEFAULT NULL COMMENT 'The EVN.2 recorded timestamp of the last ADT update applied. Used to ensure updates are applied in order.',
`loaded_disch_disp` varchar(75) DEFAULT NULL COMMENT 'The discharge disposition originally loaded from ED Log for this chart',
`admitted` tinyint(1) DEFAULT NULL,
`injury_real_date` date DEFAULT NULL,
`lmp_date` date DEFAULT NULL COMMENT 'Last Menstrual Period',
`transfer` tinyint(1) DEFAULT NULL COMMENT 'Coded in patient header when patient was transferred between facilities',
`transfer_dest` varchar(75) DEFAULT NULL COMMENT 'If chart is a transfer to another facility, the coder specifies the facility in this field',
`dos_end` date DEFAULT NULL,
`outpatient` tinyint(1) DEFAULT NULL,
`discharge` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`encounter_uid`),
KEY `idx_fac_id` (`facility_id`),
KEY `idx_name` (`patient_name`),
KEY `idx_dos` (`encounter_dos`),
KEY `attend_provider_uid` (`attend_provider_uid`),
KEY `idx_fin` (`patient_fin`),
KEY `idx_ins` (`encounter_primary_type`),
KEY `idx_prov` (`encounter_assign_provider1`),
KEY `idx_mrn_temp` (`patient_mrn`),
CONSTRAINT `tbl_encounter_ibfk_1` FOREIGN KEY (`attend_provider_uid`) REFERENCES `tbl_provider` (`provider_uid`)

  

for tbl_code_weight

Create Table: CREATE TABLE `tbl_code_weight` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(20) NOT NULL,
`weight` float NOT NULL,
KEY `idx_composite` (`code`,`weight`)
PRIMARY KEY (`id`)

0 个答案:

没有答案