我在三台安装SQL Server 2012的服务器上搭建分布式数据库,把产品环境中一年近1.4亿条数据大致均匀地存储在这三台服务器中,每台Server 存储4个月的数据,物理机的系统配置基本相同:内存16G,双核 CPU 3.6GHz,软件环境是Windows Server 2012 R,和SQL Server 2012。

1,创建水平分区视图

基础表是dbo.Commits,每个基础表大致存储4个月的数据,近5000万条记录:

CREATE TABLE [dbo].[Commits]
([CommitID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,[AuthorID] [bigint] NOT NULL,[CreatedDate] [datetime2](7) NOT NULL,[CreatedDateKey] [int] NOT NULL,CONSTRAINT [PK__Commits_CommitID] PRIMARY KEY CLUSTERED
([CommitID] ASC,[CreatedDateKey] ASC
)
) 

View Code

创建分区视图,Linked Server的Alias是db2 和 db3,Catalog 是 tdw(test data warehouse):

CREATE view [dbo].[view_commits]
asselect [CommitID],[AuthorID],[CreatedDate],[CreatedDateKey]
from dbo.commits c with(nolock)
where c.[CreatedDateKey] between 20150900 and 20160000union ALL
select [CommitID],[AuthorID],[CreatedDate],[CreatedDateKey]
from db3.tdw.dbo.commits c with(nolock)
where c.[CreatedDateKey] between 20150000 and 20150500union ALL
select [CommitID],[AuthorID],[CreatedDate],[CreatedDateKey]
from db2.tdw.dbo.commits c with(nolock)
where c.[CreatedDateKey] between 20150500 and 20150900
WITH check OPTION;
GO

View Code

2,查询性能测试

Test1,在基础表上测试,基础表是全部的数据,cost:79s

select count(0)
from dbo.commits_total c  with(nolock)
where day(c.[CreatedDate])=1

Test2,使用分区视图测试,cost=134s,比Test1的查询性能明显降低。

select count(0)
from dbo.view_commits c  with(nolock)
where day(c.[CreatedDate])=1

3,使用OpenQuery查询

OpenQuery把查询语句直接发送到Linked Server上执行,返回查询的结果,cost:105s,还是很高,相对提高20%的性能。

select sum(t.cnt) as cnt
from
(select count(0) as cntfrom dbo.commits c  with(nolock)where day(c.[CreatedDate])=1UNION allselect p.cntfrom openquery(db2,N'select count(0) as cntfrom dbo.commits c  with(nolock)where day(c.[CreatedDate])=1') as pUNION allselect p.cntfrom openquery(db3,N'select count(0) as cntfrom dbo.commits c  with(nolock)where day(c.[CreatedDate])=1') as p
) as t

View Code

4,使用C# 多线程编程

创建三个Task同时运行在三台Server上,Cost:28s

static void Main(string[] args)
{List<Task> tasks = new List<Task>();int c1=0, c2=0, c3=0;Task t1 = new Task(()=> {c1= GetCount("xxx");});Task t2 = new Task(() =>{c2=  GetCount("xxx");});Task t3 = new Task(() =>{c3= GetCount("xxx");});tasks.Add(t1);tasks.Add(t2);tasks.Add(t3);Stopwatch sw = new Stopwatch();sw.Start(); t1.Start();t2.Start();t3.Start();Task.WaitAll(tasks.ToArray());int sum = c1 + c2 + c3;sw.Stop();Console.Read();
}static int GetCount(string str)
{using (SqlConnection con = new SqlConnection(str)){con.Open();var cmd = con.CreateCommand();cmd.CommandText = @" select count(0) as cntfrom dbo.commits c  with(nolock)where day(c.[CreatedDate]) = 1";int count = (int)cmd.ExecuteScalar();con.Close();return count;}
}

View Code

5,结论

  • 将数据水平切分,分布式部署在不同的SQL Server上,其查询性能并不一定比单一DB性能更好。
  • 使用OpenQuery函数将查询语句在Remote Server上执行,返回查询结果,能够优化Linked Server 的查询性能。
  • 在使用分布式数据库查询数据时,针对特定的应用,编写特定的代码,这需要fore-end 更多的参与。

参考doc:

Top 3 Performance Killers For Linked Server Queries

[翻译]——SQL Server使用链接服务器的5个性能杀手

Linked Server 3:SQL Server 分布式数据库性能测试相关推荐

  1. [Microsoft][ODBC SQL Server Driver][SQL Server]数据库‘XXXXXX‘的事务日志已满。解决办法!

    下午上班,关务人员反应报关软件无法使用,截图过来看了下,报错提示如下: [Microsoft][ODBC SQL Server Driver][SQL Server]数据库'XXXXXX'的事务日志已 ...

  2. [Oracle][ODBC SQL Server Driver][SQL Server]对象名 'RECOVER.HS_TRANSACTION_LOG' 无效(转)

    原帖由 qingyun 于 2010-6-21 15:44 发表  在写pl/sql的时候,有个很重要的注意点: 比如: begin   update  某个sqlserver的表@dblink名字 ...

  3. aws rds监控慢sql_探索AWS RDS SQL Server上SQL Server集成服务(SSIS)

    aws rds监控慢sql In the previous article, Deploy tabular databases in SSAS on AWS RDS SQL Server, we ex ...

  4. [Microsoft][ODBC SQL Server Driver][SQL Server]String or binary data would be truncated.

    Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e57' [Microsoft][ODBC SQL Server Driver][SQL Ser ...

  5. 安装SQL Server和SQL Server Management Studio(SSMS)

    我也是第一次安装SQL Server和SQL Server工具,SQL Server Management Studio(SSMS). 经过查询了资料,开始了我的第一次数据库安装. 这是我安装的经验分 ...

  6. 2.SQL SERVER笔记——SQL SERVER系统概念

    2.SQL SERVER笔记--SQL SERVER系统概念 系统数据库 数据库管理员(DBA)的一项基本的技能是对SQL数据库引擎的系统数据库的深刻理解.数据库开发人员了解SQLSERVER自带的系 ...

  7. 用eclipse连接数据库捕获java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]将截断字符串或二进制数据。

    用eclipse连接数据库捕获异常显示java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]将截断字符串或二进制数 ...

  8. 开机总显示SQL server服务器,sql server 2008启动服务是:提示请求失败或者服务无及时响应...

    这是log日志 2012-12-10 23:28:51.06 Server      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86 ...

  9. mssql 数据库审计账户_SQLServer数据库审计功能入门之SQL Server审核 (SQL Server Audit)...

    本文主要向大家介绍了SQLServer数据库审计功能入门之SQL Server审核,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. 介绍 Audit是SQL Server ...

