2017-12-19 8 views
0

私はSQLite Databaseを持っています。ここでは、次のような2つのテーブルからデータを取得します。querySQLite | 2つの(スキーマ)同一テーブルのデータの取得

select ie.* 
from (select * 
     from History where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
     union all 
     select * 
     from Pending where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
    ) ie 
order by TimeStampCome desc LIMIT 100 OFFSET 1 

これは最高のパフォーマンスを発揮する方法ですか?私はにalarm objectsを保存しました。したがって、100万を超えるエントリが簡単に存在する可能性があります。あなたの特定のクエリに対する

答えて

1

、最初に、このように各テーブルから行を制限するために、おそらくより良いです:

with h as (
     select h.* 
     from history h 
     where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
     order by TimeStampCome desc 
     limit 101 
    ), 
    p as (
     select p.* 
     from pending p 
     where Station = @station and TimeStampCome <= @till and TimeStampCome >= @from 
     order by TimeStampCome desc 
     limit 101 
    ) 
select pe.* 
from (select h.* from h union all 
     select p.* from p 
    ) pe 
order by TimeStampCome desc 
limit 100 offset 1; 

あなたが異なるオフセットで作業を開始した場合ただし、これはあまり可能となります。

パフォーマンスが問題になる場合は、両方のテーブルの(Station, TimeStampCome)でインデックスを開始してください。

+0

ああ、これはヒント 'indexes'です!私は、提出されたフィルタ(ステーション、から、まで)に応じてクエリを構築します。そうすれば、このような問題は発生しません。 –

関連する問題