SIÇÁ·ÎÁ§Æ® ½Ç¹«°æÇè¹ÙÅÁ Àü¹®ÀÚ¹Ù°³¹ßÀÚ°øµ¿Ã¼    
    WASÀü¹®±â¼ú/½Ã½ºÅÛÀå¾ÖÁø´Ü/¼º´ÉÀÌ·Ð/ÄÁ¼³ÆÃ
¾ÆÀ̵ð: 
ºñ¹Ð¹øÈ£: 
[ȸ¿ø°¡ÀÔ]
¡ß DBMS
---------------
- DataBase
- JDBC @
Áú¹®Àº [¹¯°í´äÇϱâ]¶õ¿¡¸¸ ¿Ã·ÁÁÖ¼¼¿ä









ÃÖ±Ù¿¡ Å͵æÇÑ »õ·Î¿î °ÍÀÌ ÀÖ³ª¿ä? ¸Ó¸® ¼Ó¿¡ ³Ö¾î µÎ¸é ±Ý¹æ Àؾî¹ö¸®ÁÒ? ÀÛÀº °ÍÀÌ¶óµµ ±¦ÂúÀ¸´Ï À̰÷¿¡ ¿Ã·Á³õ°í ³²µé°ú °øÀ¯ÇϽÃÁö¿ä.. ¿Ã¸±¸¸ÇÑ ¸Þ´º°¡ ¾ø´Ù±¸¿ä? ¸¸µé¾î µå¸±²²¿ä.


[ÃÖ±Ù ¿Ã¶ó¿Â ±Ûº¸±â] °Ë»ö¾î:
 JavaResource | API Tips | Open Source | APM | ApplicationServer | Unix/DB/JVM/Etc | Q&A/Help
¡ß Java Resources
--------------------
- °øÁö»çÇ×
- ÃÖ±Ù IT ¼Ò½Ä
- ¼¼¹Ì³ª¼Ò½Ä
- À̺¥Æ® & ÇÁ·Î¸ð¼Ç

- Ãßõ¹®¼­(2006)
- Ãßõ¹®¼­(2003-2005)
- Ãßõ¹®¼­(±¸)

- °ü·Ã »çÀÌÆ®

- ¹¯°í´äÇϱâ @
- ¹¯°í´äÇϱâ-BACKUP @
- ±â¼ú Åä·ÐÀå @
- ÀÏ¹Ý Åä·ÐÀå @
- »çȸ Åä·ÐÀå @


- ÇÁ·ÎÁ§Æ®½Ç¹«ÄÁ¼³ÆÃ@

- ±³À°/°­ÀÇ/°­ÁÂ
- ÇÁ·ÎÁ§Æ®/¼Ö·ç¼ÇÁ¦¾È
- ±¸Àζõ
- ±¸Á÷¶õ

- Àǰ߳ª´®ÅÍ/Àâ´ã

- ÀÚ¹Ù¼­ºñ½º³ÝÀº...
¡ß Java API Tips
--------------------
- Programming Tips
- JDC Tech Tips

- Servlet/JSP
- J2EE/EJB
- XML/SOAP/UDDI/WSDL
- Jakarta POI
- JDBC
- Mobile Java
- Applet,Swing,SWT
- CORBA
- RMI
- JavaMail
- HTML/HTC/css/js
- Web ÀϹÝ

- ¼­ºí·¿¿£Áø @
(JServ,Tomcat,JRun,..)
¡ß Open Source
----------------------
- Eclipse/Plugin
- Apache Struts
- JDF Framework
  - Download / API

- Open Source Q&A
* ¿ÀǼҽº °Ô½ÃÆÇ
  ½Åû¹Þ¾Æ¿ä.
¡ß APM
----------------------
- Performance Forum
- Jennifer
  - Consulting Doc.
  - »ç¿ëÈıâ/±â´ÉÃß°¡¿äû
  - Jennifer FAQ
  - Jennifer Q&A
  - Jennifer Download
- CA/Wily
- Mercury Topaz/J2EE Diag.
- Symantec i3
- Borland Optimizeit
- Compuware Vantage
¡ßApplicationServer
-------------------- 
- ¾îÇø®ÄÉÀ̼Ǽ­¹ö

- ATG Dynamo
- BEA WebLogic
- BolandEnterpriseServer
- Evermind Orion Server
- Fujitsu Interstage
- GemStone's GemStone/J
- HP Netaction
- IBM WebSphere
- IONA iPortal
- InfronTech WebTide
- Macromedia JRun
- Marc Fleury's JBoss
- Oracle 9iAS/OAS/OSDK
- Persistence PowerTier
- SilverStream eXtend
- Sun/Netscape iPlanet
- Sybase EAServer
- TmaxSoft JEUS

- ¼­ºí·¿¿£Áø
(JServ,Tomcat,Resin,..)

- eclipse/Plugin
- Other IDE Tools
¡ßUnix/DB/JVM/Etc
--------------
- Unix/Network
- Hacking/Cracking

- DataBase
- JDBC @

- JVM/JDK Issue
- ±âŸ ÀÚ·á½Ç
¡ß Q&A/Help
--------------------
- ¹¯°í´äÇϱâ
- ¹¯°í´äÇϱâ-BACKUP
- Åä·ÐÀå
- ÇÁ·ÎÁ§Æ®½Ç¹«ÄÁ¼³ÆÃ
- Framework Q&A @
- Jennifer Q&A @

- °Ô½ÃÆÇ »ç¿ë¹ý
- ÀÚ¹Ù¼­ºñ½º³ÝÀº... @
JDBC(Java Database Connectivity)
  [±Û¸ñ·Ï /½Ã°£¼ø] [´ä±Û¾²±â] [ÇÁ¸°Æ®]   
Á¦¸ñ : Prepared StatementsÀÇ Á߿伺
±Û¾´ÀÌ: ¼­Á¤Èñ(to2space) 2002/04/12 23:42:17 Á¶È¸¼ö:6856 ÁÙ¼ö:389

-----------------------------------------------
************************************************
¼­Á¤Èñ 
ÇÑ¼Ö ÅÚ·¹ÄÄ 

Why Prepared Statements are important and how to use them "properly"
À̱ÛÀº www.ejbinfo.comÀÇ ±â»ç¸¦ ¹ø¿ªÇÑ °ÍÀÔ´Ï´Ù.
v 1.0



Technical translation
¼­Á¤Èñ

¹ø¿ª ȤÀº ±â¼ú»óÀÇ ¿À·ù¿¡ ´ëÇØ¼­ feedbackÀ» ÁÖ¼¼¿ä.
to2space@kornet.net

JDBC¿¡ °ü·ÃµÈ »çÇ×À» ´õ º¸½Ç·Á¸é..
http://home.megapass.co.kr/~to2space/


¹ø¿ªÇÏ´Â ÀÌÀÇ ¼ö°íµµ »ý°¢ÇÏ´Â Àǹ̿¡¼­ ²À Ãâó´Â ¸í±âÇØ Áֽʽÿä.
************************************************
------------------------------------------------

¿ø¹®Àº ÇÏ´Ü¿¡ ÀÖ½À´Ï´Ù.
Prepared Statement»ç¿ëÀÇ ±âº»ÀûÀÎ ¿ø¸®¸¦ ±â¼úÇÏ´Â ³»¿ëÀÔ´Ï´Ù.
JDBCÀÇ ÀÌÇØ¸¦ µ½±â À§ÇØ ¹ø¿ªÇØ º¸¾Ò½À´Ï´Ù.



 
Why Prepared Statements are important and how to use them "properly".

¿Ö PreparedStatements´Â Áß¿äÇÏ°í ¾î¶»°Ô À̵éÀ» ¡°ÀûÀýÈ÷¡± ÀÌ¿ëÇØ¾ßÇÒ±î¿ä?
µ¥ÀÌÅÍ º£À̽º´Â ´Ù·ç±â Èûµé´Ù. ¸¹Àº ¼öÀÇ Å¬¶óÀÌ¾ðÆ®·ÎºÎÅÍ µ¿½Ã¿¡
sqlÄõ¸®¹®À» ¹Þ¾ÆµéÀÌ°í ¸¹Àº µ¥ÀÌÅÍ¿¡ ´ëÇØ¼­ °¡´ÉÇÑ È¿À²ÀûÀ¸·Î Äõ¸®¸¦ ½ÇÇàÇÑ´Ù.
StatementµéÀ» ¼öÇàÇÏ´Â °ÍÀº ÆÛÆ÷¸Õ½º¸¦ ¸¹ÀÌ Àâ¾Æ ¸Ô´Â ÀÛ¾÷ÀÌÁö¸¸ µ¥ÀÌÅÍ º£À̽ºµéÀº
ÇöÀç °¡´ÉÇÏ´Ù¸é ÀÌ·± ¿À¹öÇìµå¸¦ ÁÙÀÌ´Â ¹æ½ÄÀ¸·Î ±â·ÏµÇ¾îÁø´Ù. 
±×·¯³ª ¸¸¾à ¿ì¸®°¡ ÃÖÀûÈ­ °úÁ¤¿¡ ÁßÁ¡À» µÐ´Ù¸é ÀÌ·¯ÇÑ °úÁ¤Àº °³¹ßÀÚÀÇ µµ¿òÀ» 
ÇÊ¿ä·Î ÇÑ´Ù. ÀÌ ±â»ç´Â Á¤È®ÇÑ PreparedStatementsÀÇ »ç¿ëÀÌ ¾ó¸¶³ª µ¥ÀÌÅÍ º£À̽º°¡
 ÀÌ·¯ÇÑ ÃÖÀûÈ­ °úÁ¤À» µµ¿Í ÁÙ ¼ö ÀÖ´ÂÁö¸¦ º¸¿©ÁØ´Ù.


