在下面的两个sql查询中,建议哪一个更好.单个查询加入或两个简单查询?

San*_*ngi 2 sql postgresql join function

假设A)中的第一个查询结果(envelopecontrolnumber,partnerid,docfileid)=(000000400,31,35)

一个)

select envelopecontrolnumber, partnerid, docfileid 
from envelopeheader 
where envelopeid ='LT01ENV1107010000050';

select count(*) 
from envelopeheader 
where envelopecontrolnumber = '000000400' 
  and partnerid= 31 and docfileid<>35 ;
Run Code Online (Sandbox Code Playgroud)

要么

B)

select count(*)  
from envelopeheader a 
join envelopeheader b on a.envelopecontrolnumber = b.envelopecontrolnumber 
                       and a.partnerid= b.partnerid 
                       and a.envelopeid = 'LT01ENV1107010000050' 
                       and b.docfileid <> a.docfileid;
Run Code Online (Sandbox Code Playgroud)

我在sql函数中使用上面的查询.我在pgAdmin(postgres)中尝试了查询,它显示了16ms(A)和B).当我在pgadmin上分别尝试B)的查询时.它仍然分别为每一个显示16毫秒 - 为B)制作32毫秒 - 这是错误的,因为当你从B中一次运行两个查询时,它显示16毫秒.请建议哪一个更好.我正在使用postgres数据库.

peu*_*feu 5

显示的时间包括以下时间:

  • 将查询发送到服务器
  • 解析查询
  • 计划查询
  • 执行查询
  • 将结果发送回客户端
  • 处理所有结果

尝试像"SELECT 1"这样的简单查询.你可能也会得到16毫秒.

您很可能只是测量服务器的ping时间.

如果您想知道查询使用的服务器上有多少时间,则需要EXPLAIN ANALYZE.