Rumah > Soal Jawab > teks badan
Saya telah menulis pertanyaan. Ia berfungsi lebih baik. Tetapi pada masa ini, semua jadual mempunyai 100K baris, dan salah satu pertanyaan saya kembali terlalu perlahan. Bolehkah anda mencadangkan saya cara mengoptimumkan pertanyaan saya?
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
Sila ambil perhatian bahawa "ID pesakit" di sini ialah varchar.
P粉1248907782024-03-31 00:07:43
Ini mungkin berjalan lebih laju:
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
Indeks ini mungkin membantu:
tbl_location: INDEX(CODE) tbl_gxp_information: INDEX(center_id, patientID) -- (either order)
Memandangkan OR
kurang dioptimumkan, OR
优化不佳,可能最好为 communicate_with_pt
mungkin
communicate_with_pt
(untuk mengelakkan ujian kedua-duanya). 🎜