How does a database execute a statement?

¾î¶»°Ô µ¥ÀÌÅÍ º£À̽º°¡ statement¸¦ ¼öÇàÇÒ±î¿ä?
ºÐ¸íÈ÷, ÀÌ ±Û¿¡¼­´Â ¸¹Àº ¼¼ºÎÀûÀÎ °ÍÀ» ±â´ëÇÏÁö ¸¶½Ê½Ã¿À. 
¿ì¸®´Â ´ÜÁö ÀÌ ±â»çÀÇ Áß¿äÇÑ Ãø¸éÀ» °ËÁõÇϱ⸸ ÇÒ °ÍÀÔ´Ï´Ù
µ¥ÀÌÅÍ º£À̽º°¡ ÇϳªÀÇ statement¸¦ ¹ÞÀ» ¶§ µ¥ÀÌÅÍ º£À̽º ¿£ÁøÀº óÀ½ ¹®ÀåÀ»
 parse½Ã۰í À߸øµÈ ¹®¹ýÀ» °Ë»çÇÑ´Ù. ÀÏ´Ü statement°¡ parseµÇ¾îÁö°í 
±×·± ´ÙÀ½ µ¥ÀÌÅÍ º£À̽º´Â statement ½ÇÇàÇϱâ À§ÇÑ °¡Àå È¿À²ÀûÀÎ ¹æ¹ýÀ» ¾Ë¾Æ¾ßÇÑ´Ù. 
À̰ÍÀº °è»ê»ó ¾ÆÁÖ Å« ºñ¿ëÀÌ µé¼ö°¡ ÀÖ´Ù.(¼­¹ö ¼º´ÉÀ» ¼ø°£ÀûÀ¸·Î ¸¹ÀÌ Àâ¾Æ¸Ô´Â´Ù)
µ¥ÀÌÅÍ º£À̽º´Â µµ¿òÀÌ µÈ´Ù¸é ¾î¶² À妽º¸¦ ¾µ°ÍÀÎÁö ȤÀº Å×ÀÌºí¿¡ ÀÖ´Â ¸ðµç row¸¦
´Ù Àоî¾ß ÇÏ´Â Áö¸¦ È®ÀÎÇÑ´Ù. °á±¹ µ¥ÀÌÅÍ º£À̽º´Â ¹«¾ùÀÌ °¡Àå ÁÁÀº ¹æ¹ýÀÎÁö¸¦ 
ÀÌÇØÇϴµ¥, ƯÁ¤ÇÑ µ¥ÀÌÅÍ¿¡ ´ëÇØ¼­ Åë°è¸¦ ÀÌ¿ëÇÑ´Ù.
ÀÏ´Ü Äõ¸® Ç÷£query plan( ¿ªÀÚÁÖ query¸¦ ¾ó¸¶³ª È¿À²ÀûÀ¸·Î ÇÒ ¼ö ÀÖ´ÂÁö¿¡ 
´ëÇÑ Ç÷£, ¹Ì¸® µ¥ÀÌÅÍ º£À̽º´Â Ç÷£À» ¼¼¿î ÈÄ Äõ¸®¸¦ ÁøÇàÇÑ´Ù.)ÀÌ ÀÌ·ç¾îÁö¸é 
À̰ÍÀº µ¥ÀÌÅÍ º£À̽º¿£Áø¿¡ ÀÇÇØ ¼öÇàµÇ¾îÁø´Ù.
ÀÌ·± µ¥ÀÌÅÍ¿¡ ´ëÇÑ Á¢±Ù °èȹÀÇ »ý¼ºÀº cpu Àü¿øÀ» ¼Ò¸ð ½ÃŲ´Ù.
°ü³äÀûÀ¸·Î ¸¸¾à ¿ì¸®°¡ ¶È°°Àº statement ¸¦ µ¥ÀÌÅÍ º£À̽º¿¡ µÎ¹øÂ°·Î º¸³½´Ù¸é
¿ì¸®´Â µ¥ÀÌÅÍ º£À̽º°¡ ù¹ø ° statement¿¡´ëÇÑ Á¢±Ù °èȹÀ» Àç»ç¿ëÇϵµ·Ï ÇÏ´Â °ÍÀ» 
¼±È£ÇÒ °ÍÀÌ´Ù. À̰ÍÀº ¶Ç Çѹø Á¢±Ù °èȹÀ» »ý¼º ½ÃŰ´Â °Í º¸´Ù ÀûÀº cpuÁ¡À¯À²À» 
»ç¿ëÇÒ °ÍÀÌ´Ù.


Statement Caches
µ¥ÀÌÅÍ º£À̽ºµéÀº ÀÌ·± ¹æ½ÄÀ¸·Î Á¶ÀýµÇ¾îÁø´Ù.
 µ¥ÀÌÅÍ º£À̽º´Â º¸Åë ÀÏÁ¾ÀÇ statement cacheÀ» °¡Áö°í ÀÖ´Ù. 
ÀÌ cache´Â statement ÀÚü¸¦ Ű·Î½á »ç¿ëÇÏ°í µ¥ÀÌÅÍ¿¡ ´ëÇÑ Á¢±Ù °èȹÀº ÀÌ cache ¼Ó¿¡
 ´ëÀÀµÇ´Â statement¿Í ÇÔ²² ÀúÀåµÇ¾î ÀÖ´Ù.
À̰ÍÀº µ¥ÀÌÅÍ º£À̽º ¿£ÁøÀÌ ÀÌÀü¿¡ ½ÇÇà µÇ¾ú´ø statementµé¿¡ ´ëÇÑ Ç÷£À»
 Àç»ç¿ëÇÏ°Ô ÇÑ´Ù. ¿¹¸¦ µé¾î ¸¸¾à ¿ì¸®°¡ µ¥ÀÌÅÍ º£À̽º¿¡ 
¡°select a,b from t where c = 2¡±¿Í °°Àº statement¸¦ º¸³Â´Ù¸é °è»êµÈ ½ÇÇà Ç÷£Àº
 cache¿¡ ÀúÀåµÇ¾îÁø´Ù. ¸¸¾à ¿ì¸®°¡ ¶È°°Àº statementÀ» ¶Ç´Ù½Ã º¸³»¸é µ¥ÀÌÅÍ º£À̽º´Â
 Áö³­ ¹ø¿¡ ¾´ Á¢±Ù °èȹÀ» Àç»ç¿ëÇÒ ¼ö ÀÖ°Ô µÇ¾î¼­ cpu »ç¿ëÀ²À» ÁÙÀÌ°Ô ÇØÁØ´Ù.

Àüü statement°¡ ۶ó´Â °ÍÀ» ÁÖÁöÇϰí, ¿¹¸¦ µé¾î¼­ ¿ì¸®°¡ ³ªÁß¿¡ 
¡°select a,b from t where c = 3¡± ¶ó´Â statement¸¦ º¸³Â´Ù¸é 
À̰ÍÀº Á¢±Ù °èȹÀ» ¹ß°ß ÇÒ ¼ö ¾øÀ»Áöµµ ¸ð¸¦ °ÍÀÌ´Ù. À̰ÍÀº c=3ÀÌ cache¿¡ ÀúÀåµÈ 
Ç÷£ÀÇ c=2¿Í ´Ù¸£±â ¶§¹®ÀÌ´Ù.
¿¹¸¦ µé¾î,
for(int I = 0; I < 1000; ++I) {
 PreparedStatement ps = conn.prepareStatement("select a,b from t where c = " + I);
 ResultSet rs = Ps.executeQuery();
 rs.close();
 ps.close();
 } 

