如何优化以下MySQL查询

时间:2017-01-11 04:48:44

标签: mysql sql query-optimization

我尽力优化以下查询:

SELECT 
    prov,
    role,
    COUNT(*) AS visits,
    charge_amt,
    charge_amt * COUNT(*) AS AvgCharge,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu)),
                2),
            '')) AS WorkRVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu)),
                2),
            '')) / SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu) + COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu) + COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) AS AvgWorkRVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) AS MALRVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) / SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu) + COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu) + COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) AS AvgMalRVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu)),
                2),
            '')) AS PERVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu)),
                2),
            '')) / SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu) + COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu) + COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) AS AvgPervRVU,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu) + COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu) + COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) AS TotalRvu,
    SUM(IFNULL(FORMAT(qty * (COALESCE(WithMod.work_rvu, WithoutMod.work_rvu) + COALESCE(WithMod.fac_pe_rvu,
                        WithoutMod.fac_pe_rvu) + COALESCE(WithMod.mp_rvu, WithoutMod.mp_rvu)),
                2),
            '')) / COUNT(*) AS AvgTotalRvu
FROM
    (SELECT 
        CAST(`e`.`encounter_dos` AS DATE) AS `encounter_dos`,
            `e`.`encounter_uid` AS `encounter_uid`,
            `ed`.`work_item_uid` AS `work_item_uid`,
            `e`.`encounter_ins_1` AS `encounter_ins_1`,
            `ed`.`encounter_status` AS `encounter_status`,
            MAX((((`pro`.`em_level` <> '0')
                OR (`fac`.`em_level` <> '0'))
                AND (`ed`.`encounter_status` = 'DE_COMPLETE'))) AS `Billed`,
            MAX(((ISNULL(`pro`.`em_level`)
                OR (`pro`.`em_level` = '0'))
                AND (ISNULL(`fac`.`em_level`)
                OR (`fac`.`em_level` = '0'))
                AND (`ed`.`encounter_status` IN ('DE_COMPLETE' , 'DE_LWBS', 'LWBS')))) AS `NoCharge`
    FROM
        (((`tbl_encounter` `e`
    JOIN `tbl_encounter_detail` `ed` ON ((`e`.`encounter_uid` = `ed`.`encounter_uid`)))
    LEFT JOIN `tbl_event_em_pro` `pro` ON (((`pro`.`encounter_uid` = `e`.`encounter_uid`)
        AND (`pro`.`work_item_uid` = `ed`.`work_item_uid`))))
    LEFT JOIN `tbl_event_em_fac` `fac` ON (((`fac`.`encounter_uid` = `e`.`encounter_uid`)
        AND (`fac`.`work_item_uid` = `ed`.`work_item_uid`))))
    WHERE
        (`ed`.`encounter_status` IN ('COMPLETE' , 'DE_COMPLETE', 'DE_HOLD', 'DE_IN_PROGRESS', 'DE_LWBS', 'DE_PRO_IN_PROGRESS', 'DE_PRO_READY', 'DE_TEC_IN_PROGRESS', 'DE_TEC_READY', 'IN_PROGRESS', 'IN_QA', 'LWBS', 'OPEN', 'REGISTERED', 'SUSPEND', 'TRANSFER', 'VISIT_COMPLETE', 'VISIT_IN_PROGRESS'))
    GROUP BY `e`.`encounter_uid` , `ed`.`work_item_uid`) vw
        JOIN
    tbl_encounter USING (encounter_uid)
        JOIN
    tbl_work_item USING (work_item_uid)
        JOIN
    tbl_facility ON tbl_facility.facility_id = tbl_work_item.facility_id
        JOIN
    (SELECT 
        work_item_uid,
            encounter_uid,
            em_level AS cpt,
            1 AS qty,
            em_modifier1 AS mod1,
            mid AS Prov,
            'MLP PRIMARY' AS Role
    FROM
        tbl_event_em_pro
    WHERE
        em_level <> '0' AND mid <> ''
            AND shared = 0 UNION ALL SELECT 
        work_item_uid,
            encounter_uid,
            em_level AS cpt,
            1 AS qty,
            em_modifier1 AS mod1,
            em_provider,
            'PHYSICIAN (NO SEC)'
    FROM
        tbl_event_em_pro
    WHERE
        em_level <> '0' AND mid <> ''
            AND shared = 1 UNION ALL SELECT 
        work_item_uid,
            encounter_uid,
            em_level AS cpt,
            1 AS qty,
            em_modifier1 AS mod1,
            em_provider,
            'PHYSICIAN (NO SEC)'
    FROM
        tbl_event_em_pro
    WHERE
        em_level <> '0' AND mid = ''
            AND shared = 0 UNION ALL SELECT 
        cpt.work_item_uid,
            cpt.encounter_uid,
            cpt.procedure_num,
            GREATEST(1, cpt.procedure_qty),
            cpt.procedure_modifier1,
            em.em_provider,
            'PHYSICIAN (NO SEC)'
    FROM
        tbl_event_cpt_pro cpt
    JOIN tbl_event_em_pro em USING (encounter_uid)
    WHERE
        em.mid <> '' AND em.shared = 1 UNION ALL SELECT 
        cpt.work_item_uid,
            cpt.encounter_uid,
            cpt.procedure_num,
            GREATEST(1, cpt.procedure_qty),
            cpt.procedure_modifier1,
            em.mid,
            'MLP PRIMARY'
    FROM
        tbl_event_cpt_pro cpt
    JOIN tbl_event_em_pro em USING (encounter_uid)
    WHERE
        em.mid <> '' AND em.shared = 0 UNION ALL SELECT 
        cpt.work_item_uid,
            cpt.encounter_uid,
            cpt.procedure_num,
            GREATEST(1, cpt.procedure_qty),
            cpt.procedure_modifier1,
            em.em_provider,
            'PHYSICIAN (NO SEC)'
    FROM
        tbl_event_cpt_pro cpt
    JOIN tbl_event_em_pro em USING (encounter_uid)
    WHERE
        em.mid = '' AND em.shared = 0) T USING (work_item_uid , encounter_uid)
        LEFT JOIN
    (SELECT 
        work_item_uid,
            cpt_hcpcs_code AS cpt,
            MIN(client_proc_desc) AS client_proc_desc,
            client_proc_desc AS descrip,
            charge_amt AS charge_amt
    FROM
        tbl_charge_master
    WHERE
        charge_type = 'ED_PROFEE'
            AND effective_date = '2015-01-01'
    GROUP BY work_item_uid , cpt_hcpcs_code) cm USING (work_item_uid , cpt)
        LEFT JOIN
    tbl_rvu_master WithMod ON WithMod.hcpcs = cpt
        AND WithMod.modifier = mod1
        AND WithMod.effective_date = '2015-01-01'
        LEFT JOIN
    tbl_rvu_master WithoutMod ON WithoutMod.hcpcs = cpt
        AND WithoutMod.modifier = ''
        AND WithoutMod.effective_date = '2015-01-01'
        LEFT JOIN
    tbl_cpt_blacklist bl ON bl.cpt_code = cpt
        AND bl.charge_type = 'ED_PROFEE'
        AND (bl.work_item_uid IS NULL
        OR bl.work_item_uid = vw.work_item_uid)
