ºìÁªLinuxÃÅ»§
Linux°ïÖú

OracleÖÐ--ÓÅ»¯SQLÓï¾äÖ´ÐеÄÔ­Ôò

·¢²¼Ê±¼ä:2006-05-17 09:31:29À´Ô´:ºìÁª×÷Õß:cooc
1¡£ÒѾ­¼ìÑéµÄÓï¾äºÍÒÑÔÚ¹²Ïí³ØÖеÄÓï¾äÖ®¼äÒªÍêÈ«Ò»Ñù
2¡£±äÁ¿Ãû³Æ¾¡Á¿Ò»ÖÂ
3¡£ºÏÀíʹÓÃÍâÁª½Ó
4¡£ÉÙÓöà²ãǶÌ×
5¡£¶àÓò¢·¢

Óï¾äµÄÓÅ»¯²½ÖèÒ»°ãÓУº
1¡£µ÷ÕûsgaÇø£¬Ê¹µÃsgaÇøµÄÊÇÓÃ×îÓÅ¡£
2¡£sqlÓï¾ä±¾ÉíµÄÓÅ»¯£¬¹¤¾ßÓÐexplain,sql traceµÈ
3¡£Êý¾Ý¿â½á¹¹µ÷Õû
4¡£ÏîÄ¿½á¹¹µ÷Õû
дÓï¾äµÄ¾­Ñ飺
1¡£¶ÔÓÚ´ó±íµÄ²éѯʹÓÃË÷Òý
2¡¢ÉÙÓÃin,existµÈ
3¡¢Ê¹Óü¯ºÏÔËËã

£±£®¶ÔÓÚ´ó±í²éѯÖеÄÁÐÓ¦¾¡Á¿±ÜÃâ½øÐÐÖîÈç
¡¡¡¡ £Ô£ï£ß£ã£è£á£ò£¬£ô£ï£ß£ä£á£ô£å£¬£ô£ï£ß£î£õ£í£â£å£ò
¡¡¡¡ µÈת»»
£²£®ÓÐË÷ÒýµÄ¾¡Á¿ÓÃË÷Òý£¬ÓÐÓõ½Ë÷ÒýµÄÌõ¼þдÔÚÇ°Ãæ
¡¡¡¡ ÈçÓпÉÄܺÍÓбØÒª¾Í½¨Á¢Ò»Ð©Ë÷Òý¡¡
£³£®¾¡Á¿±ÜÃâ½øÐÐÈ«±íɨÃ裬ÏÞÖÆÌõ¼þ¾¡¿ÉÄܶ࣬ÒÔ±ã¸ü¿ì
¡¡¡¡ ËÑË÷µ½Òª²éѯµÄÊý¾Ý

ÈçºÎÈÃÄãµÄSQLÔËÐеøü¿ì
½»Í¨ÒøÐг¤´º·ÖÐеçÄÔ²¿
ÈÎÁÁ
---- ÈËÃÇÔÚʹÓÃSQLʱÍùÍù»áÏÝÈëÒ»¸öÎóÇø£¬¼´Ì«¹Ø×¢ÓÚËùµÃµÄ½á¹ûÊÇ·ñÕýÈ·£¬¶øºöÂÔÁ˲»Í¬µÄʵÏÖ·½·¨Ö®¼ä¿ÉÄÜ´æÔÚµÄÐÔÄܲîÒ죬ÕâÖÖÐÔÄܲîÒìÔÚ´óÐ͵ĻòÊǸ´ÔÓµÄÊý¾Ý¿â»·¾³ÖУ¨ÈçÁª»úÊÂÎñ´¦ÀíOLTP»ò¾ö²ßÖ§³ÖϵͳDSS£©ÖбíÏÖµÃÓÈΪÃ÷ÏÔ¡£±ÊÕßÔÚ¹¤×÷ʵ¼ùÖз¢ÏÖ£¬²»Á¼µÄSQLÍùÍùÀ´×ÔÓÚ²»Ç¡µ±µÄË÷ÒýÉè¼Æ¡¢²»³ä·ÝµÄÁ¬½ÓÌõ¼þºÍ²»¿ÉÓÅ»¯µÄwhere×Ӿ䡣ÔÚ¶ÔËüÃǽøÐÐÊʵ±µÄÓÅ»¯ºó£¬ÆäÔËÐÐËÙ¶ÈÓÐÁËÃ÷ÏÔµØÌá¸ß£¡ÏÂÃæÎÒ½«´ÓÕâÈý¸ö·½Ãæ·Ö±ð½øÐÐ×ܽ᣺