¿©±â¿¡¼­´Â cache´Â »ç¿ëµÇÁö ¾ÊÀ» °ÍÀÌ´Ù. For loopÀÇ °¢°¢ÀÇ I°ª¿¡ ÇØ´çÇÏ´Â 
´Ù¸¥ sql¹®À» µ¥ÀÌÅÍ º£À̽º¿¡ º¸³½´Ù. »õ·Î¿î Á¢±Ù °èȹÀÌ °¢°¢ÀÇ I °ªÀÇ ÇØ´çÇÏ´Â 
sql¿¡ ÀÇÇØ °è»êµÇ¾îÁö°í ±âº»ÀûÀ¸·Î ¿ì¸®´Â ÀÌ·± Á¢±Ù ¹æ½ÄÀ» ÀÌ¿ëÇϹǷνá cpu »ç¿ëÀ»
 ¼Ò¸ð½ÃÄÑ ¹ö¸°´Ù. ±×·¸Áö¸¸, ´ÙÀ½ ¿¹Á¦¸¦ º¸¸é
PreparedStatement ps = conn.prepareStatement("select a,b from t where c = ?");
for(int I = 0; I < 1000; ++I) {
 ps.setInt(1, I); 
 ResultSet rs = ps.executeQuery();
 rs.close();
 ps.close(); 
}
À§ÀÇ ¿¹Á¦´Â ´õ¿í´õ È¿À²ÀûÀÏ °ÍÀÌ´Ù. µ¥ÀÌÅÍ º£À̽º¿¡ º¸³»¾îÁö´Â À§ÀÇ sql¹®¿¡¼­ ? 
Ç¥½Ã´Â ´ëÀÀµÇ´Â ÆÄ¶ó¸ÞÅÍ °ªÀ» ¹Þ°í ÀÖ´Ù. À̰ÍÀº ¸ðµç iteration¿¡ ÇØ´çÇÏ´Â sql 
¹®ÀåÀÌ ÇϳªÀÇ °°Àº sql¹®ÀåÀ¸·Î "c=?" ¿¡ ´ëÀÀµÇ´Â ´Ù¸¥ ÆÄ¶ó¸ÞŸ °ª°ú ÇÔ²²
 µ¥ÀÌÅÍ º£À̽º¿¡ º¸³»¾îÁø´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. À̰ÍÀº µ¥ÀÌÅÍ º£À̽º°¡ À§
 ¿¹Á¦ÀÇ ÇϳªÀÇ ¹®Àå¿¡ ´ëÇÑ Á¢±Ù °èȹÀ» ÀÌ¿ëÇÏ°Ô Çϰí ÇÁ·Î±×·¥ÀÌ µ¥ÀÌÅÍ º£À̽º 
³»ºÎ¿¡¼­ ´õ¿í ´õ È¿À²ÀûÀ¸·Î ¼öÇàµÇ°Ô ÇÑ´Ù. À̰ʹ ±âº»ÀûÀ¸·Î 
¿©·¯ºÐÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀÌ ´õ ºü¸£°Ô ¼öÇàµÇ°Ô Çϰųª ¶Ç´Â ´õ ¸¹Àº
 cpu ÀÇ ºñÁ¡À¯ ºÎºÐÀ» µ¥ÀÌÅÍ º£À̽ºÀÇ ´Ù¸¥ À¯Àú¿¡°Ô Á¦°ø µÇ°ÔÇÑ´Ù.


PreparedStatements and J2EE servers

¿ì¸®°¡ J2EE¼­¹ö¸¦ ÀÌ¿ëÇÒ ¶§ »óȲÀº Á»´õ º¹ÀâÇØÁú¼ö ÀÖ´Ù. 
º¸Åë a prepared statement´Â ÇϳªÀÇ ´ÜÀÏ µ¥ÀÌÅÍ º£À̽º connection°ú °ü·ÃÀÌ ÀÖ´Ù.
ConnectionÀÌ ´ÝÇûÀ» ¶§ a prepared statement´Â ¹ö·ÁÁø´Ù.
º¸Åë a fat Client¹«°Å¿î Ŭ¶óÀÌ¾ðÆ® ¾ÖÇø®ÄÉÀ̼Ç(¿ªÀÚÁÖ ¼­¹ö¿¡ ÁÖ¿ä ºñÁî´Ï½º ·ÎÁ÷À» 
µÎÁö ¾Ê°í Ŭ¶óÀÌ¾ðÆ®¿¡ ÁÖ¿ä ºñÁî´Ï½º ·ÎÁ÷À» µÐ ÇüÅÂ)Àº connectionÀ» Çϳª °¡Áö°í 
¾ÖÇø®ÄÉÀ̼ÇÀÇ Á¾·á ½ÃÁ¡±îÁö °¡Áö°í ÀÖ´Ù.
À̰ÍÀº ¶ÇÇÑ ¸ðµç prepared statementµéÀ» ¡°°Ý·ÂÇϰԡ± ȤÀº ¡°¿Ï¸¸Çϰԡ± ¸¸µç´Ù. 
¡°°Ý·ÄÇϰԡ± eagerly¶ó´Â Àǹ̴ ¸ðµç prepared statementµéÀÌ ¾ÖÇø®ÄÉÀ̼ÇÀÌ 
½ÃÀÛÇÒ ¶§ Çѹø¿¡ ¸¸µé¾î Áø´Ù´Â °ÍÀÌ°í ¡°¿Ï¸¸Çϰԡ± 
lazilyÀÇ Àǹ̴ a prepared statementµéÀÌ »ç¿ëµÉ ¶§¿¡ ¸¸µé¾îÁø´Ù´Â °ÍÀÌ´Ù. 
ÀüÀÚ´Â ¾ÖÇø®ÄÉÀ̼ÇÀÌ ±âµ¿ÇÒ ¶§ Áö¿¬ Çö»óÀ» ÁÖ³ª ±âµ¿Çϸé ÃÖÀûÀÇ Á¶°Ç¿¡¼­ Àß ¼öÇàµÈ´Ù. 
(¿ªÀÚ ´ç¿¬È÷ ¸ðµç prepared statementµéÀÌ ¸¸µé¾îÁö¹Ç·Î ´À¸®´Ù)
ÈÄÀÚ´Â ¾î¶°ÇÑ »çÀüÀÇ Áغñ°¡ ÀÌ·ç¾î ÁöÁö ¾Ê´Â´Ù ±×·¯³ª ¾ÖÇø®ÄÉÀ̼ÇÀÇ µ¿ÀÛÇÔ¿¡ µû¶ó
  prepared statementµéÀÌ ¾ÖÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ óÀ½À¸·Î »ç¿ëµÇ¾îÁú ¶§ 
prepared statementµéÀº ¸¸µé¾îÁø´Ù. À̰ÍÀº ¸ðµç ¹®ÀåµéÀÌ ÁغñµÇ¾úÀ» ¶§±îÁö °øÆòÇÏÁö
 ¸øÇÑ ¼º´ÉÀ» ³ªÅ¸³½´Ù. ±×·¯³ª °á±¹¿£ ¾ÖÇø®ÄÉÀ̼ÇÀÌ ¾ÈÁ¤ÀûÀ¸·Î ¼öÇàµÇ°í ¡°°Ý·ÄÇÏ°Ô 
¼öÇàµÇ´Â ¾ÖÇø®ÄÉÀ̼ǡ± º¸´Ù ºü¸¥ ¼º´ÉÀ» ³ªÅ¸³½´Ù. ¾î¶² °ÍÀÌ ÃÖ°í³Ä´Â ¿©·¯ºÐÀÌ 
¡°ºü¸¥ ½ÃÀÛÀ» ¿øÇϴ°¡?¡± ¶Ç´Â ¡°Æò±ÕÀûÀÎ ¼º´ÉÀ» ¿øÇÏ´Â °¡?¡±¿¡ ´Þ·ÁÀÖ´Ù.
J2EE ¾ÖÇø®ÄÉÀ̼ǿ¡ À־ÀÇ ¹®Á¦´Â ÀÌó·³ µ¿ÀÛÇÏÁö ¾Ê´Â °ÍÀÌ´Ù.
 À̰ÍÀº ´ÜÁö ÇϳªÀÇ ¿äû ±â°£ µ¿¾È ÇϳªÀÇ connection¿¡ ´ëÇØ¼­ À¯ÁöµÈ´Ù. 
ÀÌ´Â ¿äûÀÌ ½ÇÇàµÉ ¶§ ¸¶´Ù prepared statementµéÀ» ¹Ýµå½Ã ¸¸µé¾î¾ß ÇÑ´Ù´Â Àǹ̴Ù. 
À̰ÍÀº ¸Å¹ø ¿äûÀÌ µÉ ¶§ ¸¶´Ù ¸¸µé¾îÁö´Â °Íº¸´Ù prepared statementµéÀÌ Çѹø¿¡
 ¸¸µé¾îÁö´Â ¹«°Å¿î Ŭ¶óÀÌ¾ðÆ® Á¢±Ù ¹æ½ÄÀÇ °æ¿ì ¸¸Æ´ È¿À²ÀûÀÌÁö ¾Ê´Ù. 
