SQL Server 2005技术内幕:查询、调整和优化5——用C#实现SQL查询

前面几篇文章作为自己的读书笔记(SQL Server 2005技术内幕:查询、调整和优化),基本都是书上的内容,没敢放在首页上。现在学习SQL Server都是理论知识,自己有个习惯,一直看理论看下去不容易吸收,所以看到“聚合”这一节决定写写代码,用来加深对SQL Server执行计划的理解。

首先看看我在在SQL Server中是怎么处理连接查询和分组查询的。

ExpandedBlockStart.gif代码
--建表
if OBJECT_ID('tableA'is not null
   
drop table tableA
Create table tableA(
   ID 
int identity primary key,
   Name 
varchar(30)
)
 
if OBJECT_ID('tableB'is not null
   
drop table tableB
Create table tableB(
   ID 
int,
   summary 
varchar(30)
)
 
--插入测试数据
insert into tableA(Name) select 'A' union all select 'B' union all select 'C'
insert into tableB(ID, summary)
   
select 1'test-001' union all
   
select 1'test-002' union all
   
select 3'test-001' union all
   
select 4'test-001'
 
 
--左连接查询
select A.ID, A.Name, B.summary from tableA A left join tableB B on(A.ID = B.ID)
 
--根据ID分组查询
select ID, COUNT(1number from tableB group by ID
 

 

 

上面一段SQL非常简单,创建了两张表,TableATableB,字典非常简单,TableBIDTableA的外键,这里没有主动建立外键关系。插入几条测试数据,其中实现左连接和分组查询结果如下:


下面我们用C#代码是连接查询和分组功能:

ExpandedBlockStart.gif代码
        class TableA
        {
            
public int ID { getset; }
            
public Char Name { getset; }
        }
 
        
class TableB
        {
            
public int ID { getset; }
            
public String summary { getset; }
        }

 

 

 

第一部创建两个实体类与SQL Server中两个实体相对应,然后插入测试数据:

 

       ExpandedBlockStart.gif代码

 private IList<TableA> tableA = null;
        
private IList<TableB> tableB = null;
 
        
public SQLQuery()
        {
            tableA 
= new List<TableA>()
            {
                
new TableA(){ID=1, Name = 'A'},
                
new TableA(){ID=2, Name = 'B'},
                
new TableA(){ID=3, Name = 'C'}
            };
            tableB 
= new List<TableB>()
            {
                
new TableB(){ID=1, summary = "test-001"},
                
new TableB(){ID=1, summary = "test-002"},
                
new TableB(){ID=3, summary = "test-001"},
                
new TableB(){ID=4, summary = "test-001"},
            };
        }

 

实现连接查询的代码如下:

ExpandedBlockStart.gif代码
        /// <summary>
        
/// 实现SQL中左连接的效果
        
/// </summary>
        public void JoinQuery()
        {
            
foreach (var item in tableA)
            {
                
foreach (var item2 in tableB)
                    
if (item.ID == item2.ID)
                        Console.WriteLine(
"ID: {0}; Name: {1}; Summary: {2}", item.ID, item.Name, item2.summary);
                
if ((from s in tableB where s.ID == item.ID select s).Count() == 0)
                    Console.WriteLine(
"ID: {0}; Name: {1}; Summary: {2}", item.ID, item.Name, null);
            }
        }

 

 

嵌套循环中的两个集合tableAtableB是数据库中两个表,第二循环中比对连接键,相同则输出,第一个循环中最后的那个if用来检查tableB集合中是存在引用tableA的当前ID,如果没有输出一行有IDSummarynull的记录,这里是左连接的特例,如果是普通的内连接,这一行可以去掉。

 

实现分组查询的代码如下:

ExpandedBlockStart.gif代码
/// <summary>
        
/// 分组查询
        
/// </summary>
        public void GroupByQuery()
        {
            
int[] ID = (from s in tableB group tableB by s.ID into groupKey select groupKey.Key).ToArray();
            
int IDIndex = 0;
            
int count = 0;
            
foreach (var item in tableB)
            {
                
if (ID[IDIndex] != item.ID)
                {
                    Console.WriteLine(
"ID: {0}; Number: {1}", ID[IDIndex], count);
                    count 
= 0;
                    IDIndex
++;

                    
if (ID[IDIndex] == item.ID)
                        count
++;

                }
                
else
                    count
++;

            }
            Console.WriteLine(
"ID: {0}; Number: {1}", ID[IDIndex], count);
        }

 

 

这段代码开始对分组键进行去重,这点可以参考我的上篇文章,采用是的流聚合,这里不多加解释。变量IDIndex是用来获取当前ID的索引,Count计算分组情况,也是我们分组查询的目的所在。这里的算法可能有问题,有重复的地方,但功能实现,那位大侠有兴趣可以帮忙改改。

小弟初学SQL Server,之前对数据库的理解仅限于CRUD,这是第一次写文章,写得不对或不好的地方大家帮忙指点下!

 

转载于:https://www.cnblogs.com/fengxiang/archive/2010/09/05/1818386.html

《Microsoft SQL Server 2005技术内幕查询调整优化》是一本涵盖了SQL Server 2005查询性能及优化方面内容的权威指南。本书分为查询处理、查询调优、查询执行查询优化器等部分,详细介绍了SQL Server 2005查询的各个环节优化方法。 在查询处理部分,本书介绍了SQL Server 2005查询处理的基本原理步骤,包括解析、重写、优化执行查询等过程。读者可以了解到SQL Server 2005是如何将查询语句转换为可执行的计划,并将其发送给底层的存储引擎执行的。 在查询调优部分,本书着重介绍了如何识别解决查询性能问题。通过详细讲解如何使用SQL Server 2005自带的性能监视器查询执行计划等工具,读者可以学习到如何分析并优化查询的性能,从而提高系统的响应速度效率。 在查询执行部分,本书深入探讨了SQL Server 2005查询的执行过程原理。读者可以了解到查询的数据获取方式、并行执行查询处理器的内部工作原理等内容,帮助读者更好地理解优化查询的执行。 在查询优化器部分,本书详述了SQL Server 2005查询优化器的工作原理优化策略。读者可以了解到查询优化器是如何根据统计信息成本估算来选择最优的执行计划,从而提高查询的性能效率。 通过阅读《Microsoft SQL Server 2005技术内幕查询调整优化》,读者可以全面了解SQL Server 2005查询的内部原理优化方法,掌握如何调整优化查询,从而提升系统的性能响应速度。无论是数据库开发人员、DBA还是对SQL Server 2005感兴趣的技术专业人士,都会从本书中获得宝贵的经验知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值