---- ΪÁ˸üÖ±¹ÛµØ˵Ã÷ÎÊÌ⣬ËùÓÐʵÀýÖеÄSQLÔËÐÐʱ¼ä¾ù¾­¹ý²âÊÔ£¬²»³¬¹ý£±ÃëµÄ¾ù±íʾΪ£¨< 1Ã룩¡£

---- ²âÊÔ»·¾³--
---- Ö÷»ú£ºHP LH II
---- Ö÷Ƶ£º330MHZ
---- Äڴ棺128Õ×
---- ²Ù×÷ϵͳ£ºOperserver5.0.4
----Êý¾Ý¿â£ºSybase11.0.3

Ò»¡¢²»ºÏÀíµÄË÷ÒýÉè¼Æ
----Àý£º±írecordÓÐ620000ÐУ¬ÊÔ¿´ÔÚ²»Í¬µÄË÷ÒýÏ£¬ÏÂÃ漸¸ö SQLµÄÔËÐÐÇé¿ö£º
---- 1.ÔÚdateÉϽ¨ÓÐÒ»·Ç¸öȺ¼¯Ë÷Òý

select count(*) from record where date >
19991201 and date < 19991214and amount >
2000 (25Ãë)
select date,sum(amount) from record group by date
(55Ãë)
select count(*) from record where date >
19990901 and place in (BJ,SH) (27Ãë)
---- ·ÖÎö£º
----dateÉÏÓдóÁ¿µÄÖظ´Öµ£¬ÔÚ·ÇȺ¼¯Ë÷ÒýÏ£¬Êý¾ÝÔÚÎïÀíÉÏËæ»ú´æ·ÅÔÚÊý¾ÝÒ³ÉÏ£¬ÔÚ·¶Î§²éÕÒʱ£¬±ØÐëÖ´ÐÐÒ»´Î±íɨÃè²ÅÄÜÕÒµ½ÕâÒ»·¶Î§ÄÚµÄÈ«²¿ÐС£

---- 2.ÔÚdateÉϵÄÒ»¸öȺ¼¯Ë÷Òý

select count(*) from record where date >
19991201 and date < 19991214 and amount >
2000 £¨14Ã룩
select date,sum(amount) from record group by date
£¨28Ã룩
select count(*) from record where date >
19990901 and place in (BJ,SH)£¨14Ã룩
---- ·ÖÎö£º
---- ÔÚȺ¼¯Ë÷ÒýÏ£¬Êý¾ÝÔÚÎïÀíÉÏ°´Ë³ÐòÔÚÊý¾ÝÒ³ÉÏ£¬Öظ´ÖµÒ²ÅÅÁÐÔÚÒ»Æð£¬Òò¶øÔÚ·¶Î§²éÕÒʱ£¬¿ÉÒÔÏÈÕÒµ½Õâ¸ö·¶Î§µÄÆðÄ©µã£¬ÇÒÖ»ÔÚÕâ¸ö·¶Î§ÄÚɨÃèÊý¾ÝÒ³£¬±ÜÃâÁË´ó·¶Î§É¨Ã裬Ìá¸ßÁ˲éѯËٶȡ£

---- 3.ÔÚplace£¬date£¬amountÉϵÄ×éºÏË÷Òý

