[英]Joining two tables in SQL Server when one table has huge data and other has few data
If I want to join two tables (not inner join), Left table has huge data (millions of record), and right table has few records. 如果我要联接两个表(而不是内部联接),则左表具有巨大的数据(数百万条记录),而右表具有很少的记录。 What should I prefer (Left or Right Outer join) and why.
我应该选择哪种(左或右外部联接)以及原因。
Well first of all That join is independent of the size of the tables 首先,该联接与表的大小无关
Well I think this depends what data you want either from Left table or from right table let us assume you have two tables Employees which has millions of records let us put this in right and Department which has 10 record put it in left Now each employee has one department. 好吧,我认为这取决于您要从“左”表还是“右”表中获取什么数据,让我们假设您有两个表,拥有数百万条记录的员工将其放到右边,将拥有10条记录的部门放到左边,现在每个员工都有一个部门。
Employee 雇员
DepartmentId DepartmentID的
Department 部
DepartmentId DepartmentID的
Department Name 部门名称
Now Suppose You want to know which employee belongs to which Department Use This Query. 现在假设您想知道哪个雇员属于哪个部门使用此查询。
Select e.empId,d.DepartmentName
from employee e
join department d
on e.departmentid=d.departmentid
Now Suppose You want to know which employee has now assigned any department use the below query 现在假设您想知道哪个员工现在已经分配了任何部门,请使用以下查询
Select e.empId,d.DepartmentName
from employee e
left join department d
on e.departmentid=d.departmentid
where d.departmentid is null
Now Suppose You want to know how many employees which depratment use the below query 现在假设您想知道有多少贬低的员工使用以下查询
Select d.[Department Name],COUNT(e.empID) from Employee e
left join Department d
on e.DepartmentId=d.DepartmentId
group by d.[Department Name]
For more information about joins please use the below image 有关加入的更多信息,请使用下图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.