2016-09-02 11 views
-2

これらの2つのクエリによって得られた結果は同じであると私は信じていますか?これらのSQLクエリの実行時間は同じですか?

最初のクエリ:

SELECT 
    sensor_id, 
    measurement_time, 
    measurement_value 
FROM 
    public.measurement_pm2_5 
    WHERE (sensor_id = 12 AND measurement_time BETWEEN to_timestamp(3000) AND to_timestamp(12000)) 
    OR (sensor_id = 27 AND measurement_time BETWEEN to_timestamp(3000) AND to_timestamp(12000)) 
    OR (sensor_id = 1 AND measurement_time BETWEEN to_timestamp(500) AND to_timestamp(1000)) 
    OR (sensor_id = 1 AND measurement_time BETWEEN to_timestamp(6000) AND to_timestamp(9000)); 

2番目のクエリ:実行時間程度

SELECT 
    sensor_id, 
    measurement_time, 
    measurement_value 
FROM 
    public.measurement_pm2_5 
    WHERE (sensor_id in (12,27) AND measurement_time BETWEEN to_timestamp(3000) AND to_timestamp(12000)) 
    OR (sensor_id = 1 AND ((measurement_time BETWEEN to_timestamp(500) AND to_timestamp(1000)) OR (measurement_time BETWEEN to_timestamp(6000) AND to_timestamp(9000)))); 

どのように?違いはどれほど大きいのですか?

最初のクエリ:

Start-up Cost: 0 
Total Cost: 580.56 
Number of Rows: 1 
Row Width: 18 
Start-up Time: 2.676 
Total Time: 2.676 
Real Number of Rows: 0 
Loops: 1 

Hash Join (cost=0.10..280.06 rows=115 width=18) (actual time=8.596..8.596 rows=0 loops=1) 
    Hash Cond: (p.sensor_id = "*VALUES*".column1) 
    Join Filter: ((p.measurement_time >= to_timestamp(("*VALUES*".column2)::double precision)) AND (p.measurement_time <= to_timestamp(("*VALUES*".column3)::double precision))) 
    Rows Removed by Join Filter: 590 
    -> Seq Scan on measurement_pm2_5 p (cost=0.00..207.39 rows=12439 width=18) (actual time=0.010..2.558 rows=12443 loops=1) 
    -> Hash (cost=0.05..0.05 rows=4 width=12) (actual time=0.017..0.017 rows=4 loops=1) 
     Buckets: 1024 Batches: 1 Memory Usage: 9kB 
     -> Values Scan on "*VALUES*" (cost=0.00..0.05 rows=4 width=12) (actual time=0.002..0.003 rows=4 loops=1) 
Planning time: 0.148 ms 
Execution time: 8.627 ms 

2番目のクエリ:マイクのクエリ@

Start-up Cost: 0 
Total Cost: 456.17 
Number of Rows: 1 
Row Width: 18 
Start-up Time: 2.237 
Total Time: 2.237 
Real Number of Rows: 0 
Loops: 1 

Hash Join (cost=0.10..280.06 rows=115 width=18) (actual time=8.596..8.596 rows=0 loops=1) 
    Hash Cond: (p.sensor_id = "*VALUES*".column1) 
    Join Filter: ((p.measurement_time >= to_timestamp(("*VALUES*".column2)::double precision)) AND (p.measurement_time <= to_timestamp(("*VALUES*".column3)::double precision))) 
    Rows Removed by Join Filter: 590 
    -> Seq Scan on measurement_pm2_5 p (cost=0.00..207.39 rows=12439 width=18) (actual time=0.010..2.558 rows=12443 loops=1) 
    -> Hash (cost=0.05..0.05 rows=4 width=12) (actual time=0.017..0.017 rows=4 loops=1) 
     Buckets: 1024 Batches: 1 Memory Usage: 9kB 
     -> Values Scan on "*VALUES*" (cost=0.00..0.05 rows=4 width=12) (actual time=0.002..0.003 rows=4 loops=1) 
Planning time: 0.148 ms 
Execution time: 8.627 ms 

Hash Join (cost=0.10..280.06 rows=115 width=18) (actual time=8.596..8.596 rows=0 loops=1) 
    Hash Cond: (p.sensor_id = "*VALUES*".column1) 
    Join Filter: ((p.measurement_time >= to_timestamp(("*VALUES*".column2)::double precision)) AND (p.measurement_time <= to_timestamp(("*VALUES*".column3)::double precision))) 
    Rows Removed by Join Filter: 590 
    -> Seq Scan on measurement_pm2_5 p (cost=0.00..207.39 rows=12439 width=18) (actual time=0.010..2.558 rows=12443 loops=1) 
    -> Hash (cost=0.05..0.05 rows=4 width=12) (actual time=0.017..0.017 rows=4 loops=1) 
     Buckets: 1024 Batches: 1 Memory Usage: 9kB 
     -> Values Scan on "*VALUES*" (cost=0.00..0.05 rows=4 width=12) (actual time=0.002..0.003 rows=4 loops=1) 
Planning time: 0.148 ms 
Execution time: 8.627 ms 

質問がある場合、これら二つの間の時間の実行の違いこれらのクエリが大規模なデータベースで行われる場合、クエリは重要ですか?

+2

2つのクエリの実行時間を知りたい場合は、システム上のデータベース上のデータに対してクエリを実行します。それはあなたが求めている質問に対する答えをあなたに与えるでしょう。 –

+1

'explain(analyze)'を使用して実行計画を確認してください –

+0

実行時間が問題でない場合、どうしてあなたに質問していますか? – jarlh

答えて

1

はこれを使用してみてください:

SELECT 
    sensor_id, 
    measurement_time, 
    measurement_value 
FROM 
    public.measurement_pm2_5 p, 
    (values(12,3000,12000),(27,3000,12000),(1,500,1000),(1,6000,9000)) as t(sens,t1,t2) 
    WHERE p.sensor_id = t.sens 
    AND measurement_time BETWEEN to_timestamp(t.t1) AND to_timestamp(t.t2); 

この決定は、通常より速く任意のORIN

1

//ここに

例えば最初のクエリを貼り付けANALYZE EXPLAINより:EXPLAIN ANALYZE select * from employee;

あなたはあなたのクエリとそれぞれのサブクエリがとった時間についての詳細な説明が得られます。

関連する問題