J2EE º¥´õµéÀº À̸¦ ¾Ë°í ¼º´É °¨¼Ò¸¦ ÇÇÇϱâ À§Çؼ­ connetion poolingÀ» 
µðÀÚÀÎ ÇÏ¿´´Ù. J2EE ¼­¹ö´Â ¿©·¯ºÐÀÇ ¾ÖÇø®ÄÉÀ̼ǿ¡ ÇϳªÀÇ connectionÀ» Á¦°øÇßÀ» ¶§,
 À̰ÍÀº ½ÇÁúÀûÀÎ connectionÀÌ ¾Æ´Ï¶ó ¿©·¯ºÐÀº ÇϳªÀÇ wrapper(¿ªÀÚÁÖ: ½ÇÁúÀûÀÎ 
connectionÀÌ ¾Æ´Ñ º¥´õµé¿¡ ÀÇÇØ ¸¸µé¾îÁö »õ·Î¿î
Ŭ·¡½º¿¡ ÀÇÇØ ½×¿©Áø wrapped°ÍÀ» ¸»ÇÑ´Ù) ¸¦ ¾òÀº °ÍÀÌ µÈ´Ù.
¿©·¯ºÐÀº À̰ÍÀ» ÁÖ¾îÁø connection¿¡ ´ëÇÑ Å¬·¡½ºÀÇ À̸§À» º¸°í È®ÀÎ ÇÒ ¼ö ÀÖ´Ù.
 À̰ÍÀº JDBC connecion´Â ¾Æ´Ï°í ¿©·¯ºÐÀÇ application server¿¡ ÀÇÇØ ¸¸µé¾îÁø 
Ŭ·¡½ºÀÏ °ÍÀÌ´Ù.
¸ðµç JDBC°´Ã¼´Â applicaion serverÀÇ Ä¿³Ø¼Ç ¸Þ´ÏÁ®¿¡ ÀÇÇØ °ü¸®µÇ¾îÁú °ÍÀÌ´Ù. 
¸ðµç JDBC ResultSets, statements, CallableStatements, preparedStatementsµîÀº 
wrapperµÇ¾î¼­ proxy°´Ã¼(¿ªÀÚ ÁÖ ¿©±â¼­´Â ½ÇÁúÀûÀÎ JDBCÀÇ °´Ã¼µéconnection,
resultsetµîÀ» ´ë½ÅÇØ¼­ application server¿¡¼­ ¸¸µé¾îÁø Ŭ·¡½º)·Î¼­ 
¾ÖÇø®ÄÉÀ̼ÇÀ¸·Î Á¦°øµÉ °ÍÀÌ´Ù. ¿©·¯ºÐÀÌ ConnectionÀ» ´ÝÀ» ¶§ ÀÌµé °´Ã¼´Â 
¹«¿ëÇÏ´Ù°í Ç¥½ÃµÇ°í jvm¿¡ ÀÇÇÑ garbage collect°¡ ÀÌ·ç¾îÁø´Ù.
º¸Åë ¿©·¯ºÐÀÌ ÇϳªÀÇ connection¸¦ ´ÝÀ¸¸é jdbc driver´Â connectionÀ» ´Ý´Â´Ù.
 ¿ì¸®´Â ÀÌ connectionÀÌ J2EE ¾ÖÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ close°¡ ºÒ·ÁÁú ¶§ poolÇ®·Î
 µÇµ¹¾Æ°¥¼ö Àֱ⸦ ¿øÇÑ´Ù. ÀÌ·¸°Ô Çϱâ À§Çؼ­ ½ÇÁ¦ connection°ú À¯»çÇÑ 
proxy connetionÀ» ¸¸µç´Ù.
À̰ÍÀº ½ÇÁ¦ÀÇ connectionÀÇ reference¸¦ °¡Áø´Ù. ¿ì¸®°¡ connection¿¡ ´ëÇØ¼­ ¾î¶² 
¸Þ¼Òµå¸¦ È£ÃâÇÒ ¶§ ÀÌ proxy´Â È£ÃâÀ» ½ÇÁúÀûÀÎ conncetionÀ¸·Î È£ÃâÇÑ´Ù. ±×·¯³ª 
¿ì¸®°¡ ½ÇÁúÀûÀÎ connectionÀÇ close ¸Þ¼Òµå¸¦ È£ÃâÇÒ ¶§ ½ÇÁ¦ÀÇ connectionÀÇ close¸¦
 È£ÃâÇÏ´Â ´ë½Å °£´ÜÇÏ°Ô connectionÀ» connection pool¿¡ ¹Ý³³ÇÑ´Ù. 
±×¸®°í proxy connectionÀÌ ºÒÇÊ¿äÇϴٴ ǥ½Ã¸¦ ÇÑ´Ù, ¸¸¾à Ç¥½ÅµÈ 
connectionÀ» ¾ÖÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ ´Ù½Ã ¾²¿©Áø´Ù¸é exception¿¡·¯¸¦ ¹ß»ý ½Ãų°ÍÀÌ´Ù.
ÀÌ wrappingÀº ¸Å¿ì À¯¿ëÇÏ°í ¶Ç ¿ª½ÃJ2EE ¾ÖÇø®ÄÉÀÌ¼Ç ¼­¹ö ¼öÇàÀ» µµ¿Í ¿©·¯°¡Áö
 ÀÌÇØÇϱ⠽¬¿î ¹æ¹ýÀ¸·Î prepared statements¿¡ ´ëÇÑ µµ¿òÀ» ÁÙ°ÍÀÌ´Ù.
¾ÖÇø®ÄÉÀ̼ÇÀÌ Connection.prepareStatementÀ» È£ÃâÇÒ ¶§ À̰ÍÀº driver¿¡ ÀÇÇØ
 a PreparedStatement ·Î µÇµ¹¾Æ °£´Ù. ¾ÖÇø®ÄÉÀ̼ÇÀº connectionÀ» °¡Áö°í ÀÖ´Â
 µ¿¾È ÇÚµéÀ» À¯ÁöÇÏ°í ¿äûÀÌ ³¡³µÀ» ¶§ Connection.prepareStatement¸¦ ´Ý°í 
connectionÀ» ´Ý´Â´Ù. ±×·¯³ª connectionÀÌ pool·Î ¹Ý³³µÈ ÈÄ ³ªÁß¿¡ °°Àº ȤÀº 
´Ù¸¥ ¾ÖÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ Àç»ç¿ëµÈ´Ù¸é ±×¶§ ¸¸¾à ±× ¾ÖÇø®ÄÉÀ̼ÇÀÌ ¶ÇÇÑ °°Àº
 ¹®ÀåÀ» ÀÌ»óÀûÀ¸·Î ÁغñÁßÀ̶ó¸é, ¿ì¸®´Â °°Àº PreparedStatement°¡ 
¾ÖÇø®ÄÉÀ̼ÇÀ¸·Î °¡±â¸¦ ¿øÇÒ °ÍÀÌ´Ù.


J2EE PreparedStatement Cache

À̰ÍÀº J2EE server connection pool manager¿¡ ÀÖ´Â cacheÀ» ÀÌ¿ëÇØ¼­ ÀÌ·ç¾îÁø´Ù. 
J2EE server´Â Ç®¿¡ ÀÖ´Â °¢°¢ÀÇ database connection¿¡ ´ëÇÑ prepared statements ÀÇ
 ¸ðµç ¸®½ºÆ®¸¦ À¯Áö ÇÑ´Ù. ¾ÖÇø®ÄÉÀ̼ÇÀÌ Çϳª¿¡ connection¿¡ ´ëÇØ¼­
 prepared statement¸¦ È£ÃâÇÏ¸é ¼­¹ö´Â ±× ¹®ÀåÀÌ ¹Ì¸® ÁغñµÇ¾î Àִ°¡¸¦ È®ÀÎÇÑ´Ù.
 ¸¸¾à ±×°ÍÀÌ ¹Ì¸® Áغñ µÇ¾ú´Ù¸é PreparedStatement °´Ã¼°¡ cache¼Ó¿¡ ÀÖÀ» °ÍÀ̰í
 À̰ÍÀº ¾ÖÇø®ÄÉÀ̼ǷΠ¹Ýȯ µÈ´Ù. ¸¸¾à ±×·¸Áö ¾Ê´Ù¸é ÀÌ È£ÃâÀº 
jdbc driver·Î ³Ñ°ÜÁö°í ´Ù½Ã Äõ¸®/ PreparedStatement °´Ã¼°¡ ±× connection cache¿¡
 Ãß°¡ µÈ´Ù. Jdbc driver°¡ ÀÌ·¸°Ô µ¿ÀÛÇϱ⠶§¹®¿¡ ¿ì¸®´Â connection¸¶´Ù 
