惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Kain

Microsoft.Practices.Unity 的一个线程安全Bug浅析 NET Reflector 7发布,其 不再免费 (抽象)工厂的另一种实现方式 自定义EF4 Model 代码生成 - Kain .net 4.0 中对多线程新特性(四)--任务和任务工厂 .net 4.0 中对多线程新特性(三) .net 4.0 中对多线程新特性(二) .net 4.0 中对多线程新特性(一) Flex&.Net开篇 - Kain [读书笔记]Start-up fatigue(启动杂役) 马上又要过中秋和国庆了! 一个小问题 C#2.0 新的关键字 yield 我们的游戏! 不知道有没有同在学习aspnetforums的 郁闷! 关于面试 关于DataGride的Key事件 开心就好!
SqlSever N层表数据查询效率
Kain · 2007-11-02 · via 博客园 - Kain

在做数据查询时INNER JOIN 可以说是最熟悉的一个关键字之一了。例如通常查询一个客户的订单详细信息时可能会有这样的Sql:

 Select * From Orders A
   
Inner Join CustomerOrders B On B.Order_Id = A.Order_Id
   
Inner Join Customers C On C.Customer_Id = B.Customer_Id
   
Where C.Customer_Name = ‘Jack’

这样的查询对数据库来说是非常快的,因此也习惯了这样用,用几个表Join几个表。但是随着Join的表越多查询的性能会急剧下降。为了提高查询这种深层次对象的效率,采用了一种折中的办法就是将相邻两个表作join将主键插入一个表变量,然后作为下一个表查询的条件,上面的Sql就会变成:
 

Declare @customer_temp Table(Customer_Id int);
 
Insert Into @customer_temp
  
Select Customer_Id 
 
From Customers
 
Where Customer_Name = ‘Jack’
 
 
Declare @cutomerorders_temp Table ( Customer_Id int,Order_Id int);
 
Insert Into @cutomerorders_temp
 
Select Customer_Id,Order_id 
 
From CustomerOrders A
 
Inner Join @customer_temp B On B.Customer_Id = A.Customer_Id
 
 
Select * From Orders A
  
Inner Join @cutomerorders_temp B On B.Order_Id = A.Order_Id 

   这样做的好处在于表的层次非常深,根据一个顶层对象查询一个底层表时效率会比直接join高上很多。Join 表的次数如果达9层时查询几乎是非常困难的,如果用这种方式表的嵌套可以在20级左右效率还是可以承受,但是像那种层次100多级就够呛了(不知道那位仁兄做过这样的研究,现在为这个问题头疼)。当然如果需要同时返回多个表中的列这种方式就不行了,例如:

Select A.OrderNo,C.Customer_Name From Orders A
  
Inner Join CustomerOrders B On B.Order_Id = A.Order_Id
  
Inner Join Customers C On C.Customer_Id = B.Customer_Id
  
Where C.Customer_Name = ‘Jack’

PS:可能解决这种问题的方式很多,自己也在不断的尝试新的办法。借此抛砖引玉,看各位看官能否有更好的意见和建议。