即使沒有order by
下面的查詢是非常緩慢,我不明白爲什麼。我猜這是where date_affidavit_file
,但我怎樣才能讓它快速與order by
以及?也許是job_id匹配的地方,然後將其傳遞到代碼的其餘部分,但我仍然需要通過服務器來訂購像這樣的服務器名稱。有什麼建議麼?mysql緩慢複雜的查詢與訂單
explain select sql_no_cache court_county, job.id as jid, job_status,
DATE_FORMAT(job.datetime_served, '%m/%d/%Y') as dserved ,
CONCAT(server.namefirst, ' ', server.namelast) as servername, client_name,
DATE_FORMAT(job.datetime_received, '%m/%d/%Y') as dtrec ,
DATE_FORMAT(job.datetime_give2server, '%m/%d/%Y') as dtg2s,
DATE_FORMAT(date_kase_filed, '%m/%d/%Y') as dkf,
DATE_FORMAT(job.date_sent_to_court, '%m/%d/%Y') as dtstc ,
TO_DAYS(datetime_served)-TO_DAYS(date_kase_filed) as totaldays from job
left join kase on kase.id=job.kase_id
left join server on job.server_id=server.id
left join client on kase.client_id=client.id
left join LUcourt on LUcourt.id=kase.court_id
where date_affidavit_filed is not null and date_affidavit_filed !='' order by servername;
+----+-------------+---------+--------+----------------------+---------+---------+-----------------------+--------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+--------+----------------------+---------+---------+-----------------------+--------+----------------------------------------------+ | 1 | SIMPLE | job | ALL | date_affidavit_filed | NULL | NULL | NULL | 365212 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | kase | eq_ref | PRIMARY | PRIMARY | 4 | pserve.job.kase_id | 1 | | | 1 | SIMPLE | server | eq_ref | PRIMARY | PRIMARY | 4 | pserve.job.server_id | 1 | | | 1 | SIMPLE | client | eq_ref | PRIMARY | PRIMARY | 4 | pserve.kase.client_id | 1 | | | 1 | SIMPLE | LUcourt | eq_ref | PRIMARY | PRIMARY | 4 | pserve.kase.court_id | 1 | | +----+-------------+---------+--------+----------------------+---------+---------+-----------------------+--------+----------------------------------------------+
你的桌子上是否有索引? – andrewsi
這可能是http://dba.stackexchange.com/而不是StackOverflow的問題。 – Fluffeh
您正在使用左連接。你期望有任何'null'值嗎? – Arjan