海量数据库查询语句
以下代码说明了我们实例中数据库的“红头文件”一表的部分数据结构: 1000 @iint@i=1 @i<=250000 Tgongwen(fariqi,neibuyonghu,reader,title)(,,,) @i=@i+1 declare @i int set @i=1 while @i<=250000 begin insert into Tgongwen(fariqi,title) values('2004-9-16','',',,,,,,,,,,','25') set @i=@i+1 end GO @hint @h=1 @h<=100 @iint @i=2002 @i<=2003 @jint @j=0 @j<50 @kint @k=0 @k<50 Tgongwen(fariqi,title)(cast(@ivarchar(4))++cast(@jvarchar(2))++cast(@jvarchar(2)),) @k=@k+1 @j=@j+1 @i=@i+1 @h=@h+1 @i=1 @i<=9000000 Tgongwen(fariqi,title)(,) @i=@i+1000000 通过以上语句,我们创建了 2520042525200496200220031002500502004559001000
index,SELECT
SQL SERVERclustered indexnonclustered index anazazhang
67263390
2004112004101
1 SQL SERVER IDID1GidSQL SERVERID
IDIDIDIDID
where1328 ID1000325 1 Select gid,fariqi,title from tgongwen 128470128 2fariq gid,titleTgongwen fariqi>dateadd(day,-90,getdate()) 用时: 53763543fariqi gid,getdate()) 用时: 24232251000ID12ID selectdeclare @d datetime set @d=getdate() select select [(datediff(ms,@d,getdate()) 2 23fariqi 1000fariqi5003 3 compound index 25fariqineibuyonghu 1select gid,title from Tgongwen where fariqi>'2004-5-5' 2513 2select gid,title from Tgongwen where fariqi>'2004-5-5' and neibuyonghu='' 2516 3select gid,title from Tgongwen where neibuyonghu='' 60280 12
1 25 select gid,title from Tgongwen where fariqi='2004-9-16' 3326 select gid,title from Tgongwen where gid<=250000 4470 1/4 2order by select gid,title from Tgongwen order by fariqi 12936 select gid,title from Tgongwen order by gid 18843 order by3/1010 3 select gid,title from Tgongwen where fariqi>'2004-1-1' 6343100 select gid,title from Tgongwen where fariqi>'2004-6-6' 317050 select gid,title from Tgongwen where fariqi='2004-9-16' 3326 select gid,title from Tgongwen where fariqi>'2004-1-1' and fariqi<'2004-6-6' 3280 4 10020041150505000 select gid,title from Tgongwen where fariqi>'2004-1-1' order by fariqi 6390 select gid,title from Tgongwen where fariqi<'2004-1-1' order by fariqi 6453
SQL SQLSQL SERVERSQLSQL SERVER select * from table1 where name='zhangsan' and tID > 10000 : select * from table1 where tID > 10000 and name='zhangsan' tID10000name='zhangsan'tID>10000 SQL SERVERwhere where SARG SARGAND < >
< >
Name= >5000 5000< Name= and >5000 SARGSQL SERVERWHERESARG SARGSARG 1LikeSARG name like % SARG name like %,SARG % 2or Name= and >5000 SARGName= or >5000 SARGor 3SARG SARGNOT!=<>!<!>NOT EXISTSNOT INNOT LIKESARG ABS()<5000 Name like %
WHERE *2>5000 SQL SERVERSARGSQL SERVER WHERE >2500/2 SQL SERVER 4IN OR
Select * from table1 where tid in (2,3)
Select * from table1 where tid=2 or tid=3 tid 5NOT 6exists in existsinnot existsnot innotSQL SERVERpubsSQL SERVERstatistics I/O 1select title,price from titles where title_id in (select title_id from sales where qty>30)
'sales' 18 56 0 0 'titles' 1 2 0 0
2select title,price from titles where exists (select * from sales where sales.title_id=titles.title_id and qty>30)
'sales' 18 56 0 0 'titles' 1 2 0 0 existsin 7charindex()%LIKE LIKE%charindex()LIKE select gid,title,reader from tgongwen where charindex('',reader)>0 and fariqi>'2004-5-5' 7 4 7155 0 0 select gid,reader from tgongwen where reader like '%' + '' + '%' and fariqi>'2004-5-5' 7 4 7155 0 0 8unionor whereorunionor select gid,title from Tgongwen where fariqi='2004-9-16' or gid>9990000 68 1 404008 283 392163 select gid,title from Tgongwen where fariqi='2004-9-16' union select gid,title from Tgongwen where gid>9990000 9 8 67489 216 7499 unionor orunionorunionor select gid,title from Tgongwen where fariqi='2004-9-16' or fariqi='2004-2-5' 6423 2 14726 1 7176 select gid,title from Tgongwen wherefariqi='2004-2-5' 11640 8 14806 108 1144 9select *
select top 10000 gid,title from tgongwen order by gid desc 4673 select top 10000 gid,title from tgongwen order by gid desc 1376 select top 10000 gid,fariqi from tgongwen order by gid desc 80
10count(*)count() * select count(*) from Tgongwen 1500 select count(gid) from Tgongwen 1483 select count(fariqi) from Tgongwen 3140 select count(title) from Tgongwen 52050 count(*)count()count(*)count(*) SQL SERVERcount() 11order by gidfariqi select top 10000 gid,title from tgongwen 196 1 289 1 1527 select top 10000 gid,title from tgongwen order by gid asc 4720 1 41956 0 1287 select top 10000 gid,title from tgongwen order by gid desc 4736 1 55350 10 775 select top 10000 gid,title from tgongwen order by fariqi asc 173 1 290 0 0 select top 10000 gid,title from tgongwen order by fariqi desc 156 1 289 0 0 order by order by
12TOP I/0 select top 10 * from ( select top 10000 gid,title from tgongwen where neibuyonghu='' order by gid desc) as a order by gid asc 1000010I/OI/OTOPTOPSQL SERVERTOPORACLEORACLErownumberTOP
web :ADO ADO
pagination1 (@pagesizeint, @pageindexint ) nocounton @indextable(idintidentity(1,1),nidint) @PageLowerBoundint @PageUpperBoundint @PageLowerBound=(@pageindex-1)*@pagesize @PageUpperBound=@PageLowerBound+@pagesize rowcount@PageUpperBound @indextable(nid)gidTGongwenfariqi>dateadd(day,-365,getdate())fariqi O.gid,O.mid,O.title,O.fadanwei,O.fariqiTGongwenO,@indextabletO.gid=t.nid t.id>@PageLowerBoundt.id<=@PageUpperBoundt.id nocountoff 以上存储过程运用了 SQL SERVERCREATE TABLE #TempSQL SERVERADOnm publish n m TOPm-n+1* publish (id (TOPn-1id publish)) id publishASP.NET+ C#SQL SERVER pagination2( @SQLnVARCHAR(4000), @Pageint, @RecsPerPageint, @IDVARCHAR(255), @SortVARCHAR(255) ) @StrnVARCHAR(4000) @Str=+CAST(@RecsPerPageVARCHAR(20))++@SQL++@ID+ (TOP()T9) PRINT@Str sp_ExecuteSql@Str 其实,以上语句可以简化为: Table1(ID (TOP页大小*页数id 表 id)) ID 但这个存储过程有一个致命的缺点,就是它含有 NOT INTable1(*(top(页大小*页数)*table1id)bb.id=a.id) id 即,用 not existsnot inTOP NOT IN not existsSQL SERVERTOPTOPTOP TOPNOT INTOPNOT INNOT IN max()min()maxmin><SARG Select top 10 * from table1 where id>200 table1 id> ((id) (top((页码-1)*页大小)idtable1id)T ) id 在选择即不重复值,又容易分辨大小的列时,我们通常会选择主键。下表列出了笔者用有着 1000GIDGIDgid,title110100500100011025501 2 3 1 60 30 76 10 46 16 63 1001076 720130 50054012943 83 1000 17110 470250 1 24796 4500 140 10 38326 42283 1553 25 28140 128720 2330 50 121686 127846 7168 10010001 SQL SERVERSQLWEBSQL -- pagination3@tblNamevarchar(255), @strGetFieldsvarchar(1000)=, @fldNamevarchar(255)=, @PageSizeint=10, @PageIndexint=1, @doCountbit=0, @OrderTypebit=0, @strWherevarchar(1500)= @strSQLvarchar(5000) @strTmpvarchar(110) @strOrdervarchar(400)@doCount!=0 @strWhere!= @strSQL=+@tblName++@strWhere @strSQL=+@tblName+ @OrderType!=0 @strTmp= @strOrder=+@fldName+ @strTmp= @strOrder=+@fldName+ @PageIndex=1 @strWhere!= @strSQL=+str(@PageSize)++@strGetFields++@tblName++@strWhere++@strOrder @strSQL=+str(@PageSize)++@strGetFields++@tblName++@strOrder @strSQL=+str(@PageSize)++@strGetFields+ +@tblName++@fldName++@strTmp++@fldName++str((@PageIndex-1)*@PageSize)++@fldName++@tblName++@strOrder++@strOrder@strWhere!= @strSQL=+str(@PageSize)++@strGetFields+ |