select count(*) from record where date >
19991201 and date < 19991214 and amount >
2000 £¨26Ã룩
select date,sum(amount) from record group by date
£¨27Ã룩
select count(*) from record where date >
19990901 and place in (BJ, SH)£¨< 1Ã룩
---- ·ÖÎö£º
---- ÕâÊÇÒ»¸ö²»ºÜºÏÀíµÄ×éºÏË÷Òý£¬ÒòΪËüµÄÇ°µ¼ÁÐÊÇplace£¬µÚÒ»ºÍµÚ¶þÌõSQLûÓÐÒýÓÃplace£¬Òò´ËҲûÓÐÀûÓÃÉÏË÷Òý£»µÚÈý¸öSQLʹÓÃÁËplace£¬ÇÒÒýÓõÄËùÓÐÁж¼°üº¬ÔÚ×éºÏË÷ÒýÖУ¬ÐγÉÁËË÷Òý¸²¸Ç£¬ËùÒÔËüµÄËÙ¶ÈÊǷdz£¿ìµÄ¡£

---- 4.ÔÚdate£¬place£¬amountÉϵÄ×éºÏË÷Òý

select count(*) from record where date >
19991201 and date < 19991214 and amount >
2000(< 1Ãë)
select date,sum(amount) from record group by date
£¨11Ã룩
select count(*) from record where date >
19990901 and place in (BJ,SH)£¨< 1Ã룩
---- ·ÖÎö£º
---- ÕâÊÇÒ»¸öºÏÀíµÄ×éºÏË÷Òý¡£Ëü½«date×÷Ϊǰµ¼ÁУ¬Ê¹Ã¿¸öSQL¶¼¿ÉÒÔÀûÓÃË÷Òý£¬²¢ÇÒÔÚµÚÒ»ºÍµÚÈý¸öSQLÖÐÐγÉÁËË÷Òý¸²¸Ç£¬Òò¶øÐÔÄÜ´ïµ½ÁË×îÓÅ¡£

---- 5.×ܽ᣺

---- ȱʡÇé¿öϽ¨Á¢µÄË÷ÒýÊÇ·ÇȺ¼¯Ë÷Òý£¬µ«ÓÐʱËü²¢²»ÊÇ×î¼ÑµÄ£»ºÏÀíµÄË÷ÒýÉè¼ÆÒª½¨Á¢ÔÚ¶Ô¸÷ÖÖ²éѯµÄ·ÖÎöºÍÔ¤²âÉÏ¡£Ò»°ãÀ´Ëµ£º

---- ¢Ù.ÓдóÁ¿Öظ´Öµ¡¢ÇÒ¾­³£Óз¶Î§²éѯ

£¨between, >,< £¬>=,< =£©ºÍorder by
¡¢group by·¢ÉúµÄÁУ¬¿É¿¼Âǽ¨Á¢Èº¼¯Ë÷Òý£»
---- ¢Ú.¾­³£Í¬Ê±´æÈ¡¶àÁУ¬ÇÒÿÁж¼º¬ÓÐÖظ´Öµ¿É¿¼Âǽ¨Á¢×éºÏË÷Òý£»

---- ¢Û.×éºÏË÷ÒýÒª¾¡Á¿Ê¹¹Ø¼ü²éѯÐγÉË÷Òý¸²¸Ç£¬ÆäÇ°µ¼ÁÐÒ»¶¨ÊÇʹÓÃ×îƵ·±µÄÁС£

¶þ¡¢²»³ä·ÝµÄÁ¬½ÓÌõ¼þ£º
---- Àý£º±ícardÓÐ7896ÐУ¬ÔÚcard_noÉÏÓÐÒ»¸ö·Ç¾Û¼¯Ë÷Òý£¬±íaccountÓÐ191122ÐУ¬ÔÚ account_noÉÏÓÐÒ»¸ö·Ç¾Û¼¯Ë÷Òý£¬ÊÔ¿´ÔÚ²»Í¬µÄ±íÁ¬½ÓÌõ¼þÏ£¬Á½¸öSQLµÄÖ´ÐÐÇé¿ö£º

