关于LEFT JOIN查询性能

yousite1 2014-09-18 04:12:46
有两个a,b
需要进行LEFT JOIN关联查询,A,B表记录数大概都在100W左右。
SQL查询原形为:

select an.time an_time,an.host an_host,an.tid an_tid,
an.client_ip an_client_ip,an.client_port an_client_port,
an.server_ip an_server_ip, an.server_port an_server_port,
an.up an_up, an.down an_down, an.connect_time an_connect_time,
an.disconnect_time an_disconnect_time, an.duration an_duration,
an.target_type an_target_type,an.target_ip an_target_ip, an.target_port an_target_port,
an.syn_rtt an_syn_rtt,an.error_info an_error_info,
bn.time bn_time,bn.host bn_host,bn.tid bn_tid,
bn.client_ip bn_client_ip,bn.client_port bn_client_port,
bn.server_ip bn_server_ip, bn.server_port bn_server_port,
bn.up bn_up, bn.down bn_down, bn.connect_time bn_connect_time,
bn.disconnect_time bn_disconnect_time, bn.duration bn_duration,
bn.target_type bn_target_type,bn.target_ip bn_target_ip, bn.target_port bn_target_port,
bn.syn_rtt bn_syn_rtt,bn.error_info bn_error_info
from logsys.a an left join
logsys.b bn on an.tid = bn.tid and an.target_ip = bn.target_ip

where an.time >= 0 and an.time < 1408515318001 and bn.syn_rtt >= 1 and bn.syn_rtt < 10000
and bn.down >= 100 and bn.down < 1000 and (an.host = 0 or bn.host= 0);
...全文
428 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yousite1 2014-09-19
  • 打赏
  • 举报
回复
我了个去代码发错了。 应该是:


select an.*,
bn.time bn_time,bn.host bn_host,bn.tid bn_tid,
 bn.client_ip bn_client_ip,bn.client_port bn_client_port,
bn.server_ip bn_server_ip, bn.server_port bn_server_port,
bn.up bn_up, bn.down bn_down, bn.connect_time bn_connect_time,
bn.disconnect_time bn_disconnect_time, bn.duration bn_duration,
bn.target_type bn_target_type,bn.target_ip bn_target_ip, bn.target_port bn_target_port,
bn.syn_rtt bn_syn_rtt,bn.error_info bn_error_info
 from (
	select an.time an_time,an.host an_host,an.tid an_tid,
	an.client_ip an_client_ip,an.client_port an_client_port,
	an.server_ip an_server_ip, an.server_port an_server_port,
	an.up an_up, an.down an_down, an.connect_time an_connect_time,
	an.disconnect_time an_disconnect_time, an.duration an_duration,
	an.target_type an_target_type,an.target_ip an_target_ip, an.target_port an_target_port,
	an.syn_rtt an_syn_rtt,an.error_info an_error_info
			from logsys.A an where  an.time >= 0 and an.time < 1408515328001
	) an 
	left join logsys.B  bn on an_tid = bn.tid 
	
	where  bn.syn_rtt >= 1 and bn.syn_rtt < 10000
	and bn.down >= 100 and bn.down < 1000 and (an_host = 0 or bn.host= 0);
	
yousite1 2014-09-19
  • 打赏
  • 举报
回复
改成子查询的方式是否能好一些。

 select an.time an_time,an.host an_host,an.tid an_tid,
	an.client_ip an_client_ip,an.client_port an_client_port,
	an.server_ip an_server_ip, an.server_port an_server_port,
	an.up an_up, an.down an_down, an.connect_time an_connect_time,
	an.disconnect_time an_disconnect_time, an.duration an_duration,
	an.target_type an_target_type,an.target_ip an_target_ip, an.target_port an_target_port,
	an.syn_rtt an_syn_rtt,an.error_info an_error_info,
	bn.time bn_time,bn.host bn_host,bn.tid bn_tid,
	bn.client_ip bn_client_ip,bn.client_port bn_client_port,
	bn.server_ip bn_server_ip, bn.server_port bn_server_port,
	bn.up bn_up, bn.down bn_down, bn.connect_time bn_connect_time,
	bn.disconnect_time bn_disconnect_time, bn.duration bn_duration,
	bn.target_type bn_target_type,bn.target_ip bn_target_ip, bn.target_port bn_target_port,
	bn.syn_rtt bn_syn_rtt,bn.error_info bn_error_info
from logsys.A an left join
	logsys.B bn on an.tid = bn.tid and an.target_ip = bn.target_ip
	
	where an.time >= 0 and an.time < 1408515318001 and bn.syn_rtt >= 1 and bn.syn_rtt < 10000
	and bn.down >= 100 and bn.down < 1000 and (an.host = 0 or bn.host= 0);
用子查询的方式,主要是可以把A表的记录数减到很少,因为在里面按时间过滤掉了,是否这样会改善很多。
ACMAIN_CHM 2014-09-18
  • 打赏
  • 举报
回复
引用 3 楼 yousite1 的回复:
不能,左边A表的记录必须有的,bn.down>=100是对查询结果进行过滤。
建议做个测试,理论上这样写是不可能的。 B表中没有记录的A表记录无法满足条件,也就不会列出。
yousite1 2014-09-18
  • 打赏
  • 举报
回复
不能,左边A表的记录必须有的,bn.down>=100是对查询结果进行过滤。
ACMAIN_CHM 2014-09-18
  • 打赏
  • 举报
回复
既然有 bn.down >= 100 那么 left join 就没什么意义了。 直接用 inner join 就行了。
yousite1 2014-09-18
  • 打赏
  • 举报
回复
请问这个数据量级查询性能会是怎样的? 如果两表的数据量是500W呢?查询时间大概多久?

972

社区成员

发帖
与我相关
我的任务
社区描述
PostgreSQL相关内容讨论
sql数据库数据库架构 技术论坛(原bbs)
社区管理员
  • PostgreSQL社区
  • yang_z_1
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