ÇϳªÀÇ cache¸¦ ÇÊ¿ä·ÎÇÑ´Ù. ¿ì¸®°¡ ÀÌ cacheÀÇ ÀÕÁ¡À» Àß ÀÌ¿ëÇϱ⠿øÇÑ´Ù¸é 
¾Õ¿¡¼­ ¸»ÇÑ ¹Ù¿Í °°ÀÌ ÆÄ¶ó¸ÞÅÍÈ­ µÈ Äõ¸®¸¦ »ç¿ëÇØ¼­ cache¼Ó¿¡ ¹Ì¸® ÁغñµÈ °ÍÀ»
 »ç¿ëÇÑ´Ù. ´ëºÎºÐÀÇ ¾ÖÇø®ÄÉÀÌ¼Ç ¼­¹ö´Â ÀÌ prepared statement cacheÀÇ »çÀÌÁ 
Á¶Àý ÇÒ ¼ö ÀÖµµ·Ï µÇ¾îÀÖ´Ù.


Summary

±×·¡¼­ ¿ì¸®´Â prepared statements¸¦ ÆÄ¶ó¸ÞÅÍ¿Í ÇÔ²² »ç¿ëÇØ¾ßÇÑ´Ù. 
À̰ÍÀº ¹Ì¸® ¸¸µé¾îÁø Á¢±Ù °èȹÀ» Àç »ç¿ëÇϹǷμ­ µ¥ÀÌÅÍ º£À̽º¿¡ ´ëÇÑ ·Îµå¸¦ 
ÁÙ¿© ÁØ´Ù.
ÀÌ cache´Â µ¥ÀÌÅÍ º£À̽º°¡ È®ÀåµÈ °ÍÀ̾ ¿©·¯ºÐÀÇ ¸ðµç ¾ÖÇø®ÄÉÀ̼ÇÀÌ 
À¯»çÇÑ ÆÄ¶ó¸ÞÅÍÈ­µÈ sqlÀ» »ç¿ëÇϸé ÇϳªÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀÌ ´Ù¸¥ ¾ÖÇø®ÄÉÀ̼ǿ¡
 ÀÇÇØ »ç¿ëµÈ prepared statements¸¦ ÀÌ¿ëÇϹǷΠij½Ã ½ºÅ°¸¶ÀÇ È¿À²¼ºÀ» Áõ´ë 
½Ãų ¼ö ÀÖ´Ù.
À̰ÍÀº application server »ç¿ëÀÇ ÀÌÁ¡ÀÌ´Ù. ¿Ö³ÄÇÏ¸é µ¥ÀÌÅÍ º£À̽º¿¡ Á¢±ÙÇÏ´Â 
·ÎÁ÷Àº µ¥ÀÌÅÍ Á¢±Ù °èÃþ¿¡ ÁýÁßÈ­ µÇ¾î¾ßÇϱ⠶§¹®ÀÌ´Ù.
µÎ¹øÂ°·Î prepared statementsÀÇ ¿Ã¹Ù¸¥ »ç¿ëÀº ¶ÇÇÑ ¿©·¯ºÐÀÌ ¾ÖÇø® ÄÉÀÌ¼Ç ³»ºÎÀÇ 
prepared statements cache¸¦ Àß ÀÌ¿ëÇÒ ¼ö ÀÖ°Ô ÇÑ´Ù. À̰ÍÀº ¾ÖÇø®ÄÉÀ̼ÇÀÌ 
ÀÌÀü¿¡ »ç¿ëÇß´ø prepared statements È£ÃâÀ» Àç»ç¿ëÇØ¼­ JDBC driver¿¡ ´ëÇÑ È£ÃâÀÇ
 ¼ö¸¦ °¨¼Ò½ÃÄÑ ¼º´ÉÀÇ Çâ»óÀ» ½ÃŲ´Ù. À̰ÍÀº Çö¸íÇÑ fat clients »ç¿ëÀ» È¿À²ÀûÀ¸·Î 
±×¸®°í °æÀï·ÂÀÖ°Ô ¸¸µé°í
µ¶Á¡ÀûÀÎ connectionÀ» À¯ÁöÇÒ ¼ö ¾ø´Â ºÒÀÌÀÍÀ» Á¦°ÅÇÑ´Ù.
¸¸¾à ÆÄ¶ó¸ÞŸȭµÈ prepared statements¸¦ »ç¿ëÇÑ´Ù¸é ¿©·¯ºÐÀº µ¥ÀÌÅÍ º£À̽º¿Í 
Äڵ带 °¡Áö°í ÀÖ´Â application serverÀÇ È¿À²À» ³ôÀÏ ¼ö ÀÖ´Ù. ÀÌµé °³¼±µÈ Á¡Àº 
¿©·¯ºÐÀÇ ¾ÖÇø®ÄÉÀ̼ÇÀÇ ¼º´ÉÀ» Çâ»ó ½Ãų¼ö ÀÖ°Ô ÇÒ°ÍÀÌ´Ù.

-------------------------------------------------------------
´ÙÀ½¹ø¿¡´Â java performance tuning¿¡ ´ëÇÑ ±â»ç¸¦ º¸³»µå¸³´Ï´Ù.
-------------------------------------------------------------



[¿ø¹®]

Why Prepared Statements are important and how to use them "properly".

Databases have a tough job. 
They accept SQL queries from many clients concurrently and
 execute the queries as efficiently as possible against the data.
 Processing statements can be an expensive operation but databases 
are now written in such a way so that this overhead if minimized. But, 
these optimizations need assistance from the application developers 
if we are to capitalize on them. This article shows how the correct
 use of PreparedStatements can significantly help a database perform 
these optimizations. 

How does a database execute a statement?
Obviously, don't expect a lot of detail here; we'll only examine the aspects 
important to this article. When a database receives a statement then
 the database engine first parses the statement and looks for syntax errors. 
Once the statement is parsed then the database needs to figure out the most 
efficient way to execute the statement. This can be computationally quite 
expensive. The database checks what indexes if any could help, or whether 
it should do a full read of all rows in a table. Databases use statistics 
on the data to figure out what is the best way. Once the query plan is created
 then it can be execute by the database engine. 
It takes CPU power to do the access plan generation. Ideally, 
if we send the same statement to the database twice then we'd like the database
 to reuse the access plan for the first statement. This uses less CPU than if
 it regenerated the plan another time. 

Statement Caches
Databases are tuned to do this. They usually include sort kind of statement cache.
This cache uses the statement it-self as a key and the access plan is stored 
in the cache with the corresponding statement. This allows the database engine
 to reuse the plans for statements that have been executed previously. 
For example, if we sent the database a statement such as select a,b from 
t where c = 2 then the computed access plan is cached. 
If we send the same statement later then the database can reuse the 
previous access plan thus giving us a saving in CPU power. 

Note however that the whole statement is the key.
 So, for example if we later sent the statement 
select a,b from t where c = 3 then it would not find an access plan. 
This is because the c=3 is different from the cached plan "c=2". So, 
for example: 
for(int I = 0; I < 1000; ++I) { 
  PreparedStatement ps = conn.prepareStatement("select a,b from t where c = " + I);
  ResultSet rs = Ps.executeQuery(); rs.close(); ps.close(); 
} 




Here the cache won't be used. Each iteration of the loop sends a different
 SQL statement to the database. A new access plan is computed for each iteration 
and basically, we're throwing CPU cycles away using this approach. How-ever,
 look at the next snippet: 

PreparedStatement ps = conn.prepareStatement("select a,b from t where c = ?"); 
for(int I = 0; I < 1000; ++I) { 
 ps.setInt(1, I); 
 ResultSet rs = ps.executeQuery(); 
 rs.close(); 
} 
ps.close(); 

Here it will be much more efficient. The statement sent to the database 
is parameterized using the '?' marker in the sql. This means every iteration 
is sending the same statement to the database with different parameters for 
the "c=?" part. This allows the database to reuse the access plans for the 
statement and makes the program execute more efficiently inside the database. 
This basically lets your application run faster or makes more CPU available to 
users of the database. 

PreparedStatement ps = conn.prepareStatement("select a,b from t where c = ?");
for(int I = 0; I < 1000; ++I) {
ps.setInt(1, I); 
ResultSet rs = ps.executeQuery();
rs.close();
ps.close(); 
}
 
PreparedStatements and J2EE servers
Things could be complicated when we use a J2EE server. Normally,
 a prepared statement is associated with a single database connection.
 When the connection is closed then the preparedstatement is discarded.
 Normally a fat client application would get a database connection and 
then hold it for its lifetime. It would also create all prepared statements 
either eagerly or lazily. Eagerly means that they are all created at once 
when the application starts. Lazily means that they are created as they are used.
 An eager approach gives a delay when the application starts but once 
it starts then it performs optimally. A lazy approach gives a fast start 
(as no preparation is done) but as the application runs the prepared statements 
are created when they are first used by the application. This gives an uneven
 performance until all statements are prepared but eventually the application 
settles and runs as fast as the eager application. Which is best depends on
 whether you need a fast start or you need even performance. 

The problem with a J2EE application is that it doesn't work like this.
 It only keeps a connection for the duration of the request. This means that 
it must create the prepared statemes every time the request is executed.
 This is not as efficient as the fat client approach where the prepared 
statements are created once rather than on every request. J2EE vendors have 
noticed this and designed the connection pooling to avoid this performance 
disadvantage. 
When the J2EE server gives your application a connection, it isn't giving
 you the actual connection; you're getting a wrapper. 


You can verify this by looking at the name of the class for the connection
 you are given. It won't be a database JDBC connection, it'll be a class 
created by your application server. Every JDBC object will be proxied by 
the application servers connection pool manager. All JDBC ResultSets, 
statements, CallableStatements, preparedStatements etc will be wrapped 
and returned to the application as a proxy object. When you close the 
connection these objects are mark invalid and can be garbage collected. 


Normally if you called close on a connection then the jdbc driver closes
 the connection. We want the connection to be returned to the pool when 
close is called by a J2EE application. We do this by making a proxy jdbc 
connection class that looks like a real connection. It has a reference to 
the actual connection. When we invoke any method on the connection then the 
proxy forwards the call to the real connection. But, when we call methods 
such as close then instead of calling close on the real connection, it simply
 returns the connection to the connection pool and then marks the proxy 
connection as invalid so that if it is used again by the application then 
we'll get an exception. 

This wrapping is very useful as it also helps J2EE application server
 implementers add support for prepared statements in a sensible way. 
When an application calls Connection.prepareStatement then it is returned 
a PreparedStatement object by the driver. The application then keeps the
 handle while it has the connection and those closes it before it closes 
the connection when the request finishes. But, after the connection is 
returned to the pool and later reused by the same or another application 
then if that application also prepares the same statement then ideally, 
we want the same PreparedStatement to be returned to the application. 

J2EE PreparedStatement Cache
This is implemented using a cache inside the J2EE server connection pool manager.
 The J2EE server keeps a list of prepared statements for each database connection 
in the pool. When an application calls prepareStatement on a connection then the
 app server checks if that statement was previously prepared. If it was then the 
PreparedStatement object will be in the cache and this is returned to the 
application. If not then the call is passed to the jdbc driver and then 
query/preparedstatement object is added in that connections cache. 
We need a cache per connection because that's the way jdbc drivers work. 
Any preparedstatements returned are specific to that connection. 
If we want to take advantage of this cache then the same rules apply 
as before. We need to use parameterized queries so that
 they will match ones already prepared in the cache. 
Most application servers will allow you to tune the size of this prepared 
statement cache. 

Summary
So, we absolutely must use parameterized queries with prepared statements. 
This reduces the load on the database by allowing it to reuse access plans
 that were already prepared. This cache is database wide so if you can arrange for
 all your applications to use similar parameterized SQL then you improve
 the efficiency of this caching scheme as an application can take advantage 
of prepared statements used by another application. This is an advantage of 
an application server because logic that accesses the database should be 
centralized in a data access layer (either an OR-mapper, entity beans or 
straight JDBC). This makes it easier to assure this. 
Secondly, the correct use of prepared statements also lets you take advantage 
of the prepared statement cache in the application server. 
This improves the performance of your application as the application can reduce 
the number of calls to the JDBC driver when it can reuse a previous prepared 
statement call. This makes it competitive with fat clients efficiency wise and
 removes the disadvantage of not being able to keep a dedicated connection. 
If you use parameterized prepared statements then you improve the efficiency 
of the database and of your application server hosted code.
 Both of these improvements will allow your application to improve its performance. 

Á¦¸ñ : Re: Prepared Statements¸¦ ¾²Áö ¾Ê¾Æ¾ß ÇÒ¶§.
±Û¾´ÀÌ: ¹è°æ¿­(kybae) 2002/04/13 09:13:48 Á¶È¸¼ö:2568 ÁÙ¼ö:31
PreparedStatement°¡ SQL¹®ÀÇ ¼öÇà°èȹÀ» Àç»ç¿ëÇÒ ¼ö ÀÖ´Â ÀåÁ¡ÀÌ ÀÖÁö¸¸
±×°ÍÀº ¿ªÀ¸·Î ¸»Çϸé Ä¡¸íÀûÀÎ ¾àÁ¡ÀÌ µÉ ¼ö ÀÖ½À´Ï´Ù.

¿¹¸¦µé¾î)
selectÇÏ´Â Å×À̺íÀÇ Æ¯Á¤Ä®·³¿¡ 'A'¶ó´Â µ¥ÀÌÅÍ¿Í 'B'¶ó´Â µ¥ÀÌÅͰ¡ Á¸ÀçÇϰí
±× ºÐÆ÷µµ°¡ A=99%, B=1%¸¸ Á¸ÀçÇÑ´Ù¸é SQL¹®ÀÇ °Ë»öÁ¶°ÇÀÌ 'B'¸¦ °Ë»öÇÒ°æ¿ì
Å×À̺íÀÇ Index¸¦ Ÿ´Â°ÍÀÌ ¹Ù¶÷Á÷ÇÏÁö¸¸ 'A'¸¦ °Ë»öÇÒ°æ¿ì Àüü Å×À̺íÀ» ½ºÄËÇØ¾ß
ÇÒ°ÍÀÔ´Ï´Ù.

ÇÏÁö¸¸ PreparedStatement·Î ¾î´À Á¶°ÇÀ» °Ë»öÇϵçÁö°£¿¡ Çѹø ¼öÇàµÈ ÀÌÈķδÂ
Query ½ÇÇà °èȹÀ» Àç»ç¿ë ÇÔÀ¸·Î¼­ ¾ÕÀ¸·Î ¼öÇàµÇ´Â QueryÀÇ ½ÄÀº ¹«Á¶°Ç Index¸¦ 
Ÿ°Å³ª ¶Ç´Â ¹«Á¶°Ç FullScanÀ» ÇÏ´Â ÇüÅ·Π½ÇÇà°èȹÀ» ¼ö¸³ÇÒ °ÍÀÔ´Ï´Ù.

ÀÌ´Â Åë°èµ¥ÀÌÅ͸¦ ÀûÀýÈ÷ Ȱ¿ëÇÏÁö ¸øÇϰԵǴ ġ¸íÀûÀÎ ¿À·ù°¡ ¹ß»ýÇÒ ¼ö ÀÖÀ½À»
À̾߱â ÇÒ ¼ö ÀÖ½À´Ï´Ù.

µû¶ó¼­ Á¦ ªÀº ¼Ò°ßÀ¸·Î´Â ½ÇÇà°èȹÀÌ Æ¯Á¤ °ª¿¡ ÀÇÇØ Å©°Ô ¹Ù²î´Â °ÍÀÌ ¾Æ´Ñ°æ¿ì¿£
PreparedStatementÀÇ SQL Execution planÀç»ç¿ëÀÌ È¿À²ÀûÀ̶ó°í º¼ ¼ö ÀÖÀ¸¸ç ¹Ý¸é¿¡
ƯÁ¤ °ª¿¡ µû¶ó ½ÇÇà°èȹÀ» Àç»ý¼º ÇØ¾ß ÇÒ °æ¿ì¿£ Statment¸¦ »ç¿ëÇϴ°ÍÀÌ ¹Ù¶÷Á÷
ÇÒ ¼ö ÀÖ½À´Ï´Ù. ¿Ö³Ä¸é ½ÇÇà°èȹÀ» Àç»ý¼ºÇϴ°ÍÀº ¾ÆÁÖ ÂªÀº ½Ã°£¿¡ ¼öÇà µÉ ¼ö ÀÖ
Áö¸¸ Çѹø À߸ø »ý¼ºµÈ ½ÇÇà°èȹ¿¡ ÀÇÇØ ¼öÇàµÇ´Â SQL¹®Àº ¾öû³­ ½Ã°£Àû ºñ¿ëÀ» ³¶ºñ
ÇÒ¼ö ÀÖ½À´Ï´Ù.

ÀϹÝÀûÀ¸·Î OLTP¼º ¾÷¹«¿£ PreparedStatement»ç¿ëÇÏ¿© PlanÀç»ç¿ëÀ» À¯µµÇÏ¸ç ¹Ý¸é
OLAP¿Í °°ÀÌ ÀæÀº SQL¹® ¼öÇàÀº ¹ß»ýÇÏÁö ¾ÊÁö¸¸ ¸¹Àº ·®ÀÇ µ¥ÀÌÅ͸¦ ó¸®ÇØ¾ß ÇÒ¶§´Â 
Statement¸¦ »ç¿ëÇϴ°ÍÀÌ ¹Ù¶÷Á÷ÇÏ´Ù°í ÇÒ ¼ö ÀÖ½À´Ï´Ù.