select sum(a.amount) from account a,
card b where a.card_no = b.card_no£¨20Ã룩
---- ½«SQL¸ÄΪ£º
select sum(a.amount) from account a,
card b where a.card_no = b.card_no and a.
account_no=b.account_no£¨< 1Ã룩
---- ·ÖÎö£º
---- ÔÚµÚÒ»¸öÁ¬½ÓÌõ¼þÏ£¬×î¼Ñ²éѯ·½°¸Êǽ«account×÷Íâ²ã±í£¬card×÷ÄÚ²ã±í£¬ÀûÓÃcardÉϵÄË÷Òý£¬ÆäI/O´ÎÊý¿ÉÓÉÒÔϹ«Ê½¹ÀËãΪ£º

---- Íâ²ã±íaccountÉϵÄ22541Ò³+£¨Íâ²ã±íaccountµÄ191122ÐÐ*ÄÚ²ã±ícardÉ϶ÔÓ¦Íâ²ã±íµÚÒ»ÐÐËùÒª²éÕÒµÄ3Ò³£©=595907´ÎI/O

---- ÔÚµÚ¶þ¸öÁ¬½ÓÌõ¼þÏ£¬×î¼Ñ²éѯ·½°¸Êǽ«card×÷Íâ²ã±í£¬account×÷ÄÚ²ã±í£¬ÀûÓÃaccountÉϵÄË÷Òý£¬ÆäI/O´ÎÊý¿ÉÓÉÒÔϹ«Ê½¹ÀËãΪ£º

---- Íâ²ã±ícardÉϵÄ1944Ò³+£¨Íâ²ã±ícardµÄ7896ÐÐ*ÄÚ²ã±íaccountÉ϶ÔÓ¦Íâ²ã±íÿһÐÐËùÒª²éÕÒµÄ4Ò³£©= 33528´ÎI/O

---- ¿É¼û£¬Ö»Óгä·ÝµÄÁ¬½ÓÌõ¼þ£¬ÕæÕýµÄ×î¼Ñ·½°¸²Å»á±»Ö´ÐС£

---- ×ܽ᣺

---- 1.¶à±í²Ù×÷ÔÚ±»Êµ¼ÊÖ´ÐÐÇ°£¬²éѯÓÅ»¯Æ÷»á¸ù¾ÝÁ¬½ÓÌõ¼þ£¬Áгö¼¸×é¿ÉÄܵÄÁ¬½Ó·½°¸²¢´ÓÖÐÕÒ³öϵͳ¿ªÏú×îСµÄ×î¼Ñ·½°¸¡£Á¬½ÓÌõ¼þÒª³ä·Ý¿¼ÂÇ´øÓÐË÷ÒýµÄ±í¡¢ÐÐÊý¶àµÄ±í£»ÄÚÍâ±íµÄÑ¡Ôñ¿ÉÓɹ«Ê½£ºÍâ²ã±íÖеÄÆ¥ÅäÐÐÊý*ÄÚ²ã±íÖÐÿһ´Î²éÕҵĴÎÊýÈ·¶¨£¬³Ë»ý×îСΪ×î¼Ñ·½°¸¡£

---- 2.²é¿´Ö´Ðз½°¸µÄ·½·¨-- ÓÃset showplanon£¬´ò¿ªshowplanÑ¡Ï¾Í¿ÉÒÔ¿´µ½Á¬½Ó˳Ðò¡¢Ê¹ÓúÎÖÖË÷ÒýµÄÐÅÏ¢£»Ïë¿´¸üÏêϸµÄÐÅÏ¢£¬ÐèÓÃsa½ÇÉ«Ö´ÐÐdbcc(3604,310,302)¡£

