简体   繁体   中英

Alternate for Indexing

We need result from Table based on datetime filter. But it working slow in mysql. I can't implement indexing on date & timestamp columns due to it will slow our insertion/updation. So can you please suggest any alternate for select the data quickly based on date and datetime filter with better performance.

SQL Query :

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 Column has indexing in the table

Get rid of

SELECT   *
FROM     (
         )t

it adds nothing, except to slow things down.

Let's focus on

  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

Does that shortened query run "too slow"? If not, then we can use that as a derived table to see if that will speed it up. If so, then we will need to get into UNION and indexing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM