ÆÊÎöSQL ServerÖ´Ðмƻ®
-->Title: 淺議SQL ServerÖ´Ðмƻ®
-->Author: wufeng4552
-->Date :2009-10-20 15:08:24
ǰÑÔ:
×î½ü溫習ÁË執ÐÐ計劃·½ÃæµÄ²¿·ÝÖª識,為Á˼ÓÉîÓ¡Ïó與·½±ã³õ學Õß,ÌØ×öÁËÈçÏÂÕûÀí.
²»對µØ·½歡ÓÌá³ö並Ö¸Õý.
²é¿´Ö´Ðмƻ®µÄ·½Ê½:
(1)²Ë單·½Ê½:
(1.1)ÏÔʾʵ¼ÊÖ´Ðмƻ®
(1.2)ÏÔʾԤ¹ÀµÄÖ´Ðмƻ®
ÒÔÉÏ兩種¾ùλì¶Î»ÓÚ”²éѯ”ÏÂÀ²Ëµ¥ÖÐ,Á½ÕߵIJ»Í¬Ö®´¦ÔÚÓÚµ±Êµ¼ÊÔËÐÐÒ»¸ö²éѯʱ,µ±Ç°µÄ·þÎñÆ÷ÉϵÄÔËËãÒ²»á±»¿¼ÂǽøÈ¥¡£´ó¶àÊýÇé¿öÏÂ,Á½ÖÖ·½Ê½²úÉúµÄÖ´Ðмƻ®²úÉúµÄ½á¹ûÊÇÏàËÆµÄ.
(2)ÃüÁʽ
SET SHOWPLAN_TEXT ON
ÕâÌõÃüÁî±»Ö´Ðкó,ËùÓÐÔÚµ±Ç°Õâ¸ö²éѯ·ÖÎöÆ÷»á»°ÖÐÖ´ÐеIJéѯ¶¼²»»áÔËÐÐ,¶øÊÇ»áÏÔʾһ¸ö»ùÓÚÎı¾µÄÖ´Ðмƻ®
×¢Òâ:Ö´ÐÐijÌõÓõ½ÁÙʱ±íµÄ²éѯʱ£¬±ØÐëÔÚÖ´ÐвéѯÏÈÔËÐÐSET STATISTICS PROFILE ONÓï¾ä Èç:
go
if not object_id('tempdb..#t') is null
drop table #t
Go
Create table #t([ÈÕÆÚ] Datetime,[ÐÕÃû] nvarchar(2))
Insert #t
select '2009-10-01',N'ÕÅÈý' union all
select '2009-10-01',N'ÀîËÄ' union all
select '2009-10-02',N'ÕÔÁù'
Go
SET STATISTICS PROFILE ON
go
select * from #T
SET STATISTICS PROFILE OFF
結¹ûÈç圖1
圖1
為ÁË討論·½±ã ÏÂÃæÒÔ Northwind 庫Öбí [Order Details] 為Àý(ÎÒÒÑ經將Ö÷鍵刪³ý)
use Northwind
go
SET SHOWPLAN_TEXT ON
go
select ProductID,sum(Quantity)Quantity from [Order Details]
group by ProductID order by ProductID
go
SET SHOWPLAN_TEXT OFF
/*
StmtText
|--Sort(ORDER BY:([Northwind].[dbo].[Order Details].[ProductID] ASC))
|--Hash Match(Aggregate, HASH:([Northwind].[dbo].[Order Details].[ProductID]) DEFINE:([Expr1004]=SUM([Northwind].[dbo].[Order Details].[Quantity])))
|--Table Scan(OBJECT:([Northwind].[dbo].[Order Details]))
*/
use Northwind
go
----½¨Ò»¸ö¾Û¼¯Ë÷Òý
CREATE CLUSTERED INDEX INDEX_ProductID on [Order Details](ProductID)
go
SET SHOWPLAN_TEXT ON
go
select ProductID,sum(Quantity)Quantity from [
Ïà¹ØÎĵµ£º
1.Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)¡¡¡¡
¡¡¡¡ SQLSERVERµÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬Òò´Ëfrom×Ó¾äÖÐдÔÚ×îºóµÄ±í£¨»ù´¡±ídriving table£©½«±»×îÏÈ´¦Àí£¬ÔÚfrom×Ó¾äÖаüº¬¶à¸ö±íµÄÇé¿öÏ£¬±ØÐëÑ¡Ôñ¼Ç¼ÌõÊý×îÉٵıí×÷Ϊ»ù´¡±í£¬µ±SQLSERVER´¦Àí¶à¸ö±íʱ£¬»áÔËÓÃÅÅÐò¼°ºÏ²¢µÄ·½Ê½Á ......
¾³£½øÐвéѯ£¬Ð´×Åselect * from Ì«·Ñʱ¼ä£¬Äܲ»ÄÜÖ±½ÓÊäÈëÒ»¸ös ¾ÍÄÜ×Ô¶¯³öÀ´ select * from Âð£¿
·¢ÏÖpl/sqlÖпÉÒÔÅäÖÃ×Ô¶¯Ìæ»»
ÔÚPL/SQLµÄ°²×°Ä¿Â¼ÏÂÃæ£º$\PLSQL Developer\PlugIns ÖÐÌí¼ÓÒ»¸öÎı¾Îļþ£¬±ÈÈçÃüÃûΪ:AutoReplace.txt¡£Îı¾ÎļþÖÐÌîдÈçÏÂÄÚÈÝ£º
st = select t.* ,t.rowid from t
s = se ......
/*
±ÈÈçExcelÓÐÁ½ÁУ¬AÁкÍBÁÐÐèÒªµ¼Èëµ½SQL±íÖУ¬·´ÕýÎÒÒѾÓм¸Äê²»ÓÃDTSÖ®ÀàµÄ¹¤¾ßÁË¡£
ÔÚExcelÖеÄеÄÒ»ÁÐÖУ¬Ö±½Óд¹«Ê½
=CONCATENATE("Insert #tmp values('",A1,"','",B1,"')")
°ÑÿһÐж¼Éè³ÉͬÑùµÄ¹«Ê½(Ë«»÷¼´¿ÉÍê³É)¡£
°ÑÕûÁи´ÖÆÏÂÀ´£¬·Åµ½²éѯ·ÖÎöÆ÷ÖÐÖ±½ÓÔËÐоͺÃÁË¡£
Ò²¿ÉÒ԰ѹ«Ê½¸Ä³É =CONCATEN ......
--ÇóÌõsqlÓï¾ä£º
-- ÒªÇó£º1.°´ÕÕGroup_OutDate×Ö¶ÎÅÅÐò £¬µÃµ½Ç°5Ìõ¼Ç¼
-- 2.Group_Name×ֶεÄÖµ²»ÄÜÖØ¸´
SELECT TOP 5 * from
( SELECT DISTINCT Group_Name ,
(SELECT TOP 1 Group_ID from AA_Sell_Group AS b WHERE b.Gro ......