Èý¡¢²»¿ÉÓÅ»¯µÄwhere×Ó¾ä
---- 1.Àý£ºÏÂÁÐSQLÌõ¼þÓï¾äÖеÄÁж¼½¨ÓÐÇ¡µ±µÄË÷Òý£¬µ«Ö´ÐÐËÙ¶ÈÈ´·Ç³£Âý£º

select * from record where
substring(card_no,1,4)=5378(13Ãë)
select * from record where
amount/30< 1000£¨11Ã룩
select * from record where
convert(char(10),date,112)=19991201£¨10Ã룩
---- ·ÖÎö£º
---- where×Ó¾äÖжÔÁеÄÈκβÙ×÷½á¹û¶¼ÊÇÔÚSQLÔËÐÐʱÖðÁмÆËãµÃµ½µÄ£¬Òò´ËËü²»µÃ²»½øÐбíËÑË÷£¬¶øûÓÐʹÓøÃÁÐÉÏÃæµÄË÷Òý£»Èç¹ûÕâЩ½á¹ûÔÚ²éѯ±àÒëʱ¾ÍÄܵõ½£¬ÄÇô¾Í¿ÉÒÔ±»SQLÓÅ»¯Æ÷ÓÅ»¯£¬Ê¹ÓÃË÷Òý£¬±ÜÃâ±íËÑË÷£¬Òò´Ë½«SQLÖØд³ÉÏÂÃæÕâÑù£º

select * from record where card_no like
5378%£¨< 1Ã룩
select * from record where amount
< 1000*30£¨< 1Ã룩
select * from record where date= 1999/12/01
£¨< 1Ã룩

---- Äã»á·¢ÏÖSQLÃ÷ÏÔ¿ìÆðÀ´£¡

---- 2.Àý£º±ístuffÓÐ200000ÐУ¬id_noÉÏÓзÇȺ¼¯Ë÷Òý£¬Çë¿´ÏÂÃæÕâ¸öSQL£º

select count(*) from stuff where id_no in(0,1)
£¨23Ã룩
---- ·ÖÎö£º
---- whereÌõ¼þÖеÄinÔÚÂß¼­ÉÏÏ൱ÓÚor£¬ËùÒÔÓï·¨·ÖÎöÆ÷»á½«in (0,1)ת»¯Îªid_no =0 or id_no=1À´Ö´ÐС£ÎÒÃÇÆÚÍûËü»á¸ù¾Ýÿ¸öor×Ó¾ä·Ö±ð²éÕÒ£¬ÔÙ½«½á¹ûÏà¼Ó£¬ÕâÑù¿ÉÒÔÀûÓÃid_noÉϵÄË÷Òý£»µ«Êµ¼ÊÉÏ£¨¸ù¾Ýshowplan£©,ËüÈ´²ÉÓÃÁË"OR²ßÂÔ"£¬¼´ÏÈÈ¡³öÂú×ãÿ¸öor×Ó¾äµÄÐУ¬´æÈëÁÙʱÊý¾Ý¿âµÄ¹¤×÷±íÖУ¬ÔÙ½¨Á¢Î¨Ò»Ë÷ÒýÒÔÈ¥µôÖظ´ÐУ¬×îºó´ÓÕâ¸öÁÙʱ±íÖмÆËã½á¹û¡£Òò´Ë£¬Êµ¼Ê¹ý³ÌûÓÐÀûÓÃid_noÉÏË÷Òý£¬²¢ÇÒÍê³Éʱ¼ä»¹ÒªÊÜtempdbÊý¾Ý¿âÐÔÄܵÄÓ°Ïì¡£

---- ʵ¼ùÖ¤Ã÷£¬±íµÄÐÐÊýÔ½¶à£¬¹¤×÷±íµÄÐÔÄܾÍÔ½²î£¬µ±stuffÓÐ620000ÐÐʱ£¬Ö´ÐÐʱ¼ä¾¹´ïµ½220Ã룡»¹²»È罫or×Ó¾ä·Ö¿ª£º

