2017-10-04 21 views
0

クライアントデータベースからデータベースへデータを取得するためにクエリを実行しようとしていますが、クエリを実行するのに多くの時間がかかります。以下user_appoint.u_idからuser_appoint.idは私のクエリ実行時間が長くかかるクエリ

SELECT 
CONCAT('D',user_appoint.`id`) AS ApptId, 
user_appoint.`u_id`, 
tbl_questions.CandAns, 
tbl_questions.ExamAns, 
tbl_questions.QueNote, 
CONCAT("[",GROUP_CONCAT(CONCAT('"',`tbl_investigations`.`test_id`,'":"',tbl_investigations.`result`,'"')),"]") AS CandInv, 
CONCAT("[",GROUP_CONCAT(CONCAT('"',`tbl_investigations`.`test_id`,'":"',tbl_investigations.`comments`,'"')),"]") AS IntComm, 
IF(tbl_questions.LastUpdatedDateTime>MAX(tbl_investigations.`ModifiedAt`),tbl_questions.LastUpdatedDateTime,MAX(tbl_investigations.`ModifiedAt`)) AS LastUpdatedDateTime, 
CONCAT('D',user_appoint.`id`) AS UniqueId 
FROM user_appoint 
LEFT JOIN tbl_investigations ON tbl_investigations.`appt_id`=user_appoint.`id` AND tbl_investigations.`ModifiedAt`>'2011-01-01 00:00:00' 
LEFT JOIN tbl_questions ON tbl_questions.`appt_id` =user_appoint.`id` AND tbl_questions.`LastUpdatedDateTime`>'2011-01-01 00:00:00' 
GROUP BY user_appoint.`id` 
HAVING LastUpdatedDateTime>'2011-01-01 00:00:00' 
ORDER BY user_appoint.`u_id` 
LIMIT 0, 2000; 

user_appoint.u_idが適切にインデックス化されています。

答えて

0

クエリの実行計画を確認してください。そして、元の質問と常に計画を共有する方が良いです。 ApptId、user_appoint AS

説明フォーマット= JSON

SELECT CONCAT( 'D'、user_appoint。id)。 u_id、 tbl_questions.CandAns、tbl_questions.ExamAns、tbl_questions.QueNote、 CONCAT( "["、GROUP_CONCAT(CONCAT( ' "'、tbl_investigationstest_id、。 ' ":"'。、tbl_investigations result、 '"'))、 "]") CandInv、 CONCAT AS( "["、GROUP_CONCAT(CONCAT( ' ' "'、tbl_investigationstest_id、 ":"'。、tbl_investigations comments、 '"'))、 "]") IntComm AS 、 IF(tbl_questions.LastUpdatedDateTime> MAX(tbl_investigations。ModifiedAt)、tbl_questions.LastUpdatedDateTime、MAX(tbl_investigations。ModifiedAt))LastUpdatedDateTime、CONCAT( 'D'、user_appoint。id)AS 一意ID AS FROM user_appoint LEFT JOIN tbl_investigations ON tbl_investigations。 appt_id = user_appoint。 id AND tbl_investigations。 ModifiedAt> '2011-01-01 00:00:00' LEFT JOIN tbl_questions on tbl_questions。 appt_id = user_appoint。 idおよび tbl_questions。 LastUpdatedDateTime> '2011-01-01 00:00:00' GROUP BY user_appoint。 id HAVING LastUpdatedDateTime> '2011-01-01 00:00:00' ORDER BY user_appoint。 u_id LIMIT 0、2000;

0

あなたの問合せを見ると、私は大量の連結、集約関数を見て、結合は単一の問合せで実行されています。

これらの操作は、クエリの実行制限を設定したので、すべての2000レコードに対して実行されます。 これにより、クエリの実行が遅くなる可能性があります。

0

あなたは異なる別名

と2つの同一の列を持っている
CONCAT('D',user_appoint.`id`) AS ApptId, 
CONCAT('D',user_appoint.`id`) AS UniqueId 

(変更)と仮定すると、NULL値は、値がNULLで任意の悪影響を克服するだろう)(その後、最大を比較し、これらの日付列に発生する可能性があります。

if(max(tbl_questions.lastupdateddatetime) > max(tbl_investigations.`modifiedat`) , max(tbl_questions.lastupdateddatetime), max(tbl_investigations.`modifiedat`)) AS LastUpdatedDateTime 

これを試してみてくださいただし

SELECT * 
FROM (
    SELECT 
      Concat('D', user_appoint.`id`) AS ApptId 
      , user_appoint.`u_id` 
      , tbl_questions.candans 
      , tbl_questions.examans 
      , tbl_questions.quenote 
      , Concat("[", Group_concat(Concat('"', `tbl_investigations`.`test_id`, '":"', tbl_investigations.`result`, '"')), "]") AS CandInv 
      , Concat("[", Group_concat(Concat('"', `tbl_investigations`.`test_id`, '":"', tbl_investigations.`comments`, '"')), "]") AS IntComm 
      , if(max(tbl_questions.lastupdateddatetime) > max(tbl_investigations.`modifiedat`) , max(tbl_questions.lastupdateddatetime), max(tbl_investigations.`modifiedat`)) AS LastUpdatedDateTime 
      , Concat('D', user_appoint.`id`) AS UniqueId 
    FROM user_appoint 
      LEFT JOIN tbl_investigations 
        ON tbl_investigations.`appt_id` = user_appoint.`id` 
        AND tbl_investigations.`modifiedat` > '2011-01-01 00:00:00' 
      LEFT JOIN tbl_questions 
        ON tbl_questions.`appt_id` = user_appoint.`id` 
        AND tbl_questions.`lastupdateddatetime` > '2011-01-01 00:00:00' 
    GROUP BY user_appoint.`id` 
    HAVING lastupdateddatetime > '2011-01-01 00:00:00' 
    ) d 
ORDER BY `u_id` 
LIMIT 0, 2000 
; 

現在使用されていない非標準形式のGROUP BY句が使用されています。 MySQLはこの奇妙な状況を許して生活を始めました。そこでは多くの列を選択できますが、そのうちの1つだけをグループ化することができます。これは、SQLでは全く標準ではありません。

最近のバージョンのMySQLでは、デフォルト設定が変更されており、GROUP BY句で1つの列を使用するとエラーが発生します。

だから、あなたはこれらのどれもが、パフォーマンス(テキストとして)実行プランを提供してくださいを改善しない場合は

GROUP BY 
     user_appoint.`id` 
     , user_appoint.`u_id` 
     , tbl_questions.candans 
     , tbl_questions.examans 
     , tbl_questions.quenote 

にあなたはグループ化を実行する方法を変更する必要があります。

関連する問題