µé¸®´Â À̾߱â·Î´Â ¿äÁò DBMS´Â ³Ê¹«³ª ±× Áö´ÉÀÌ ÁÁ¾Æ¼­ PreparedStatement¸¦ ¾µÁö¶óµµ 
³Ê¹« ¸¹Àº Resource¸¦ Áö¼ÓÀûÀ¸·Î »ç¿ëÇÒ °æ¿ì ½ÇÇà°èȹÀ» Àç¼ö¸³ ÇÑ´Ù°íµµ Çϴ±º¿ä.

°á·ÐÀûÀ¸·Î Prepared Statement°¡ ¸¸´É ÇØ°á»ç´Â ¾Æ´Ï¶ó´Â °ÍÀ» ¸»¾¸µå¸®°í ½Í½À´Ï´Ù.
Á¦¸ñ : Re: ÀúÀÇ PreparedStatement¿¡ ´ëÇÑ »ý°¢À» ¾²°Ú½À´Ï´Ù.
±Û¾´ÀÌ: ¼­¹Î±¸(4baf) 2002/04/13 11:39:06 Á¶È¸¼ö:1405 ÁÙ¼ö:42
¿¹¸¦µé¾î)
selectÇÏ´Â Å×À̺íÀÇ Æ¯Á¤Ä®·³¿¡ 'A'¶ó´Â µ¥ÀÌÅÍ¿Í 'B'¶ó´Â µ¥ÀÌÅͰ¡ Á¸ÀçÇϰí
±× ºÐÆ÷µµ°¡ A=99%, B=1%¸¸ Á¸ÀçÇÑ´Ù¸é SQL¹®ÀÇ °Ë»öÁ¶°ÇÀÌ 'B'¸¦ °Ë»öÇÒ°æ¿ì
Å×À̺íÀÇ Index¸¦ Ÿ´Â°ÍÀÌ ¹Ù¶÷Á÷ÇÏÁö¸¸ 'A'¸¦ °Ë»öÇÒ°æ¿ì Àüü Å×À̺íÀ» ½ºÄËÇØ¾ß
ÇÒ°ÍÀÔ´Ï´Ù.

ÇÏÁö¸¸ PreparedStatement·Î ¾î´À Á¶°ÇÀ» °Ë»öÇϵçÁö°£¿¡ Çѹø ¼öÇàµÈ ÀÌÈķδÂ
Query ½ÇÇà °èȹÀ» Àç»ç¿ë ÇÔÀ¸·Î¼­ ¾ÕÀ¸·Î ¼öÇàµÇ´Â QueryÀÇ ½ÄÀº ¹«Á¶°Ç Index¸¦ 
Ÿ°Å³ª ¶Ç´Â ¹«Á¶°Ç FullScanÀ» ÇÏ´Â ÇüÅ·Π½ÇÇà°èȹÀ» ¼ö¸³ÇÒ °ÍÀÔ´Ï´Ù.

->

SELECT * FROM EXP_TABLE
WHERE :id = 'A'
AND id||'' = 'A'
UNION ALL
SELECT * FROM EXP_TABLE
WHERE :id = 'B'
AND  = 'B'

RBOÀÇ °æ¿ì ¸¸¾à ¸»¾¸ÇϽŴë·Î Á¤È®ÇÑ ºÐÆ÷µµ¸¦ »çÀü ¿¹Ãø°¡´ÉÇÏ´Ù¸é 

À§¿¡Ã³·³ SQL¸¸ Àß ¾²¸é µË´Ï´Ù.

±×¸®°í, CBO¸¦ ¾²½Ç°Å¶ó¸é Ç÷£ÀÌ ¹Ù²î¸é Ç÷£ÀÌ invalidateµÇ¹Ç·Î »ó°ü¾ø°í¿ä.


µé¸®´Â À̾߱â·Î´Â ¿äÁò DBMS´Â ³Ê¹«³ª ±× Áö´ÉÀÌ ÁÁ¾Æ¼­ PreparedStatement¸¦ ¾µÁö¶óµµ 
³Ê¹« ¸¹Àº Resource¸¦ Áö¼ÓÀûÀ¸·Î »ç¿ëÇÒ °æ¿ì ½ÇÇà°èȹÀ» Àç¼ö¸³ ÇÑ´Ù°íµµ Çϴ±º¿ä.

-> ¿À¶óŬÀÇ °æ¿ì¿¡´Â ¾Æ¸¶ ÀÌ·±°Ô ¾øÀ»°Ì´Ï´Ù.

¸¸¾à ÀÚµ¿È­µÈ °ÍÀ» ¿øÇϽŴٸé Å©·ÐÀâÀ̳ª DBMS_JOBÀ¸·Î Á¾Á¾ analyze¸¸ µ¹·ÁÁÖ¸é
µÉ°Ì´Ï´Ù.


ÁñÇÁÇϼ¼¿ä

-----------------------------------------------
OCP, SCJP
Software Engineer
Seo, Min-Koo(4baf@dreamwiz.com)
Á¦¸ñ : Re: ÁÁÀº ¹æ¹ýÀ̳׿ä.
±Û¾´ÀÌ: ¹è°æ¿­(kybae) 2002/04/16 08:01:47 Á¶È¸¼ö:989 ÁÙ¼ö:4
¾î¶² °ªÀÌ µé¾îÀÖ°í ºÐÆ÷µµ¸¸ Á¤È®È÷ ¾È´Ù¸é UNION ALL·Î °í·ÁÇØ¼­
Prepared Statement¸¦ ¾²¸é µÇ°Ú³×¿ä.. ±Ùµ¥ °ªÀÌ ¿©·¯°¡Áö°Å³ª ·ÎÁ÷ÀÌ º¹ÀâÇØÁö¸é
»ç¿ëÇϱâ´Â Á» ¾î·Æ°Ú³×¿ä.. --;;;
½ÇÇà°èȹ¿¡ ´ëÇÑ invalidation checking±â´ÉÀÌ ¿À¶óŬ 9i¿¡¼­µµ ¾ø³ª¿ä?
Á¦¸ñ : Re: ±Û½ê¿ä.. ^^ invalidation checking?
±Û¾´ÀÌ: ¼­¹Î±¸(4baf) 2002/04/18 11:18:03 Á¶È¸¼ö:962 ÁÙ¼ö:18
±æ°í º¹ÀâÇÑ SQL¹®ÀåÀÌ »ç¿ëÇÏ±â ¾î·Æ½À´Ï´Ù.
Äü¼ÒÆ®´Â ¹öºí¼ÒÆ®º¸´Ù ¾î·Æ½À´Ï´Ù.

¹«½¼¸»À» Çϰí½ÍÀºÁö ¾Æ½Ã¸®¶ó »ý°¢ÇÕ´Ï´Ù. ^^;

Á¦°¡ ¹¹ õÀç¶ó°í Àú·± SQL¹®À» ½á´ë´Â °ÍÀº ¾Æ´Ï°í..
À߾ƽô ´ë¿ë·® µ¥ÀÌÅͺ£À̽º ¼Ö·ç¼ÇÁßÀÇ ÀϺγ»¿ëÀÔ´Ï´Ù.
¾Èº¸¼ÌÀ¸¸é Çѹø º¸½Ã±æ ¹Ù¶ó°í¿ä.

¿À¶óŬ 9iÀÇ »õ±â´É¿¡ ´ëÇØ¼­´Â º»°ÝÀûÀ¸·Î ÇнÀÇØº»ÀûÀÌ ¾øÁö¸¸,
Á¦°¡ ¾Ë±â·Î 'Àß ½ÇÇàµÇ´Â SQL¿¡¼­ ¾î´À³¯ °©Àڱ⠺ñ¿ëÀÌ ³Ê¹« ¸¹ÀÌ µé°ÔµÇ´Â¼ø°£
plan ÀÌ invalidateµÈ´Ù' ´Â ±â´ÉÀº ¾ø½À´Ï´Ù.


