0

EDIT FINAL: 変更されたコードは 5.5 で動作しますが、5.0 では少し異なる行セットが得られます。

次のクエリ

SELECT DISTINCT
  sql_no_cache as job.id,
  job.id as job_id,
  job.job_status,
  job.servee_name,
  kase.plaintiff,
  index_number,
  lawfirm_filenumber,
  DATE_FORMAT(job.datetime_received, '%m/%d/%Y') as date_received,
  DATE_FORMAT(job.date_sent_to_court, '%m/%d/%Y') as date_sent_to_court,
  DATE_FORMAT(kase.date_kase_filed, '%m/%d/%Y') as date_kase_filed,
  TO_DAYS(NOW())-TO_DAYS(kase.date_kase_filed) as thedays,
  DATE_FORMAT(kase.court_datetime, '%m/%d/%Y %h:%i %p') as court_date,
  kase.court_room,
  CONCAT( LUcourt.court_county," - ",LUcourt.court_type) as court_name,
  DATE_FORMAT( job.datetime_served, '%m/%d/%Y') as datetime_served,
  CONCAT( server.namefirst,' ', server.namelast) as server_name

FROM      job 
LEFT JOIN kase        ON job.kase_id = kase.id
LEFT JOIN kasebilling ON kasebilling.kase_id = kase.id
LEFT JOIN server      ON job.server_id = server.id
LEFT JOIN client      ON client.id     = kase.client_id
LEFT JOIN LUcourt     ON LUcourt.id    = kase.court_id

   WHERE job.subscriber_id = 1
ORDER BY job_id + 0
   LIMIT 0,500;

</pre>
i modified the query but it wont run ...

    SELECT DISTINCT sql_no_cache  job.id, job.id as job_id, job.job_status, job.servee_name, kase.plaintiff, index_number, lawfirm_filenumber, DATE_FORMAT(job.datetime_received, '%m/%d/%Y') as date_received, DATE_FORMAT(job.date_sent_to_court, '%m/%d/%Y') as date_sent_to_court, DATE_FORMAT(kase.date_kase_filed, '%m/%d/%Y') as date_kase_filed, TO_DAYS(NOW())-TO_DAYS(kase.date_kase_filed) as thedays, DATE_FORMAT(kase.court_datetime, '%m/%d/%Y %h:%i %p') as court_date, kase.court_room, CONCAT(LUcourt.court_county," - ",LUcourt.court_type) as court_name, DATE_FORMAT(job.datetime_served, '%m/%d/%Y') as datetime_served, concat(server.namefirst,' ', server.namelast) as server_name FROM job 
    LEFT JOIN kase ON job.kase_id = kase.id
    LEFT JOIN kasebilling  ON kasebilling.kase_id = kase.id
    LEFT JOIN server ON job.server_id = server.id
    left join client on client.id=kase.client_id
    left join LUcourt on LUcourt.id=kase.court_id  WHERE job.subscriber_id = 1 and job_id in (select distinct job.id from job where job.subscriber_id = '1' order by id+0) LIMIT 0,500
</pre>

上記のクエリを変更して、テーブル名の一部を削除しました...ただし、クエリで注文を削除すると、非常に高速になります...制限500だけでなく、すべての行で注文すると思います... どうすれば order by でこれをより速く動作させることができますか。どんな助けでも感謝します....

PS:order_byでファイルソートを使用しています...

説明:

+----+-------------+-------------+--------+------- -------------------------------------------------- ------------------------------+-------------------------------- --------------+---------------------+-------------- ------+--------------------+---------------------------------------------- ---------------+
| | ID | select_type | テーブル | タイプ | 可能な_キー | キー | key_len | 参照 | 行 | 行 エクストラ |
+----+-------------+-------------+--------+------- -------------------------------------------------- ------------------------------+-------------------------------- --------------+---------------------+-------------- ------+--------------------+---------------------------------------------- ---------------+
| | 1 | シンプル | 仕事 | 仕事 | 参照 | Subscriber_id,subscriber_id-job_loc-client_loc-server_loc,sub_id_idx_serve_name_idx | subscriber_id-job_loc-client_loc-server_loc | 4 | 定数 | 362381 | where を使用します。一時的な使用; ファイルソートの使用 |
| | 1 | シンプル | かせ | eq_ref | プライマリ | プライマリ | 4 | pserve.job.kase_id | 1 | | |
| | 1 | シンプル | カセビリング | 参照 | kase_id | kase_id | 4 | pserve.kase.id | 2 | インデックスの使用 |
| | 1 | シンプル | サーバー | eq_ref | プライマリ | プライマリ | 4 | pserve.job.server_id | 1 | | |
| | 1 | シンプル | クライアント | eq_ref | プライマリ | プライマリ | 4 | pserve.kase.client_id | 1 | インデックスの使用 |
| | 1 | シンプル | ルクール | eq_ref | プライマリ | プライマリ | 4 | pserve.kase.court_id | 1 | | |
+----+-------------+-------------+--------+------- -------------------------------------------------- ------------------------------+-------------------------------- --------------+---------------------+-------------- ------+--------------------+---------------------------------------------- ---------------+

クエリを変更しましたが、実行されません...

SELECT DISTINCT sql_no_cache  job.id, job.id as job_id, job.job_status, job.servee_name, kase.plaintiff, index_number, lawfirm_filenumber, DATE_FORMAT(job.datetime_received, '%m/%d/%Y') as date_received, DATE_FORMAT(job.date_sent_to_court, '%m/%d/%Y') as date_sent_to_court, DATE_FORMAT(kase.date_kase_filed, '%m/%d/%Y') as date_kase_filed, TO_DAYS(NOW())-TO_DAYS(kase.date_kase_filed) as thedays, DATE_FORMAT(kase.court_datetime, '%m/%d/%Y %h:%i %p') as court_date, kase.court_room, CONCAT(LUcourt.court_county," - ",LUcourt.court_type) as court_name, DATE_FORMAT(job.datetime_served, '%m/%d/%Y') as datetime_served, concat(server.namefirst,' ', server.namelast) as server_name FROM job 
LEFT JOIN kase ON job.kase_id = kase.id
LEFT JOIN kasebilling  ON kasebilling.kase_id = kase.id
LEFT JOIN server ON job.server_id = server.id
left join client on client.id=kase.client_id
left join LUcourt on LUcourt.id=kase.court_id  WHERE job.subscriber_id = 1 and job_id in (select distinct job.id from job where job.subscriber_id = '1' order by id+0) LIMIT 0,500
4

1 に答える 1

0

最初の醜い試み: 条件job.subscriber_id = '1'を追加しLEFT JOIN jobて、ORDER および LIMIT される合計行数を減らしますが、WHERE 部分にも保持します。

FROM      kase
LEFT JOIN job     ON k.kase_id   = kase.id
                 AND job.subscriber_id = '1' /* condition copied here ... */
LEFT JOIN kb      ON kb.kase_id  = kase.id
LEFT JOIN server  ON job.server_id = server.id
LEFT JOIN client  ON client.id   = kase.client_id
LEFT JOIN LUcourt ON LUcourt.id  = kase.court_id
    WHERE job.subscriber_id = '1'            /* ... but also kept here */
 ORDER BY job.id
    LIMIT 0,500;

2 番目の試行は、はるかに醜い: 最初の 500 kase*のjob組み合わせをサブ選択し、サブ選択された行に対して他のテーブルを LEFT JOIN します。

FROM
( SELECT * /* you probably have to list & alias column names if necessary */
  FROM      kase
  LEFT JOIN job   ON k.kase_id   = kase.id
      WHERE job.subscriber_id = '1'
   ORDER BY job.id
      LIMIT 0,500
) AS kase_job
/* Now we have 500 rows*/

LEFT JOIN kb      ON kb.kase_id  = kase_job.id
LEFT JOIN server  ON kase_job.server_id = server.id
LEFT JOIN client  ON client.id   = kase_job.client_id
LEFT JOIN LUcourt ON LUcourt.id  = kase_job.court_id

/* If you do `(500 rows) LEFT JOIN whatever` you can be sure you have
   at least 500 rows (never less) here, so you have to ORDER/LIMIT then again
*/

 ORDER BY kase_job.id
    LIMIT 0,500;
于 2012-09-10T19:56:59.203 に答える