select count(*) from stuff where id_no=0
select count(*) from stuff where id_no=1
---- µÃµ½Á½¸ö½á¹û£¬ÔÙ×÷Ò»´Î¼Ó·¨ºÏËã¡£ÒòΪÿ¾ä¶¼Ê¹ÓÃÁËË÷Òý£¬Ö´ÐÐʱ¼äÖ»ÓÐ3Ã룬ÔÚ620000ÐÐÏ£¬Ê±¼äÒ²Ö»ÓÐ4Ãë¡£»òÕߣ¬ÓøüºÃµÄ·½·¨£¬Ð´Ò»¸ö¼òµ¥µÄ´æ´¢¹ý³Ì£º
create proc count_stuff as
declare @a int
declare @b int
declare @c int
declare @d char(10)
begin
select @a=count(*) from stuff where id_no=0
select @b=count(*) from stuff where id_no=1
end
select @c=@a+@b
select @d=convert(char(10),@c)
print @d
---- Ö±½ÓËã³ö½á¹û£¬Ö´ÐÐʱ¼äͬÉÏÃæÒ»Ñù¿ì£¡
---- ×ܽ᣺

---- ¿É¼û£¬ËùνÓÅ»¯¼´where×Ó¾äÀûÓÃÁËË÷Òý£¬²»¿ÉÓÅ»¯¼´·¢ÉúÁ˱íɨÃè»ò¶îÍ⿪Ïú¡£

---- 1.ÈκζÔÁеIJÙ×÷¶¼½«µ¼Ö±íɨÃ裬Ëü°üÀ¨Êý¾Ý¿âº¯Êý¡¢¼ÆËã±í´ïʽµÈµÈ£¬²éѯʱҪ¾¡¿ÉÄܽ«²Ù×÷ÒÆÖÁµÈºÅÓұߡ£

---- 2.in¡¢or×Ӿ䳣»áʹÓù¤×÷±í£¬Ê¹Ë÷ÒýʧЧ£»Èç¹û²»²úÉú´óÁ¿Öظ´Öµ£¬¿ÉÒÔ¿¼ÂÇ°Ñ×Ó¾ä²ð¿ª£»²ð¿ªµÄ×Ó¾äÖÐÓ¦¸Ã°üº¬Ë÷Òý¡£

---- 3.ÒªÉÆÓÚʹÓô洢¹ý³Ì£¬ËüʹSQL±äµÃ¸ü¼ÓÁé»îºÍ¸ßЧ¡£

---- ´ÓÒÔÉÏÕâЩÀý×Ó¿ÉÒÔ¿´³ö£¬SQLÓÅ»¯µÄʵÖʾÍÊÇÔÚ½á¹ûÕýÈ·µÄÇ°ÌáÏ£¬ÓÃÓÅ»¯Æ÷¿ÉÒÔʶ±ðµÄÓï¾ä£¬³ä·ÝÀûÓÃË÷Òý£¬¼õÉÙ±íɨÃèµÄI/O´ÎÊý£¬¾¡Á¿±ÜÃâ±íËÑË÷µÄ·¢Éú¡£ÆäʵSQLµÄÐÔÄÜÓÅ»¯ÊÇÒ»¸ö¸´ÔӵĹý³Ì£¬ÉÏÊöÕâЩֻÊÇÔÚÓ¦Óòã´ÎµÄÒ»ÖÖÌåÏÖ£¬ÉîÈëÑо¿»¹»áÉæ¼°Êý¾Ý¿â²ãµÄ×ÊÔ´ÅäÖá¢ÍøÂç²ãµÄÁ÷Á¿¿ØÖÆÒÔ¼°²Ù×÷ϵͳ²ãµÄ×ÜÌåÉè¼Æ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. zotyu ÓÚ 2006-07-27 17:29:03·¢±í:

    ·¹ýѧϰ