Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

ÄãÖªµÀ¶àÉÙ¹ØÓÚsql select top N?

±³¾°:
sql select top N Óï¾äÊÇÒ»¸ö·Ç³£ÖØÒªµÄÓï¾ä, ÔÚʵÏÖ·ÖÒ³²éѯÖÐÊDz»¿É»òȱµÄ. ÓÉÓÚ·ÖÒ³²éѯͨ³£Éæ¼°º¬ÓдóÁ¿¼Ç¼µÄ±í£¬ ²¢ÇÒÊÇΪ´óÁ¿Óû§·ÖÏíµÄÈÎÎñ£¬Òò´Ë£¬¶ÔÆä½øÐÐÓÅ»¯ÊǺÜÓÐÒâÒåµÄ¡£
ʵÏÖsql top N µÄ¹¦ÄÜÓм¸ÖÖ±äÖÖ£º
1. set rowcount @n; select ... order by somefields
2. select top (@n) .... order by somefields
3. select top (xx) ....   order by somefields
        -- ÆäÖÐ xxÊÇÒ»¸ö³£Êý£¬ ±ÈÈç10
ÔÚÉÏÊöµÄ²éѯÖÐÒýÓõÄsomefields, Èç¹ûÉæ¼°µÄ±íÔÚÆäÉÏÓÐË÷ÒýÊÇÒ»ÖÖÇé¿ö£¬ ûÓÐË÷ÒýÓÖÊÇÒ»ÖÖÇé¿ö¡£
ÓÐË÷ÒýµÄ»°£¬¼´Ê¹±íº¬Óкܶà¼Ç¼£¬Ò²²»»á¶ÔÐÔÄÜÔì³ÉÌ«´óÎÊÌâ¡£
ûÓÐË÷ÒýµÄÇé¿öÒ²ÊÇ»áÓÐʵ¼ÊÐèÇóµÄ£¬±ÈÈçʵʱµÄÕÒ³öÏúÊÛ×îºÃµÄǰ100¸ö²úÆ·¡£ÔÚûÓÐË÷ÒýʱµÄ²éÕÒTop N£¬ Èç¹û²»½øÐÐϸÖµÄÓÅ»¯£¬»á¶ÔÐÔÄÜÔì³ÉºÜ´óµÄÓ°Ï죬ÉõÖÁ»áʹµÃÕû¸öϵͳ̱»¾¡£
Èç¹ûÒª¶Ôtop n½øÐÐÓÅ»¯£¬ÄÇôÁ˽âsql server ÊÇÈçºÎ´¦ÀíÉÏÊöµÄtop n µÄ¼¸ÖÖ±äÐξÍÊǺÜÓбØÒªµÄ. ÏÂÃæµÄÎÄÕÂÊÇÎÒÔÚMSµÄÂÛ̳ÉÏ·¢µÄ£¬ ÎÒ×Ô¼ºÀÁµÃ·­Òë³ÉÖÐÎÄÁË£¬ºÍ´ó¼Ò¹²Ïíһϰɡ£
Ô­ÎÄ(ÊÇÎÒÔÚhttp://social.msdn.microsoft.com/Forums/en/transactsql/thread/944ad896-b34c-4dea-af55-cfbae79251f6ÉϵÄÒ»¸ö»ØÌù)
Question:
--fast
1. select top 100 * from test where c1 < 30000 order by c2
--slow
2. select top 101 * from test where c1 < 30000 order by c2
1. is more than  two times faster than 2.
Why?
What a coinccident! I am on the same issue just at the time.
I was considering implementing an algorithm like this:
First populate the N rows to a table variable (with index on the sort column), then iterate through all left rows, adding one row to the table variable if bigger than min of the table, else discard it.  This could be either done in sql or clr aggregate function.
Then I thought maybe MS had already done it in the Top N stuff, so started to run a test against it.
CREATE TABLE [dbo].[NUM]
([n] int NOT NULL, s varchar(128) NULL, PRIMARY KEY CLUSTERED([n] ASC))
go
-- populate data
set nocount on
declare @n int, @i int
set @n=1000000
set @i = 0
while @n>0 begin
if @i = 0 begin tran
insert


Ïà¹ØÎĵµ£º

sql 2005 ´æ´¢¹ý³Ì·ÖÒ³ java ´úÂë

 create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',         
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ ......

sqlÓïÑÔÖеÄcase when £¨À´×ÔÎÒ×ð¾´µÄһλÀÏʦСF£©

sqlÓïÑÔÖÐÓÐûÓÐÀàËÆCÓïÑÔÖеÄswitch caseµÄÓï¾ä£¿£¿
  ûÓÐ,ÓÃcase   when   À´´úÌæ¾ÍÐÐÁË.  
      
  ÀýÈç,ÏÂÃæµÄÓï¾äÏÔʾÖÐÎÄÄêÔ  
   
  select   getdate()   as   È ......

³£ÓÃsqlÓï¾ä

1.ÏÞÖÆ·µ»Ø¼Ç¼µÄÐÐÊý£¨4-10ÐУ©
select rownum,c1 from t144 where rownum<10
minus
select rownum,c1 from t144 where rownum<5
2.ÅúÁ¿²åÈë
insert into tablename(select * from othertable)
»ò
insert into (column1,column2,,,,,,,)tablename(select column1,column2,,,,,,,from othertable)
3,·Ö×éº¯Ê ......

SQLÓï¾äÖÐNULLµÄÕæÊµº¬Òå

NULL£¬±íʾ²»Ã÷È·¡¢Î´ÖªµÄÁÐÖµ
²âÊÔ±í£ºtestnull(id varchar(32))
Êý¾Ý¿â£ºSybase ASA11.0
ÐÐÊý¾Ý(''), (NULL)
Êý¾Ý¿âÑ¡ÏîansinullΪtrue(Ò²ÊÇASAÊý¾Ý¿âµÄĬÈÏÑ¡Ïî)ʱ£¬
select * from testnull where id = null
select * from testnull where id != null
½á¹û¾ùΪ¿Õ
select * from testnull where id is null
½á¹ ......

C#Êý¾Ý¿â±à³Ì»ù´¡Ö®sqlÓï¾ä²Ù×÷Êý¾Ý¿âÊý¾Ý

        Á¬½Ósql serverÊý¾Ý¿âÓõ½µÄÃüÃû¿Õ¼äÊÇusing System.Data.SqlClient;
  Êý¾Ý¿âÁ¬½Ó´úÂë:¿ÉÒÔÔÚ´óÀàÖ®ÏÂÉùÃ÷ public SqlConnection myConnection;  //sqlÁ¬½Ó¶ÔÏó
               &nbs ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