WHERE
    bl.cpt_code IS NULL AND Billed
GROUP BY prov , role
ORDER BY vw.encounter_dos , patient_fin , prov;

以下是上述查询的解释输出:

---+-----------------------------------------------------+--------+---------------------------------------------------------------------+
| id | select_type  | table              | type   | possible_keys                                                                                   | key          | key_len | ref                                                 | rows   | Extra                                                               |
+----+--------------+--------------------+--------+-------------------------------------------------------------------------------------------------+--------------+---------+-----------------------------------------------------+--------+---------------------------------------------------------------------+
|  1 | PRIMARY      | tbl_work_item      | index  | PRIMARY,unique_wi_fac,idx_facility,idx_wi                                                       | idx_facility | 4       | NULL                                                |     60 | Using index; Using temporary; Using filesort                        |
|  1 | PRIMARY      | tbl_facility       | eq_ref | PRIMARY                                                                                         | PRIMARY      | 4       | clear21.tbl_work_item.facility_id                   |      1 | Using index                                                         |
|  1 | PRIMARY      | <derived3>         | ref    | <auto_key0>                                                                                     | <auto_key0>  | 4       | clear21.tbl_work_item.work_item_uid                 |   3961 | NULL                                                                |
|  1 | PRIMARY      | tbl_encounter      | eq_ref | PRIMARY                                                                                         | PRIMARY      | 4       | T.encounter_uid                                     |      1 | NULL                                                                |
|  1 | PRIMARY      | <derived2>         | ref    | <auto_key0>                                                                                     | <auto_key0>  | 8       | T.encounter_uid,clear21.tbl_work_item.work_item_uid |   4329 | Using where                                                         |
|  1 | PRIMARY      | <derived9>         | ref    | <auto_key0>                                                                                     | <auto_key0>  | 42      | clear21.tbl_work_item.work_item_uid,T.cpt           |   1074 | Using where                                                         |
|  1 | PRIMARY      | WithMod            | eq_ref | PRIMARY                                                                                         | PRIMARY      | 67      | T.cpt,T.mod1,const                                  |      1 | NULL                                                                |
|  1 | PRIMARY      | WithoutMod         | eq_ref | PRIMARY                                                                                         | PRIMARY      | 67      | T.cpt,const,const                                   |      1 | Using where                                                         |
|  1 | PRIMARY      | bl                 | ref    | cpt_code,work_item_uid,charge_type                                                              | cpt_code     | 121     | T.cpt,const                                         |      1 | Using where; Not exists; Using index                                |
|  9 | DERIVED      | tbl_charge_master  | ref    | uniq_wi_type_date_cpt_cat,charge_type,wi_pt_ft_hcpcs_date,idx_wi_type_dt_cpt,idx_wi_type_cpt_dt | charge_type  | 98      | const                                               | 107469 | Using index condition; Using where; Using temporary; Using filesort |
|  3 | DERIVED      | tbl_event_em_pro   | ALL    | em_level,idx_mid_em_shared                                                                      | NULL         | NULL    | NULL                                                | 236913 | Using where                                                         |
|  4 | UNION        | tbl_event_em_pro   | ALL    | em_level,idx_mid_em_shared                                                                      | NULL         | NULL    | NULL                                                | 236913 | Using where                                                         |
|  5 | UNION        | tbl_event_em_pro   | ALL    | em_level,idx_mid_em_shared                                                                      | NULL         | NULL    | NULL                                                | 236913 | Using where                                                         |
|  6 | UNION        | cpt                | ALL    | idx_enc_uid                                                                                     | NULL         | NULL    | NULL                                                |  13554 | NULL                                                                |
|  6 | UNION        | em                 | ref    | idx_enc_uid                                                                                     | idx_enc_uid  | 4       | clear21.cpt.encounter_uid                           |      1 | Using where                                                         |
|  7 | UNION        | cpt                | ALL    | idx_enc_uid                                                                                     | NULL         | NULL    | NULL                                                |  13554 | NULL                                                                |
|  7 | UNION        | em                 | ref    | idx_enc_uid                                                                                     | idx_enc_uid  | 4       | clear21.cpt.encounter_uid                           |      1 | Using where                                                         |
|  8 | UNION        | cpt                | ALL    | idx_enc_uid                                                                                     | NULL         | NULL    | NULL                                                |  13554 | NULL                                                                |
|  8 | UNION        | em                 | ref    | idx_enc_uid                                                                                     | idx_enc_uid  | 4       | clear21.cpt.encounter_uid                           |      1 | Using where                                                         |
| NULL | UNION RESULT | <union3,4,5,6,7,8> | ALL    | NULL                                                                                            | NULL         | NULL    | NULL                                                |   NULL | Using temporary                                                     |
|  2 | DERIVED      | ed                 | ALL    | idx_enc_uid,encounter_status                                                                    | NULL         | NULL    | NULL                                                | 529111 | Using where; Using temporary; Using filesort                        |
|  2 | DERIVED      | e                  | eq_ref | PRIMARY                                                                                         | PRIMARY      | 4       | clear21.ed.encounter_uid                            |      1 | NULL                                                                |
|  2 | DERIVED      | pro                | ref    | idx_enc_uid,work_item_uid,work_item_uid_2                                                       | idx_enc_uid  | 4       | clear21.ed.encounter_uid                            |      1 | Using where                                                         |
|  2 | DERIVED      | fac                | ref    | idx_enc_uid,work_item_uid,work_item_uid_2,encounter_uid                                         | idx_enc_uid  | 4       | clear21.ed.encounter_uid                            |      1 | Using where                                                         |
+----+--------------+--------------------+--------+-------------------------------------------------------------------------------------------------+--------------+---------+-----------------------------------------------------+--------+---------------------------------------------------------------------+
24 rows in set (0.00 sec)

虽然我从查询中替换了视图以进行一些优化。它的执行速度很慢。

如何修改SQL以将执行时间减少到最少? 任何建议/改进都会对我有所帮助。

表格:tbl_event_em_pro

CREATE TABLE `tbl_event_em_pro` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `encounter_uid` int(11) NOT NULL,
  `em_level` varchar(10) NOT NULL,
  `em_modifier1` varchar(10) DEFAULT NULL,
  `em_modifier2` varchar(10) DEFAULT NULL,
  `em_provider` varchar(75) DEFAULT NULL,
  `em_midlevel` varchar(75) DEFAULT NULL,
  `em_type` varchar(10) DEFAULT NULL,
  `em_downcode` int(1) NOT NULL DEFAULT '0',
  `em_reason` varchar(20) DEFAULT NULL,
  `mdm_total` varchar(20) 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,
  `mid` varchar(75) DEFAULT NULL,
  `dos` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_enc_uid` (`encounter_uid`),
  KEY `em_provider` (`em_provider`),
  KEY `em_midlevel` (`em_midlevel`),
  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 `em_level` (`em_level`),
  KEY `mdm_total` (`mdm_total`),
  KEY `cosigning_physician` (`cosigning_physician`),
  KEY `idx_mid_em_shared` (`em_level`,`mid`,`shared`),
  CONSTRAINT `tbl_event_em_pro_ibfk_1` FOREIGN KEY (`encounter_uid`) REFERENCES `tbl_encounter` (`encounter_uid`),
  CONSTRAINT `tbl_event_em_pro_ibfk_10` FOREIGN KEY (`work_item_uid`) REFERENCES `tbl_work_item` (`work_item_uid`) ON DELETE CASCADE,
  CONSTRAINT `tbl_event_em_pro_ibfk_11` FOREIGN KEY (`userid`) REFERENCES `tbl_user` (`user_email_address`),
  CONSTRAINT `tbl_event_em_pro_ibfk_12` FOREIGN KEY (`work_item_uid`, `encounter_uid`) REFERENCES `tbl_encounter_detail` (`work_item_uid`, `encounter_uid`),
  CONSTRAINT `tbl_event_em_pro_ibfk_15` FOREIGN KEY (`cosigning_physician`) REFERENCES `tbl_provider` (`provider_lov`),
  CONSTRAINT `tbl_event_em_pro_ibfk_2` FOREIGN KEY (`em_provider`) REFERENCES `tbl_provider` (`provider_lov`),
  CONSTRAINT `tbl_event_em_pro_ibfk_3` FOREIGN KEY (`em_midlevel`) REFERENCES `tbl_provider` (`provider_lov`),
  CONSTRAINT `tbl_event_em_pro_ibfk_4` FOREIGN KEY (`qa_procedure_provider`) REFERENCES `tbl_provider` (`provider_lov`),
  CONSTRAINT `tbl_event_em_pro_ibfk_5` FOREIGN KEY (`userid`) REFERENCES `tbl_user` (`user_email_address`),
  CONSTRAINT `tbl_event_em_pro_ibfk_6` FOREIGN KEY (`qa_userid`) REFERENCES `tbl_user` (`user_email_address`),
  CONSTRAINT `tbl_event_em_pro_ibfk_7` FOREIGN KEY (`work_item_uid`) REFERENCES `tbl_work_item` (`work_item_uid`),
  CONSTRAINT `tbl_event_em_pro_ibfk_9` FOREIGN KEY (`encounter_uid`) REFERENCES `tbl_encounter` (`encounter_uid`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=21282825 DEFAULT CHARSET=utf8

表格:tbl_event_cpt_pro

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,
  `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`),
  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
) ENGINE=InnoDB AUTO_INCREMENT=21014137 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:1)

似乎首先,tbl_event_em_pro需要encounter_uid中的索引。请注意,在EXPLAIN中,这些行如何说'ALL'并且行数很多。

LEFT JOIN足够时,请勿使用JOIN;它限制了Optimizer的选择。

如需进一步帮助,我们需要:

  • 更好的缩进,以便我们可以看到连接,联合,子查询等的嵌套结构。

  • SHOW CREATE TABLE至少对于关键表来说。

<强>更新

tbl_charge_master需要INDEX(charge_type, effective_date)

隔离并计时。如果它很慢,我认为,让我们专注于优化它。看看你是否可以摆脱UNION并使用CASE...

        SELECT  work_item_uid, encounter_uid, em_level AS cpt, 1 AS qty,
                em_modifier1 AS mod1, mid AS Prov, 'MLP PRIMARY' AS Role
            FROM  tbl_event_em_pro
            WHERE  em_level <> '0'
              AND  mid <> ''
              AND  shared = 0
            UNION  ALL 
        SELECT  work_item_uid, encounter_uid, em_level AS cpt, 1 AS qty,
                em_modifier1 AS mod1, em_provider, 'PHYSICIAN (NO SEC)'
            FROM  tbl_event_em_pro
            WHERE  em_level <> '0'
              AND  mid <> ''
              AND  shared = 1
            UNION  ALL 
        SELECT  work_item_uid, encounter_uid, em_level AS cpt, 1 AS qty,
                em_modifier1 AS mod1, em_provider, 'PHYSICIAN (NO SEC)'
            FROM  tbl_event_em_pro
            WHERE  em_level <> '0'
              AND  mid = ''
              AND  shared = 0
            UNION  ALL 
        SELECT  cpt.work_item_uid, cpt.encounter_uid, cpt.procedure_num,
                GREATEST(1, cpt.procedure_qty), cpt.procedure_modifier1,
                em.em_provider, 'PHYSICIAN (NO SEC)'
            FROM  tbl_event_cpt_pro cpt
            JOIN  tbl_event_em_pro em USING (encounter_uid)
            WHERE  em.mid <> ''
              AND  em.shared = 1
            UNION  ALL 
        SELECT  cpt.work_item_uid, cpt.encounter_uid, cpt.procedure_num,
                GREATEST(1, cpt.procedure_qty), cpt.procedure_modifier1,
                em.mid, 'MLP PRIMARY'
            FROM  tbl_event_cpt_pro cpt
            JOIN  tbl_event_em_pro em USING (encounter_uid)
            WHERE  em.mid <> ''
              AND  em.shared = 0
            UNION  ALL 
        SELECT  cpt.work_item_uid, cpt.encounter_uid, cpt.procedure_num,
                GREATEST(1, cpt.procedure_qty), cpt.procedure_modifier1,
                em.em_provider, 'PHYSICIAN (NO SEC)'
            FROM  tbl_event_cpt_pro cpt
            JOIN  tbl_event_em_pro em USING (encounter_uid)
            WHERE  em.mid = ''
              AND  em.shared = 0) T USING (work_item_uid , encounter_uid)