最新文章

  1. 写if-else不外乎两种场景:异常逻辑处理和不同状态处理。
  2. Matlab绘图函数一览
  3. EOS资源模型(1)资源说明
  4. ATM and Students 双指针,前缀和(1800)
  5. 好程序员Web前端分享无法忽视的JavaScript技巧
  6. [云炬创业基础笔记]第一章创业环境测试3
  7. spring第二冲刺阶段第九天
  8. SQL注入(1)--判断是否存在SQL注入漏洞
  9. β射线与哪些物质可产生较高的韧致辐射_辐射无所不在,香蕉土豆里都有?我们还能愉快生活吗?...
  10. Android ListView 疯狂之旅 之 《自定义下拉刷新功能的ListView》
  11. c语言c程序由函数构成 每个函数完成相对独立的功能,17秋学期(1709)《C语言程序设计》在线作业  满分...
  12. SpringBoot Cache 深入
  13. nginx是否存在文件类型解析漏洞(转)
  14. 如何用html构建ios应用,使用HTML5构建iOS原生APP(5)
  15. activemq spring 集成与测试
  16. 跟着小码哥一起学习OC语法,都在这里了
  17. Introduction to TurboFan
  18. 手把手教你,搭建内网穿透服务
  19. python红楼梦人物词频统计_用R进行文本分析初探——以《红楼梦》为例
  20. js实现动态显示时间(setInterval())

热门文章

  1. 《音乐达人秀:Adobe Audition CC实战222例》——1.2 从双卡录音机到多轨录音软件...
  2. django 声称图表_停止声称您正在使用无模式数据库
  3. [统计学笔记] (七) 假设检验
  4. AOP机制之环绕通知的见解
  5. 破解“封闭式基金折价之谜”(ZT)
  6. 魔鬼交易员害了法兴银行却救了全球经济?
  7. 专题 | 如何抢先一步拿 Offer?
  8. Go语言微服务实战之API网关
  9. 计算机语言学笔记(二)现代汉语切分研究
  10. STM8L151C8单片机学习例程(2)——CLK切换