这些SQL查询的执行时间相同吗?

这些SQL查询的执行时间相同吗?,sql,postgresql,query-optimization,Sql,Postgresql,Query Optimization,我相信这两个查询得到的结果是一样的 第一个查询: 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 measureme

我相信这两个查询得到的结果是一样的

第一个查询:

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));
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
第二个问题:

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: 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
执行时间呢?差异有多大(如果有)

第一个查询:

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));
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
启动成本:0
总费用:580.56
行数:1
行宽:18
启动时间:2.676
总时间:2.676
实际行数:0
循环:1
散列联接(成本=0.10..280.06行=115宽度=18)(实际时间=8.596..8.596行=0循环=1)
散列条件:(p.sensor_id=“*值*”。第1列)
连接筛选器:((p.measurement\u time>=to\u timestamp(“*VALUES*”.column2::双精度))和(p.measurement\u time Seq扫描测量值\u pm2\u 5p(成本=0.00..207.39行=12439宽度=18)(实际时间=0.010..2.558行=12443循环=1)
->散列(成本=0.05..0.05行=4宽度=12)(实际时间=0.017..0.017行=4循环=1)
存储桶:1024批:1内存使用量:9kB
->值扫描“*值*”(成本=0.00..0.05行=4宽度=12)(实际时间=0.002..0.003行=4圈=1)
计划时间:0.148毫秒
执行时间:8.627毫秒
第二个问题:

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: 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
启动成本:0
总费用:456.17
行数:1
行宽:18
启动时间:2.237
总时间:2.237
实际行数:0
循环:1
散列联接(成本=0.10..280.06行=115宽度=18)(实际时间=8.596..8.596行=0循环=1)
散列条件:(p.sensor_id=“*值*”。第1列)
连接筛选器:((p.measurement\u time>=to\u timestamp(“*VALUES*”.column2::双精度))和(p.measurement\u time Seq扫描测量值\u pm2\u 5p(成本=0.00..207.39行=12439宽度=18)(实际时间=0.010..2.558行=12443循环=1)
->散列(成本=0.05..0.05行=4宽度=12)(实际时间=0.017..0.017行=4循环=1)
存储桶:1024批:1内存使用量:9kB
->值扫描“*值*”(成本=0.00..0.05行=4宽度=12)(实际时间=0.002..0.003行=4圈=1)
计划时间:0.148毫秒
执行时间:8.627毫秒
@迈克的问题:

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连接(成本=0.10..280.06行=115宽度=18)(实际时间=8.596..8.596行=0循环=1)
散列条件:(p.sensor_id=“*值*”。第1列)
连接筛选器:((p.measurement\u time>=to\u timestamp(“*VALUES*”.column2::双精度))和(p.measurement\u time Seq扫描测量值\u pm2\u 5p(成本=0.00..207.39行=12439宽度=18)(实际时间=0.010..2.558行=12443循环=1)
->散列(成本=0.05..0.05行=4宽度=12)(实际时间=0.017..0.017行=4循环=1)
存储桶:1024批:1内存使用量:9kB
->值扫描“*值*”(成本=0.00..0.05行=4宽度=12)(实际时间=0.002..0.003行=4圈=1)
计划时间:0.148毫秒
执行时间:8.627毫秒
问题是,当在大型数据库上执行这些查询时,这两个查询之间的执行时间差异是否显著?

请尝试使用以下方法:

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

此决定通常比中的任何
更快

解释分析//将第一个查询粘贴到此处

例如:
EXPLAIN ANALYZE select*from employee;


您将获得有关您的查询以及每个子查询所用时间的详细解释。

如果您想知道两个查询的执行时间,请在您的数据、数据库和系统上运行查询。这将为您提出的问题提供答案。使用
explain(analyze)检查执行计划
向我们展示两个执行计划-否则这是不可能回答的-执行计划完全相同,因此如果行数发生变化,我不会期望它们之间有任何差异-但这只能在您针对具有更多数据的数据库实际运行它们时才能回答。查询优化器的选择无法预测r未知数据集