海量的查询优化及分页算法方案

来源:互联网 发布:怎么修改微博个性域名 编辑:程序博客网 时间:2024/05/16 02:26
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

海量数据库的

查询优化分页算法方案

 

随着“金盾工程”建设的逐步深入和公安信息化的高速发展,公安计算机应用系统被广泛应用在各警种、各部门。与此同时,应用系统体系的核心、系统数据的存放地――数据库也随着实际应用而急剧膨胀,一些大规模的系统,如人口系统的数据甚至超过了1000万条,可谓海量。那么,如何实现快速地从这些超大容量的数据库中提取数据(查询)、分析、统计以及提取数据后进行数据分页已成为各地系统管理员和数据库管理员亟待解决的难题。

在以下的文章中,我将以“办公自动化”系统为例,探讨如何在有着1000万条数据的MSSQLSERVER数据库中实现快速的数据提取和数据分页。以下代码说明了我们实例中数据库的“红头文件”一表的部分数据结构:

CREATETABLE[dbo].[TGongwen](   --TGongwen是红头文件表名

  [Gid][int]IDENTITY(1,1)NOTNULL,
--本表的id号,也是主键

  [title][varchar](80)COLLATEChinese_PRC_CI_ASNULL, 
--红头文件的标题

  [fariqi][datetime]NULL,
--发布日期

  [neibuYonghu][varchar](70)COLLATEChinese_PRC_CI_ASNULL,
--发布用户

  [reader][varchar](900)COLLATEChinese_PRC_CI_ASNULL,

--需要浏览的用户。每个用户中间用分隔符“,”分开

)ON[PRIMARY]TEXTIMAGE_ON[PRIMARY]

GO

 

下面,我们来往数据库中添加1000万条数据:

declare@iint

set@i=1

while@i<=250000

begin

   insertintoTgongwen(fariqi,neibuyonghu,reader,title)values('2004-2-5','通信科','通信科,办公室,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,治安支队,外事科','这是最先的25万条记录')

   set@i=@i+1

end

GO

 

declare@iint

set@i=1

while@i<=250000

begin

   insertintoTgongwen(fariqi,neibuyonghu,reader,title)values('2004-9-16','办公室','办公室,通信科,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,外事科','这是中间的25万条记录')

   set@i=@i+1

end

GO

 

declare@hint

set@h=1

while@h<=100

begin

declare@iint

set@i=2002

while@i<=2003

begin

declare@jint

       set@j=0

       while@j<50

           begin

declare@kint

           set@k=0

           while@k<50

           begin

   insertintoTgongwen(fariqi,neibuyonghu,reader,title)values(cast(@iasvarchar(4))+'-8-153:'+cast(@jasvarchar(2))+':'+cast(@jasvarchar(2)),'通信科','办公室,通信科,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,外事科','这是最后的50万条记录')

           set@k=@k+1

           end

set@j=@j+1

       end

set@i=@i+1

end

set@h=@h+1

end

GO

 

declare@iint

set@i=1

while@i<=9000000

begin

   insertintoTgongwen(fariqi,neibuyonghu,reader,title)values('2004-5-5','通信科','通信科,办公室,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,治安支队,外事科','这是最后添加的900万条记录')1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击