2016-09-02 2 views
-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)); 

두 번째 쿼리

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 

두 번째 쿼리 : 마이크의 쿼리 @

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

두 쿼리의 실행 시간을 알고 싶으면 데이터베이스의 데이터를 시스템에서 실행하십시오. 그것은 당신이 묻고있는 질문에 대한 답을 줄 것입니다. –

+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); 

이 결정은 예를 들어 여기에 첫 번째 쿼리

// ANALYZE 붙여 EXPLAIN 일반적으로 빠른 모든 ORIN

1

보다 : EXPLAIN ANALYZE select * from employee;

을 각 하위 쿼리에서 취한 쿼리 및 시간에 대한 자세한 설명을 제공합니다.

관련 문제