4

したがって、インデックスのないデータベースを処理する必要があります(私の設計ではなく、それは私を苛立たせます)。戻るのに約3秒かかるクエリを実行していますが、もっと高速にする必要があります。

関連するテーブルと列は次のとおりです。

gs_pass_data          au_entry            ground_station
  -gs_pass_data_id      -au_id              -ground_station_id
  -start_time           -gs_pass_data_id    -ground_station_name
  -end_time             -comments
  -ground_station_id

そして私の質問は:

SELECT DISTINCT gs_pass_data_id,start_time,end_time,
  ground_station_name FROM gs_pass_data 
  JOIN ground_station
  ON gs_pass_data.ground_station_id =
  ground_station.ground_station_id 
  JOIN au_entry ON au_entry.gs_pass_data_id =
  gs_pass_data.gs_pass_data_id
WHERE (start_time BETWEEN @prevTime AND @nextTime) 
  AND comments = 'AU is identified.'
  ORDER BY start_time

DISTINCTの代わりにEXISTSを使用してみましたが、改善はありません。SQLの最適化についてできる限りのことを読みましたが、このクエリを妥当な時間(0.5秒未満であることが妥当)まで下げることができないようです。任意のアイデアをいただければ幸いです。

4

4 に答える 4

12

インデックスがなければ、あなたはうんざりしています。DBエンジンは、毎回、毎回、全表スキャンを実行する必要があります。クエリをいじるのは、タイタニック号のデッキチェアを並べ替えるだけです。データが蓄積するにつれてさらに悪化する前に、今すぐDBを修正してください。

于 2011-08-25T16:04:55.993 に答える
5

クエリは、個別のなしで、代わりにgroupbyを使用して作成することもできます。ただし、おそらくまったく違いはありません。標準的なアドバイスは他の人と同じです。インデックスを追加し、'順序を`で削除して、@MarcBに+1します。

SELECT gs_pass_data_id,start_time,end_time,ground_station_name 
  FROM gs_pass_data 
  JOIN ground_station
    ON gs_pass_data.ground_station_id = ground_station.ground_station_id 
  JOIN au_entry 
    ON au_entry.gs_pass_data_id = gs_pass_data.gs_pass_data_id
 WHERE (start_time BETWEEN @prevTime AND @nextTime) 
   AND comments = 'AU is identified.'
 GROUP BY gs_pass_data_id,start_time,end_time,ground_station_name 
 ORDER BY start_time
于 2011-08-25T16:51:55.943 に答える
2

テーブルにインデックスを作成できないので...インデックス付きビューを作成する権限がありますか?

SQL 2005- http://technet.microsoft.com/en-us/library/cc917715.aspx

SQL 2008- http://msdn.microsoft.com/en-us/library/dd171921(v= sql.100 ).aspx

それはあなたにインデックスの利点を与えるでしょうが、元のテーブルを変更することはありません...

于 2011-08-25T17:06:06.393 に答える
1

あなたは次のことを試すことができます、私はあなたが他に何ができるかわかりません、またはこれがそれを少しでも速くするかどうか:/

SELECT DISTINCT gs_pass_data_id,start_time,end_time,ground_station_name 
  FROM
  (
    -- My idea is to make this first table as small as possible first, which will then make the joins quicker (TM)
    SELECT *
    FROM gs_pass_data
    WHERE (start_time BETWEEN @prevTime AND @nextTime)
  ) t
  INNER JOIN ground_station ON gs_pass_data.ground_station_id = ground_station.ground_station_id 
  INNER JOIN 
  (
    -- Same as above
    SELECT *
    FROM au_entry
    WHERE comments = N'AU is identified.' -- Make sure comments is the same type as the text string. You said nvarchar so make the string your searching by nvarchar
  ) t2  ON au_entry.gs_pass_data_id = gs_pass_data.gs_pass_data_id
ORDER BY start_time

-- OR TRY THIS

SELECT DISTINCT gs_pass_data_id,start_time,end_time,ground_station_name 
  FROM
  (
    -- My idea is to make this first table as small as possible first, which will then make the joins quicker (TM)
    SELECT *
    FROM gs_pass_data
    WHERE (start_time BETWEEN @prevTime AND @nextTime)
  ) t
  INNER JOIN ground_station ON gs_pass_data.ground_station_id = ground_station.ground_station_id 
  INNER JOIN au_entry ON au_entry.gs_pass_data_id = gs_pass_data.gs_pass_data_id
  WHERE comments = N'AU is identified.' -- Make sure comments is the same type as the text string. You said nvarchar so make the string your searching by nvarchar
ORDER BY start_time
于 2011-08-25T16:24:49.063 に答える