Ò»£ºSQL tuning Àà
1£ºÁоټ¸ÖÖ±íÁ¬½Ó·½Ê½´ð£ºmerge join£¬hash join£¬nested loop
2£º²»½èÖúµÚÈý·½¹¤¾ß£¬ÔõÑù²é¿´sqlµÄÖ´Ðмƻ®£¿´ð£ºsqlplus
set autotrace ...
utlxplan.sql ´´½¨ plan_table ±í
3£ºÈçºÎʹÓÃCBO£¬CBOÓëRULEµÄÇø±ð£¿´ð£ºÔÚ³õʼ»¯²ÎÊýÀïÃæÉèÖà optimizer_mode=choose/all_rows/first_row µÈ¿ÉÒÔʹÓà cbo¡£
rbo»áÑ¡Ôñ²»ºÏÊʵÄË÷Òý£¬cboÐèҪͳ¼ÆÐÅÏ¢¡£
4£ºÈçºÎ¶¨Î»ÖØÒª£¨ÏûºÄ×ÊÔ´¶à£©µÄSQL£¿´ð£º¸ù¾Ýv$sqlarea ÖеÄÂß¼¶Á/disk_read¡£ÒÔ¼°Ñ°ÕÒCPUʹÓùýÁ¿µÄsession£¬²é³öµ±Ç°sessionµÄµ±Ç°SQLÓï¾ä£¬»òÕߣº¼à¿ØWINƽ̨OracleµÄÔËÐÐ
5£ºÈçºÎ¸ú×Ùij¸ösessionµÄSQL£¿´ð£ºÏÈÕÒ³ö¶ÔÓ¦µÄ'sid,serial'£¬È»ºóµ÷ÓÃsystem_system.set_sql_trace_in_session(sid,serial,true)£»²Î¿¼£º¸ú×Ùij¸ö»á»°
6£ºSQLµ÷Õû×î¹Ø×¢µÄÊÇʲô£¿´ð£ºÂß¼¶Á¡£IOÁ¿
7£ºËµËµÄã¶ÔË÷ÒýµÄÈÏʶ£¨Ë÷ÒýµÄ½á¹¹¡¢¶ÔdmlÓ°Ïì¡¢¶Ô²éѯӰÏ졢ΪʲôÌá¸ß²éѯÐÔÄÜ£©´ð£ºÄ¬ÈϵÄË÷ÒýÊÇb-tree¡£
¶ÔinsertµÄÓ°Ï죺·ÖÁÑ£¬Òª±£Ö¤treeµÄƽºâ¡£
¶ÔdeleteµÄÓ°Ï죺ɾ³ýÐеÄʱºòÒª±ê¼Ç¸Ä½ÚµãΪɾ³ý¡£
¶ÔupdateµÄÓ°Ï죺Èç¹û¸üбíÖеÄË÷Òý×ֶΣ¬ÔòÒªÏàÓ¦µÄ¸üÐÂË÷ÒýÖеļüÖµ¡£²éѯÖаüº¬Ë÷Òý×ֶεļüÖµºÍÐеÄÎïÀ ......
1. ×¼±¸ÖªÊ¶£ºORACLEµÄÂß¼´æ´¢¹ÜÀí.
ORACLEÔÚÂß¼´æ´¢ÉÏ·Ö4¸öÁ£¶È:±í¿Õ¼ä,¶Î,ÇøºÍ¿é.
1.1 ¿é:ÊÇÁ£¶È×îСµÄ´æ´¢µ¥Î»,ÏÖÔÚ±ê×¼µÄ¿é´óСÊÇ8K,ORACLEÿһ´ÎI/O²Ù×÷Ò²Êǰ´¿éÀ´²Ù×÷µÄ,Ò²¾ÍÊÇ˵µ±ORACLE´ÓÊý¾ÝÎļþ¶ÁÊý¾Ýʱ,ÊǶÁÈ¡¶àÉÙ¸ö¿é,¶ø²»ÊǶàÉÙÐÐ.
1.2 Çø:ÓÉһϵÁÐÏàÁÚµÄ¿é¶ø×é³É,ÕâÒ²ÊÇORACLE¿Õ¼ä·ÖÅäµÄ»ù±¾µ¥Î»,¾Ù¸öÀý×ÓÀ´Ëµ,µ±ÎÒÃÇ´´½¨Ò»¸ö±íPM_USERʱ,Ê×ÏÈORACLE»á·ÖÅäÒ»ÇøµÄ¿Õ¼ä¸øÕâ¸ö±í,Ëæ×Ų»¶ÏµÄINSERTÊý¾Ýµ½PM_USER,ÔÀ´µÄÕâ¸öÇøÈݲ»Ï²åÈëµÄÊý¾Ýʱ,ORACLEÊÇÒÔÇøÎªµ¥Î»½øÐÐÀ©Õ¹µÄ,Ò²¾ÍÊÇ˵ÔÙ·ÖÅä¶àÉÙ¸öÇø¸øPM_USER,¶ø²»ÊǶàÉÙ¸ö¿é.
1.3 ¶Î:ÊÇÓÉһϵÁеÄÇøËù×é³É,Ò»°ãÀ´Ëµ,µ±´´½¨Ò»¸ö¶ÔÏóʱ(±í,Ë÷Òý),¾Í»á·ÖÅäÒ»¸ö¶Î¸øÕâ¸ö¶ÔÏó.ËùÒÔ´ÓijÖÖÒâÒåÉÏÀ´Ëµ,¶Î¾ÍÊÇijÖÖÌØ¶¨µÄÊý¾Ý.ÈçCREATE TABLE PM_USER,Õâ¸ö¶Î¾ÍÊÇÊý¾Ý¶Î,¶øCREATE INDEX ON PM_USER(NAME),ORACLEͬÑù»á·ÖÅäÒ»¸ö¶Î¸øÕâ¸öË÷Òý,µ«ÕâÊÇÒ»¸öË÷Òý¶ÎÁË.²éѯ¶ÎµÄÐÅÏ¢¿ÉÒÔͨ¹ýÊý¾Ý×Öµä: SELECT * from USER_SEGMENTSÀ´»ñµÃ,
1.4 ±í¿Õ¼ä:°üº¬¶Î,Çø¼°¿é.±í¿Õ¼äµÄÊý¾ÝÎïÀíÉÏ´¢´æÔÚÆäËùÔÚµÄÊý¾ÝÎļþÖÐ.Ò»¸öÊý¾Ý¿âÖÁÉÙÒ ......
ORACLE ÖÐROWNUMÓ÷¨×ܽá!
¶ÔÓÚ Oracle µÄ rownum ÎÊÌ⣬ºÜ¶à×ÊÁ϶¼Ëµ²»Ö§³Ö>,>=,=,between...and£¬Ö»ÄÜÓÃÒÔÉÏ·ûºÅ(<¡¢<=¡¢!=)£¬²¢·Ç˵ÓÃ>,>=,=,between..and ʱ»áÌáʾSQLÓï·¨´íÎ󣬶øÊǾ³£ÊDz鲻³öÒ»Ìõ¼Ç¼À´£¬»¹»á³öÏÖËÆºõÊÇĪÃûÆäÃîµÄ½á¹ûÀ´£¬ÆäʵÄúÖ»ÒªÀí½âºÃÁËÕâ¸ö rownum αÁеÄÒâÒå¾Í²»Ó¦¸Ã¸Ðµ½¾ªÆæ£¬Í¬ÑùÊÇαÁУ¬rownum Óë rowid ¿ÉÓÐЩ²»Ò»Ñù£¬ÏÂÃæÒÔÀý×Ó˵Ã÷
¼ÙÉèij¸ö±í t1(c1) ÓÐ 20 Ìõ¼Ç¼
Èç¹ûÓà select rownum,c1 from t1 where rownum < 10, Ö»ÒªÊÇÓÃСÓںţ¬²é³öÀ´µÄ½á¹ûºÜÈÝÒ×µØÓëÒ»°ãÀí½âÔÚ¸ÅÄîÉÏÄÜ´ï³ÉÒ»Ö£¬Ó¦¸Ã²»»áÓÐÈκÎÒÉÎʵġ£
¿ÉÈç¹ûÓà select rownum,c1 from t1 where rownum > 10 (Èç¹ûдÏÂÕâÑùµÄ²éѯÓï¾ä£¬ÕâʱºòÔÚÄúµÄÍ·ÄÔÖÐÓ¦¸ÃÊÇÏëµÃµ½±íÖкóÃæ10Ìõ¼Ç¼)£¬Äã¾Í»á·¢ÏÖ£¬ÏÔʾ³öÀ´µÄ½á¹ûÒªÈÃÄúʧÍûÁË£¬Ò²ÐíÄú»¹»á»³ÒÉÊDz»ËɾÁËһЩ¼Ç¼£¬È»ºó²é¿´¼Ç¼Êý£¬ÈÔÈ»ÊÇ 20 Ìõ°¡£¿ÄÇÎÊÌâÊdzöÔÚÄÄÄØ£¿
ÏȺúÃÀí½â rownum µÄÒâÒå°É¡£ROWNUMÊÇÒ»¸öÐé¼ÙµÄÁÐ,Ëü½«±»·ÖÅäΪ 1£¬2£¬3£¬4£¬...N£¬N ÊÇÐеÄÊýÁ¿¡£¼´²éµ½½á¹û¼¯Ö®ºó,ÈκÎÅÅÐòºÍ¾ÛºÏ֮ǰ,¼ÓÉÏÈ¥µÄÒ»¸öÁÐ ( ......
1. Union Ö¸ÁîµÄÄ¿µÄÊǽ«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÆðÀ´,¿ÉÒԲ鿴ÄãÐèÒª²éѯ½á¹û.
ÀýÈç:
SELECT Date from Store_Information
UNION
SELECT Date from Internet_Sales
×¢Òâ:
unionÓ÷¨ÖÐ,Á½¸öselectÓï¾äµÄ×Ö¶ÎÀàÐÍÆ¥Åä,¶øÇÒ×ֶθöÊýÒªÏàͬ
ÈçÉÏÃæµÄÀý×Ó,ÔÚʵ¼ÊµÄÈí¼þ¿ª·¢¹ý³Ì,»áÓöµ½¸ü¸´ÔÓµÄÇé¿ö,¾ßÌåÇë¿´ÏÂÃæµÄÀý×Ó
select '1' as type,FL_ID,FL_CODE,FL_CNAME,FLDA.FL_PARENTID from FLDA
WHERE ZT_ID=2006030002
union
select '2' as type,XM_ID,XM_CODE ,XM_CNAME ,FL_ID from XMDA
where exists (select * from (select FL_ID from FLDA WHERE ZT_ID=2006030002 ) a where XMDA.fl_id=a.fl_id)
order by type,FL_PARENTID ,FL_ID
Õâ¸ö¾ä×ÓµÄÒâ˼Êǽ«Á½¸ösqlÓï¾äunion²éѯ³öÀ´,²éѯµÄÌõ¼þ¾ÍÊÇ¿´XMDA±íÖеÄFL_IDÊÇ·ñºÍÖ÷±íFLDAÀïµÄFL_IDÖµÏàÆ¥Åä,(Ò²¾ÍÊÇ´æÔÚ).
UNIONÔÚ½øÐбíÁ´½Óºó»áɸѡµôÖØ¸´µÄ¼Ç¼£¬ËùÒÔÔÚ±íÁ´½Óºó»á¶ÔËù²úÉúµÄ½á¹û¼¯½øÐÐÅÅÐòÔËË㣬ɾ³ýÖØ¸´µÄ¼Ç¼ÔÙ·µ»Ø½á¹û¡£
2. ÔÚ²éѯÖлáÓöµ½ UNION ALL,ËüµÄÓ÷¨ºÍunionÒ»Ñù,Ö»²»¹ýunionº¬ÓÐdistinctµÄ¹¦ÄÜ,Ëü»á°ÑÁ½ÕűíÁËÖØ¸´µÄ¼Ç¼ȥµô,
&nb ......
CREATE OR REPLACE Procedure PRCGETV6(
inp_UserCD IN VARCHAR2,
inp_SEIHIN IN VARCHAR2,
inp_SOKOCD IN VARCHAR2,
inp_JITUZAI IN VARCHAR2,
inp_GYOSU IN VARCHAR2,
out_KOSHINYMD OUT VARCHAR2,
out_ERR OUT VARCHAR2
) Is
CONST_OPERATION_NAME CONSTANT VARCHAR2(30):= 'PRCGETV6'; --²Ù×÷識別×Ó
-- out_KOSHINYMD VARCHAR2(14);
-- out_ERR VARCHAR2(10);
varUSERCD VARCHAR2(10);
varSEIHIN VARCHAR2(11);
varSOKOCD VARCHAR2(1);
varKOSHINHI VARCHAR2(14);
numJITUZAI NUMBER;
numKEISANZAI NUMBER;
numGYOSU NUMBER;
numSEQ NUMBER;
CURSOR CUR1 IS
Select
KBN,
JUBI,
YOBI,
MENO,
MNYUKO,
DENPYONO,
MSYUKO,
EGYCD,
ZAISU,
KBN1,
KBN ......
SVRMGR> select * from dba_jobs;
³õʼ»¯Ïà¹Ø²ÎÊýjob_queue_processes
alter system set job_queue_processes=39 scope=spfile;//×î´óÖµ²»Äܳ¬¹ý1000 ;job_queue_interval = 10 //µ÷¶È×÷ҵˢÐÂÆµÂÊÃëΪµ¥Î»
DBA_JOBS describes all jobs in the database.
USER_JOBS describes all jobs owned by the current user
1 select job,what,to_char(last_date,'yyyy-mm-dd HH24:mi:ss'),to_char(next_date,'yyyy-mm-dd HH24:m),interval from dba_jobs where job in (325,295)
2 select job,what,last_date,next_date,interval from dba_jobs where job in (1,3);
²éѯjobµÄÇé¿ö¡£
show paramter background_dump_dest.
¿´alter.log ºÍtrace
SVRMGR> select * from dba_jobs;
³õʼ»¯Ïà¹Ø²ÎÊýjob_queue_processes
alter system set job_queue_processes=39 scope=spfile;//×î´óÖµ²»Äܳ¬¹ý1000
job_queue_interval = 10 //µ÷¶È×÷ҵˢÐÂÆµÂÊÃëΪµ¥Î»
DBA_JOBS describes all jobs in the database.
USER_JOBS describes all jobs owned by the current user
1 select job,what,to_char(last_date,'yyyy-mm-dd HH24:mi:ss'),to_char(next_date,'yyyy-mm-dd HH24 ......