-----------------------------------------------
OCP, SCJP
Software Engineer
Seo, Min-Koo(4baf@dreamwiz.com)
Á¦¸ñ : Re: [Áú¹®] prepared StatementÀÇ Ä³½¬ Àå¼Ò?
±Û¾´ÀÌ: Á¶Çö±æ(guest) 2002/04/20 03:24:18 Á¶È¸¼ö:1075 ÁÙ¼ö:11
À§¿¡ ºÐ ¹ø¿ª °¨»çµå¸³´Ï´Ù. ^^ prepared Stratement ¿¡ °üÇØ¼­ ±Ã±ÝÇÑ Á¡ÀÌ ÀÖ½À´Ï´Ù.
prepared Statment °¡ sql ó¸® °úÁ¤Áß syntex check, ¿Í symentics check, ½ÇÇà°èȹÀ»
Àç»ç¿ëÇÏ´Â °Å¶ó´Â °ÍÀº ÀÌÇØ°¡ °©´Ï´Ù¸¸, ÀÌ·¯ÇÑ »çÇ׿¡ ´ëÇÑ Ä³½¬°¡ DBMS°¡ ¾Æ´Ñ 
Application Server ¿¡ Á¸ÀçÇÑ´Ù´Â °ÍÀÌ ÀÌÇØ°¡ ¾È °©´Ï´Ù. JDBC ³ª  Application Server
½ºÆå»ó¿¡ ±×·¸°Ô Á¤ÀÇµÈ °ÍÀΰ¡¿ä? ¸¸¾à ij½¬°¡ Application  Server ¿¡ Á¸ÀçÇÑ´Ù¸é 
ÇØ´ç Äõ¸®°¡ ³¯¾Æ¿ÔÀ»¶§  JDBC µå¶óÀ̹ö´Â Äõ¸®½ºÆ®¸µÀÌ ¾Æ´Ñ ij½¬µÈ ³»¿ëÀ» DBMS ·Î
Àü¼ÛÇÏ°Ô µÇ´Â °ÍÀÎÁö¿ä? ^^ ±Ã±ÝÇϳ׿ä.

------------------------------------------------
Simple Coder 
Cho, Hyungil (gedwarp@netsgo.com)
Á¦¸ñ : Re: WebLogic¿¡¼­ÀÇ PreparedStatement Cache
±Û¾´ÀÌ: ¼­Á¤Èñ(to2space) 2002/06/05 23:20:23 Á¶È¸¼ö:6216 ÁÙ¼ö:148
************************************************
¼­Á¤Èñ 
ÇÑ¼Ö ÅÚ·¹ÄÄ 

¹ø¿ª ȤÀº ±â¼ú»óÀÇ ¿À·ù¿¡ ´ëÇØ¼­ feedbackÀ» ÁÖ¼¼¿ä.
to2space@kornet.net

JDBC¿¡ °ü·ÃµÈ »çÇ×À» ´õ º¸½Ç·Á¸é..
http://home.megapass.co.kr/~to2space/

¹ø¿ªÇÏ´Â ÀÌÀÇ ¼ö°íµµ »ý°¢ÇÏ´Â Àǹ̿¡¼­ ²À Ãâó´Â ¸í±âÇØ Áֽʽÿä.
************************************************

±Ã±ÝÇϽŠºÎºÐ¿¡ ´ëÇÑ ±ÛÀÔ´Ï´Ù. ¿©±â¼­ÀÇ ¿¹´Â weblogic¿¡ ´ëÇÑ »çÇ×ÀÔ´Ï´Ù. ¹ø¿ªÀº ÇÏÁö
¾Ê¾Ò°í ±Ûµµ ±Ã±ÝÇϽŠºÎºÐ¿¡ ´ëÇØ¼­ 100%ÀÇ ¼³¸íÀº µÇÁö´Â ¾ÊÁö¸¸ ±×·¡µµ ÀÐ¾î º¸½Ã¸é
¾î´À Á¤µµ ÀÌÇØ°¡ °¡½Ç°Ì´Ï´Ù.

For each connection pool that you create in WebLogic Server,
you can specify a prepared statement cache size.
When you set the prepared statement cache size, WebLogic Server stores 
each prepared statement used in applications and EJBs
 until it reaches the number of prepared statements that you specify. 
For example, if you set the prepared statement cache size to 10, WebLogic Server
 will store the first 10 prepared statements called by applications or EJBs. 

When an application or EJB calls any of the prepared statements stored 
in the cache, WebLogic Server reuses the statement stored in the cache. 
Reusing prepared statements eliminates the need for parsing statements 
in the database, which reduces CPU usage on the database machine, 
improving performance for the current statement and leaving CPU cycles 
for other tasks.

The default value for prepared statement cache size is 0. 
You can use the following methods to set the prepared statement cache size
 for a connection pool:


Using the Administration Console. See Creating and Configuring a JDBC Connection 
Pool in the Administration Console Online Help. 

Using the WebLogic management API. See the getPreparedStatementCacheSize() 
and setPreparedStatementCacheSize(int cacheSize) methods in the Javadocs
 for WebLogic Classes. 

Directly in the configuration file (typically config.xml). 
To set the prepared statement cache size for a connection pool 
using the configuration file, before starting the server, 
open the config.xml file in an editor, then add an entry 
for the PreparedStatementCacheSize attribute in the JDBCConnectionPool tag.
 For example:

    <JDBCConnectionPool CapacityIncrement="5"       
 DriverName="com.pointbase.jdbc.jdbcUniversalDriver"       
 InitialCapacity="5" MaxCapacity="20" Name="demoPool"      
  Password="{3DES}ANfMduXgaaGMeS8+CR1xoA=="      
  PreparedStatementCacheSize="20" Properties="user=examples"    
    RefreshMinutes="0" ShrinkPeriodMinutes="15"       
 ShrinkingEnabled="true" Targets="examplesServer"      
  TestConnectionsOnRelease="false"      
  TestConnectionsOnReserve="false"     
   URL="jdbc:pointbase:server://localhost/demo"/>
Usage Restrictions for the Prepared Statement Cache

Using the prepared statement cache can dramatically increase performance,
 but you must consider its limitations before you decide to use it.
 Please note the following restrictions when using the prepared statement cache. 

There may be other issues related to caching prepared statements
 that are not listed here. If you see errors in your system related 
to prepared statements, you should set the prepared statement cache size to 0, 
which turns off prepared statement caching, to test
 if the problem is caused by caching prepared statements.

Calling a Stored Prepared Statement After a Database Change May Cause Errors

Prepared statements stored in the cache refer to specific database objects 
at the time the prepared statement is cached. If you perform any DDL 
(data definition language) operations on database objects referenced 
in prepared statements stored in the cache, the statements will fail 
the next time you run them. For example, if you cache a statement
 such as select * from emp and then drop and recreate the emp table, 
the next time you run the cached statement, the statement will fail
 because the exact emp table that existed when the statement was prepared,
 no longer exists. 

Likewise, prepared statements are bound to the data type for each column
 in a table in the database at the time the prepared statement is cached. 
If you add, delete, or rearrange columns in a table, prepared statements
 stored in the cache are likely to fail when run again.

Using setNull In a Prepared Statement

When using the WebLogic jDriver for Oracle to connect to the database, 
if you cache a prepared statement that uses a setNull bind variable,
 you must set the variable to the proper data type. 
If you use a generic data type, as in the following example,
 the statement will fail when it runs with a value other than null.

java.sql.Types.Long sal=null
...
if (sal == null)    setNull(2,int)//This is incorrectelse    setLong(2,sal) 
Instead, use the following:

if (sal == null)    setNull(2,long)//This is correctelse    setLong(2,sal) 
This issue occurs consistently when using the WebLogic jDriver for Oracle.
 It may occur when using other JDBC drivers.

Prepared Statements in the Cache May Reserve Database Cursors

When WebLogic Server caches a prepared statement, the prepared statement
 may open a cursor in the database. If you cache too many statements,
 you may exceed the limit of open cursors for a connection. 
To avoid exceeding the limit of open cursors for a connection, 
you can change the limit in your database management system or 
you can reduce the prepared statement cache size for the connection pool.

Determining the Proper Prepared Statement Cache Size

To determine the optimum setting for the prepared statement cache size,
 you can emulate your server workload in your development environment and
 then run the Oracle statspack script. In the output from the script,
 look at the number of parses per second. As you increase 
the prepared statement cache size, the number of parses 
per second should decrease. Incrementally increase the prepared statement cache 
size until the number or parses per second no longer decreases.

Note: Consider the usage restrictions for the prepared statement cache 
before you decide to use it in your production environment. 
See Usage Restrictions for the Prepared Statement Cache for more information.

Using a Startup Class to Load the Prepared Statement Cache

To make the best use of the prepared statement cache and 
to get the best performance, you may want to create a startup class 
that calls each of the prepared statements that you want to store
 in the prepared statement cache. WebLogic Server caches prepared statements 
in the order that they are used and stops caching statements 
when it reaches the prepared statement cache size limit. 
By creating a startup class that calls the prepared statements 
that you want to cache, you can fill the cache with statements 
that your applications will reuse, rather than with statements t
hat are called only a few times, thus getting the best performance
 increase with the least number of cached statements. 
You can also avoid caching prepared statements that my be problematic, 
such as those described in Usage Restrictions for the Prepared Statement Cache.

Even if the startup class fails, WebLogic Server loads and caches the statements
for future use.
  [±Û¸ñ·Ï /½Ã°£¼ø] [´ä±Û¾²±â] [ÇÁ¸°Æ®]