Die MySQL-Abfrage kehrt zu langsam zurück
P粉727531237
P粉727531237 2024-03-30 18:25:44
0
1
369

Ich habe eine Anfrage geschrieben. Es funktioniert besser. Aber derzeit haben alle Tabellen 100.000 Zeilen und eine meiner Abfragen gibt zu langsam zurück. Können Sie mir vorschlagen, wie ich meine Abfrage optimieren kann?

select * 
from tbl_xray_information X 
WHERE locationCode = (SELECT t.id 
                      from tbl_location t 
                      where CODE = '202') 
  AND ( communicate_with_pt is NULL || communicate_with_pt='')
  AND x.patientID NOT IN (SELECT patientID 
                          FROM tbl_gxp_information 
                          WHERE center_id = '202')
order by insertedON desc LIMIT 2000

Bitte beachten Sie, dass die „Patienten-ID“ hier Varchar ist.

P粉727531237
P粉727531237

Antworte allen(1)
P粉124890778

可能运行得更快:

select  *
    from  tbl_xray_information AS X
    WHERE  locationCode = 
        ( SELECT  t.id
            from  tbl_location t
            where  CODE = '202'
        )
      AND  ( x.communicate_with_pt is NULL 
          OR x.communicate_with_pt = '' )
      AND  NOT EXISTS ( SELECT 1 FROM tbl_gxp_information
              WHERE x.patientID = patientID
                AND center_id = '202' )
    order by  insertedON desc
    LIMIT  2000

这些索引可能有帮助:

tbl_location:  INDEX(CODE)
tbl_gxp_information:  INDEX(center_id, patientID)  -- (either order)

由于 OR 优化不佳,可能最好为 communicate_with_pt 选择 NULL 或空字符串(以避免对两者进行测试)。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!