替代索引

时间:2019-04-16 10:24:53

标签: mysql indexing

我们需要基于日期时间过滤器的表格结果。但是它在mysql中工作缓慢。我无法在日期和时间戳列上实现索引,因为这会减慢我们的插入/更新速度。因此,您能否建议任何替代方法,以便根据日期和日期时间过滤器快速选择数据,从而获得更好的性能。

SQL查询:

SELECT   *
FROM     (
          SELECT  id
                  , title
                  , language
                  , lang_code
                  , financial
                  , fname
                  , lname
                  , mname
                  , mname_br
                  , suffix
                  , CASE WHEN DOB='0000-00-00' THEN NULL ELSE DOB END AS DOB
                  , street
                  , street2
                  , postal_code
                  , zip_ext
                  , city
                  , state
                  , country_code
                  , phone_home
                  , phone_biz
                  , phone_biz_ext
                  , phone_contact
                  , phone_cell
                  , status
                  , CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE
                    CAST(date as datetime) END AS date
                  , sex
                  , referrer
                  , referrerID
                  , providerID
                  , ethnoracial
                  , pid
                  , temp_key
                  , primary_care
                  , default_facility
                  , created_by
                  , patientStatus
                  , primary_care_id
                  , Sec_HCFA
                  , noBalanceBill
                  , erx_entry
                  , erx_patient_id
                  , athenaID
                  , CASE WHEN licenseDate='0000-00-00 00:00:00' THEN NULL ELSE  licenseDate end as licenseDate
                  , race
                  , otherRace
                  , ethnicity
                  , otherEthnicity
                  , primary_care_phy_name
                  , primary_care_phy_id
                  , CASE WHEN dod_patient='0000-00-00' THEN NULL ELSE dod_patient END AS dod_patient-- 
                  , locked-- 
                  , co_man_phy-- 
                  , co_man_phy_id-- 
                  , vip-- 
                  , External_MRN_1-- 
                  , External_MRN_2-- 
                  , External_MRN_3-- 
                  , External_MRN_4
                  , as_id
                  , CASE WHEN acc_statement_date='0000-00-00' THEN acc_statement_date END AS acc_statement_date
                  , CASE WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS timestamp
                  , api_id
                  , fmh_pt_status
                  , race_code
                  , ethnicity_code
                  , patient_payer
                  , CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS transfer_created
                  ,CASE  WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS transfer_updated
                  ,CASE  WHEN date > '2020-11-10 00:00:00' THEN 'new' ELSE 'changed' END AS flagfield
                  ,CASE  WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS sortdate
          FROM patient_data
          WHERE (date > '2020-11-10 00:00:00' or timestamp > '2019-04-01 19:53:57-04')
          AND month(date) > 0)t
          ORDER BY flagfield desc,
                  sortdate;
         )

id列在表中具有索引

1 个答案:

答案 0 :(得分:0)

摆脱

SELECT   *
FROM     (
         )t

除了减慢速度外,它什么也没添加。

让我们专注于

  SELECT id
      FROM patient_data
      WHERE (date > '2020-11-10 00:00:00'
           or timestamp > '2019-04-01 19:53:57-04')
      AND month(date) > 0

缩短查询的时间是否“太慢”?如果不是,那么我们可以将其用作派生表,以查看是否可以加快速度。如果是这样,那么我们将需要进入UNION并建立索引。