๐Ÿ’ป Programming (358)

์ž๋ฐ”์—๋Š” Dateํด๋ž˜์Šค๊ฐ€ ๋‘ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

java.util.Date์™€ java.sql.Date. ์ด ๋‘ ๊ฐ€์ง€ ์ฐจ์ด์ ๋„ ์•Œ์•„๋ณด๊ณ  ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„๋„ ๊ตฌํ•ด๋ณด์ž.

์•„๋ž˜ ์†Œ์Šค๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์†Œ์Šค์ด๋‹ค.

 

public class SimpleDateTest {
    public static void main(String[] args) {
         

        // 1. util.Date๋กœ ์–ป์–ด์˜จ ํ˜„์žฌ์‹œ๊ฐ„์„ SimpleDateFormat์„ ์ด์šฉํ•ด์„œ ์—ฐ๋„, ์›”, ์ผ, ์‹œ๊ฐ„, ๋ถ„, ์ดˆ ๊นŒ์ง€ ํ‘œํ˜„ํ•˜๋„๋ก ํ•ด๋ณด์•˜๋‹ค. ๋ถ‰์€ ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„์€ ์ž์‹ ์ด ์›ํ•˜๋Š” ํฌ๋งท์œผ๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด "yy/MM/dd"์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ง์ด๋‹ค.
        String currentDate = new SimpleDateFormat("yyMMddhhmmss").format(new java.util.Date());
        System.out.println(currentDate);

 

        // 2. sql.Date๋กœ ์–ธ์–ด์˜จ ํ˜„์žฌ์‹œ๊ฐ„์„ 1๋ฒˆ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅํ•ด๋ณธ๋‹ค. sql.Date๋Š” ์ƒ์„ฑ์‹œ longํƒ€์ž…์˜ ์‹œ๊ฐ„๊ฐ’์„ ์š”๊ตฌํ•œ๋‹ค. System.currentTimeMillis()๋Š” ํ˜„์žฌ์‹œ๊ฐ„์„ longํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฏ€๋กœ ์ด๋ฅผ ์ด์šฉํ–ˆ๋‹ค.  

        currentDate = new SimpleDateFormat("yyMMddhhmmss").format(

                                                    new java.sql.Date(System.currentTimeMillis()));
        System.out.println(currentDate); 


        // 2. util.Date๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ป์–ด์˜จ  Date
        Date date = new java.util.Date();
        System.out.println(date);

 

        // 3. sql.Date๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ป์–ด์˜จ Date
        Date sqlDate = new java.sql.Date(System.currentTimeMillis());
        System.out.println(sqlDate);
    }


๋ฃจํ”„๋ฅผ ํ•œ๋ฒˆ ๋งŒ ์‚ฌ์šฉํ•œ ์ž๋ฐ” ๊ตฌ๊ตฌ๋‹จ ์†Œ์Šค


์ž๋ฐ”๋กœ ๊ตฌ๊ตฌ๋‹จ ์†Œ์Šค๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

continue, break๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๋ฃจํ”„๋„ ์ค‘๋ณต ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ•˜๋‚˜์˜ loop๋ฌธ์„ ์‚ฌ์šฉํ•œ ๊ตฌ๊ตฌ๋‹จ์ž…๋‹ˆ๋‹ค. 


Below java source code prints out multiplication table. 

It does not use nested-loop. It only uses one for-loop. 



public class gugudan {

public static void main(String args[]){

     System.out.println("๊ตฌ๊ตฌ๋‹จ์ž…๋‹ˆ๋‹ค.");
     int i = 1,b = 1;

     while ( i < 10 ) {
          if( b == 10 ) {

              i+=1; 

              b=1;

          }
          if ( i != 10 ) { 

              System.out.println(i+"*"+b+"="+i*b);
              b++;
          }
     }
}

}


์›๋ณธ ๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


๊ฐ„๋žตํ•˜๊ฒŒ ํ•œ๊ธ€๋กœ ๋ฌธ์ œ๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


int[]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ 3 ๋ฐ”๋กœ ๋’ค์— 4๋ฅผ ์œ„์น˜ํ•˜๋„๋ก ์ •๋ ฌํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ธ์ž๋กœ ๋“ค์–ด์˜จ int[]์—์„œ ์ˆซ์ž 3๊ณผ ์ˆซ์ž4์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ๋™์ผํ•˜๋ฉฐ, ์ˆซ์ž 3 ๋’ค์—๋Š” ํ•ญ์ƒ 3๊ณผ 4๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆ˜๊ฐ€ ๋“ค์–ด์˜ต๋‹ˆ๋‹ค. ์ˆซ์ž3์€ ์ •๋ ฌํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋‹ค๋ฅธ ๋ชจ๋“  ์ˆ˜ ๋“ค์€ ์œ„์น˜๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฒซ ์ˆซ์ž 3์€ ํ•ญ์ƒ ์ˆซ์ž 4๋ณด๋‹ค ๋จผ์ € ์ถœํ˜„ํ•ฉ๋‹ˆ๋‹ค.


๋ณดํ†ต int[]๋ณด๋‹ค๋Š” Listํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” api๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

๊ทธ๋ž˜์„œ List๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„  int[] ๋ฅผ ๋ฃจํ”„๋ฅผ ๋Œ๋ ค์„œ (3 ์ด ์ถœํ˜„ํ•˜๋Š” ์œ„์น˜ + 1)๊ณผ (4์˜ ์œ„์น˜)๊ฐ€ ์–ด๋””์ธ์ง€๋ฅผ ๋‘๊ฐœ์˜ ArrayList์— ๋‚˜๋ˆ ์„œ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.

์–ด์ฐจํ”ผ 3๊ณผ 4์˜ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘๊ฐœ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” ํ•ญ์ƒ ๋™์ผํ•˜๊ฒ ์ฃ .

๊ทธ๋ ‡๊ฒŒ ๋ฃจํ”„๋ฅผ ํ•œ๋ฒˆ ๋Œ๋ฉด์„œ ์œ„์น˜์ •๋ณด๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค๊ฐ€, ์œ„์น˜์ •๋ณด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฃจํ”„๋Œ๋ฉด์„œ ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋“ค์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์ฃ . ( 3์˜ ์œ„์น˜ +1 )์„ ์ €์žฅํ•˜๋Š” ์ด์œ ๋Š” 3 ๋ฐ”๋กœ ๋‹ค์Œ์˜ ์ˆซ์ž์™€ 4์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


์ €๋Š” ์—ฌ๊ธฐ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ๋ฆฌ์ŠคํŠธํ•˜๋‚˜์— ๋งต๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งต์—๋Š” ( 3์˜ ์œ„์น˜+1)๊ณผ 4์˜ ์œ„์น˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒ ์ฃ .


public int[] fix34(int[] nums) {
  List<Integer> posOfFour = new ArrayList<Integer>();
  List<Integer> posOfThree = new ArrayList<Integer>();
  for ( int i = 0; i < nums.length; i++ ){
      if ( nums[i] == 4 ){
        posOfFour.add(i);
      }
      if ( nums[i] == 3 ){
         posOfThree.add(i+1);
      }
  }
  for ( int j = 0; j < posOfThree.size(); j++ ){
     int temp = nums[(int)posOfThree.get(j)];
     nums[(int)posOfThree.get(j)] = nums[(int)posOfFour.get(j)];
     nums[(int)posOfFour.get(j)] = temp;
  }
  return nums;
}

๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด int[]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ span์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.


์šฐ์„  span์ด๋ผ๋Š” ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”.

๋ฌธ์ œ์—์„œ๋Š” span์ด๋ผ๋Š” ๊ฒƒ์„ "๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ํŠน์ • ๊ฐ’ a์™€ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๋™์ผํ•œ ๊ฐ’ a ์‚ฌ์ด์˜ ๊ธธ์ด" ๋ผ๊ณ  ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๊ธธ์ด์—๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ๊ฐ’๊ณผ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ํฌํ•จํ•œ ๊ธธ์ด์ž…๋‹ˆ๋‹ค.


์ฆ‰, int[ 1, 2, 1 ] ์„ ์ธ์ž๋กœ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ 1์— ๋Œ€ํ•œ span์€ 3์ด ๋ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์™ผ์ชฝ์˜ 1๊ณผ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์˜ 1์„ ํฌํ•จํ•œ ๊ธธ์ด๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์ฃ .

๋งŒ์•ฝ int[ 1, 2, 1, 3, 4, 2 ]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•˜๋‹ค๋ฉด,


1์— ๋Œ€ํ•œ span = 3

2์— ๋Œ€ํ•œ span = 5

3์— ๋Œ€ํ•œ span = 1

4์— ๋Œ€ํ•œ span = 1


์ด ๋ฉ๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ maxSpan์€ 2์— ๋Œ€ํ•œ span๊ฐ’์ธ 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ํ’€์–ด๋ณธ ํ•ด๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ญ ๊ทธ๋ƒฅ ๋ฃจํ”„๋ฅผ ์ค‘๋ณต์œผ๋กœ ๋Œ๋ ค์„œ ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ ์ด๊ฑด ์ •๋ง ์ตœ์•…์˜ ํ•ด๋‹ต์ด ๋˜๊ฒ ์ฃ .

์™ ๋งŒํ•˜๋ฉด ์ค‘๋ณต ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ์†๋„์ €ํ•˜๊ฐ€ ์ƒ๋‹นํ•˜๋‹ˆ๊นŒ์š”.

๋ฌธ์ œ์—์„œ ๋งํ•˜๊ธธ ์†๋„๋Š” ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ƒฅ ๋ฐ”๋กœ ์ƒ๊ฐ๋‚˜๋Š” ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 O(n์ œ๊ณฑ)์ด ๋˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฏธ ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

์œ„์—์„œ ๋“  ์˜ˆ์ œ๋ฅผ ์ธ์šฉํ•ด์„œ ์„ค๋ช…๋“œ๋ฆฌ์ž๋ฉด,


int [ 1, 2, 1, 3, 4, 2 ]๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ์— 1๊ณผ 2๋Š” ๊ฐ๊ฐ ๋‘๋ฒˆ์”ฉ ๋“ค์–ด๊ฐ€์žˆ๋Š”๋ฐ์š”

์ด๋•Œ ๋‘๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” ์ˆซ์ž๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ์‚ฌ์‹ค ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์ฃ . ์ด๋ฏธ ์ฒซ๋ฒˆ์งธ๋กœ ๋‚˜์˜จ 1๊ณผ 2๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ span๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋™์ผํ•œ ์ˆซ์ž๋“ค์ด ๋‘๊ฐœ ์ด์ƒ ๋‚˜์˜ค๊ฒŒ๋œ๋‹ค๋ฉด ์ด๋ฏธ ๊ฒ€์‚ฌํ–ˆ๋˜ ์ˆซ์ž๋“ค์— ๋Œ€ํ•œ span์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ณ„์‚ฐ์„ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ ํ•œ๋ฒˆ ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์™€ ๋™์ผํ•œ ์ˆซ์ž๋ฅผ outer most for loop์—์„œ ๋งŒ๋‚˜๊ฒŒ๋˜๋ฉด ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž์ธ์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” first inner loop๋ฅผ ๋Œ๊ณ  ์ฒดํฌํ–ˆ๋˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ second inner loop์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๋˜ํ•œ, second inner๋ฃจํ”„์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ์—๋„ int[]์˜ ํ˜„์žฌ ์ˆซ์ž์˜ ์œ„์น˜์—์„œ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ๊ฐ€์ง€๊ณ  ๋น„๊ต๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ขŒ์ธก์— ์žˆ๋Š” ์ˆซ์ž๋“ค ์ค‘์—๋Š” ์ด ์ˆซ์ž์™€ ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋Š” ์ „์ œ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ์ด ์ „์ œ๋ฅผ ๋ฐ”๋กœ ์œ„์— ์„ค๋ช…๋“œ๋ฆฐ first inner loop์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .


์ด๋Ÿฐ์‹์œผ๋กœ ํ•˜๊ฒŒ๋  ๊ฒฝ์šฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ( int[]์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅผ ๊ฒฝ์šฐ )์—๋„ O(n์ œ๊ณฑ) ๊นŒ์ง€๋Š” ์•ˆ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์ค‘๋ณต for loop๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ ๋‚˜๋ฆ„ ์†๋„๋„ ์‹ ๊ฒฝ์„ ์“ด ๋กœ์ง์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์ฃ .



public int maxSpan(int[] nums) {
  if ( nums.length == 0 ) return 0;
  int maxSpan = 1;
  int[] checkedNumbers = new int[nums.length];
  int checkedNumbersPos = 0;

 

  // Outer Most For Loop

  for ( int i = 0; i < nums.length ; i++ ){
     boolean checked = false;


     // First Inner Loop
     for( int checkedItem : checkedNumbers ){
        if ( checkedItem == nums[i] ){
           checked = true;
        }
     }
     if ( !checked ){
         checkedNumbers[checkedNumbersPos] = nums[i];


         // Second Inner Loop
         for ( int j = i + 1; j < nums.length ; j++ ){
            if ( nums[j] == nums[i] ){
               int tempSpan = j - i + 1;
               if ( tempSpan > maxSpan ){ maxSpan = tempSpan;}
            }
         }
     }
  }
  return maxSpan;
}

๋ฌธ์ œ๋Š” ์ด๊ณณ์— ์žˆ์Šต๋‹ˆ๋‹ค.


public int noTeenSum(int a, int b, int c) {
  int sum = 0;
  sum += fixTeen(a);
  sum += fixTeen(b);
  sum += fixTeen(c);
  return sum;
}
public int fixTeen(int n){
    if ( n > 12 && n < 20 ){
         if ( n != 15 && n != 16 ){
              return 0;
         }
    }
    return n;
}

์ฝ”๋”ฉ๋ฐ•์ฅ ๋‹ท์ปด์˜ ๋ฌธ์ œํ’€์ด์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ œ๊ฐ€ ์ง์ ‘ ์ฝ”๋”ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.


๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


public int luckySum(int a, int b, int c) {

  if ( a == 13 ){ return 0;}
  if ( b == 13 ){ return a;}
  if ( c == 13 ){ return a+b;}
  return a + b + c;
}

์ด๋ฒˆ์—๋Š” ์ฝ”๋”ฉ๋ฐ•์ฅ ๋‹ท์ปด์˜ loneSum ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๋Š” ์ด๊ณณ์— ๊ฐ€์‹œ๋ฉด ํ™•์ธ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


public int loneSum(int a, int b, int c) {
   int sum = 0;
   sum += a;
   if ( a == b ){
       sum -= b;
   }else{
       sum += b;
   }
   if ( a== c || b == c ){
       sum -= c;
   }else{

       sum+= c;

   }
   return ( sum < 0 ? 0 : sum );

}

[์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ]


์ด ๋ฌธ์ œ๋Š” ์ผ์ข…์˜ online judge ์‚ฌ์ดํŠธ์ธ ์ฝ”๋”ฉ๋ฑƒ(์ฝ”๋”ฉ๋ฐ•์ฅ) ๋‹ท์ปด์— ๋‚˜์™€์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ๋Š” ์ด๊ณณ์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.


์•„๋ž˜ ํ•ด๋‹ต์€ ์ œ๊ฐ€ ์ง์ ‘ ์ฝ”๋”ฉํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.


public boolean makeBricks(int small, int big, int goal) {
  if ( (goal - small <= big*5) && ( goal%5 <= small ) ){
      return true;
  }
  return false;
}

instrํ•จ์ˆ˜๋Š” ์–ด๋–ค ์ŠคํŠธ๋ง์˜ ํŠน์ •์œ„์น˜์—์„œ ์‹œ์ž‘ํ•ด์„œ ํŠน์ • ์ผ€๋ฆญํ„ฐ๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ๊นŒ์ง€์˜ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.


Java์˜ split ๋ฉ”์†Œ๋“œ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ• ๋•Œ ์˜ค๋ผํด์˜ substrํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์ฃ .


์˜ค๋ผํด ๋ฌธ์„œ์—์„œ ์„ค๋ช…ํ•˜๋Š” instrํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ •์˜ ๋ฐ ๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.


The INSTR functions (INSTR, INSTRB, INSTRC, INSTR2, and INSTR4) searches a string for a substring using characters and returns the position in the string that is the first character of a specified occurrence of the substring. The functions vary in how they determine the position of the substring to return.

  • INSTR calculates lengths using characters as defined by the input character set.

  • INSTRB calculates lengths using bytes.

  • INSTRC calculates lengths using Unicode complete characters.

  • INSTR2 calculates lengths using UCS2 code points.

  • INSTR4 calculates lengths using UCS4 code points.

๋ฐ˜ํ™˜๊ฐ’

A nonzero INTEGER when the search is successful or 0 (zero) when it is not.


๋ฌธ๋ฒ• 

{INSTR | INSTRB | INSTRC | INSTR2 | INSTR4} (string , substring [, position [, occurrence]])


๊ทธ๋Ÿผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด์‹œ์ฃ .


abc.def.ghi.jkl ์ด๋ผ๋Š” ํŒจํ‚ค์ง€๋ช…์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค.


์ด ํŒจํ‚ค์ง€๋ช…์—์„œ abc.def ( ๋‘๋ฒˆ์งธ depth ๊นŒ์ง€)๋งŒ ์ถ”์ถœํ•ด๋‚ด๊ณ  ์‹ถ์€๋ฐ ๊ทธ๋Ÿฌ๋ ค๋ฉด substr์„ ์ƒ๊ฐํ•˜์‹ค ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ .


select substr(packageNm, 1, 7) from ClassTable;


์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฒฐ๊ณผ๋Š” abc.def ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.


ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ํŒจํ‚ค์ง€๋ช…์ด 3๊ธ€์ž.3๊ธ€์ž ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?


a.b.c.d.e.f ๋ผ๋Š” ํŒจํ‚ค์ง€๋ช…์„ ์œ„์ฒ˜๋Ÿผ ์ž๋ฅด๋ฉด a.b.c.d ๋ผ๊ณ  ์ž˜๋ ค์„œ ๋‚˜์˜ค๊ฒ ์ฃ ?


๋‘๋ฒˆ์งธ depth๊ฐ€ ์•„๋‹ˆ๋ผ 7๋ฒˆ์งธ๊นŒ์ง€ ์ถœ๋ ฅ์ด ๋˜๋ฒ„๋ฆฌ๊ฒ ๊ตฐ์š”.


์ด๋•Œ instr ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ์„ ํ•ฉ๋‹ˆ๋‹ค.


select substr( packageNm, 1, instr( packageNm, '.', 1, 2) - 1 ) ) from ClassTable;


์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์จ์ค๋‹ˆ๋‹ค.


instr( packageNm, '.', 1, 2 ) ๋ผ๋Š” ๋ถ€๋ถ„์€ packageNm์ด๋ผ๋Š” ์ŠคํŠธ๋ง์„ '.' (๋งˆ์นจํ‘œ)๋กœ ๊ตฌ๋ถ„์„ ์ง“๊ณ  1๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•ด์„œ 2๋ฒˆ์งธ ๋งˆ์นจํ‘œ๊ฐ€ ๋‚˜์˜ค๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋งˆ์นจํ‘œ๊ฐ€ ์žˆ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— -1์„ ํ•ด์ค€ ๊ฐ’์„ substrํ•จ์ˆ˜์˜ length์— ๋„ฃ์–ด์ฃผ๋ฉด ๋‘๋ฒˆ์งธ depth๊นŒ์ง€ substrํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

### ์บ๋ฆญํ„ฐ์…‹ ๋ณ€๊ฒฝ์ „ ํ™•์ธ ์‚ฌํ•ญ :

sqlplus '/as sysdba'

sql>select instance from v$thread;

INSTANCE
----------------
ora9i


oracle\ora92\network\admin\snmp_ro.ora ๋ฅผ ๋ด๋„ ๋œ๋‹ค.
=> snmp.SID.Oracle = ORACLE




select parameter, value from nls_database_parameters where parameter like '%CHAR%';

PARAMETERVALUE
NLS_NUMERIC_CHARACTERS.,
NLS_CHARACTERSETKO16KSC5601
NLS_NCHAR_CONV_EXCPFALSE
NLS_NCHAR_CHARACTERSETUTF8


########### ์บ๋ฆญํ„ฐ์…‹ ๋ณ€๊ฒฝ ##########################

****  connect sys as sysdba;

NLS CHARACTERSET ๋ณ€๊ฒฝ๋ฐฉ๋ฒ• (DB REBUILD ์—†์ด)
Bulletin no : 10016

--------------------------------------------------------------------------------

  ๋ฐ์ดํƒ€๋ฒ ์ด์Šค์˜  CHARACTER SET์€ ๋ฐ์ดํƒ€ ๋”•์…”๋„ˆ๋ฆฌ ํ…Œ์ด๋ธ”์ธ sys.props$์—
 ๋“ค์–ด ์žˆ๋‹ค

   SQL>desc sys.props$
   Name                                Null?                  Type
   -------------------------------  -----------------      ---------------
   NAME                               NOT NULL             VARCHAR2(30)
   VALUE$                                                  VARCHAR2(2000)
   COMMENT$                                                VARCHAR2(2000)

   SQL>column c1 format a30
   SQL>select name c1, value$ c1 from sys.props$;

   C1                              C1
   -----------------------------   ------------------------------
   DICT.BASE                        2
   NLS_LANGUAGE                     AMERICAN
   NLS_TERRITORY                    AMERICA
   NLS_CURRENCY                     $
   NLS_ISO_CURRENCY                 AMERICA
   NLS_NUMERIC_CHARACTERS           .,
   NLS_DATE_FORMAT                  DD-MON-YY
   NLS_DATE_LANGUAGE                AMERICAN
   NLS_CHARACTERSET                 US7ASCII
   NLS_SORT                         BINARY
   GLOBAL_DB_NAME                   NLSV7.WORLD
   
  ์—ฌ๊ธฐ์„œ NLS_CHARACTERSET์— ํ˜„์žฌ DB์˜ CHARACTER SET์ด ๋“ค์–ด ์žˆ๋Š”๋ฐ ์ด ๊ฐ’์„
 ๋ณ€๊ฒฝํ•˜์—ฌ DB์˜ CHARACTER SET์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” US7ASCII์—์„œ
 KO16KSC5601 ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ์•Œ์•„๋ณด์ž.

 ์šฐ์„  ๋ฐ”๊พธ๊ณ ์ž ํ•˜๋Š” CHRACTER SET์ด ์ง€์›๋˜๋Š” ์ง€๋ฅผ ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•œ๋‹ค.

         select convert('a','WE8DEC','KO16MSWIN949') from dual; 

  ๋งŒ์•ฝ ์ด Select ๋ฌธ์—์„œ ORA-01482 ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ง€์ •ํ•œ CHARACTER SET์ด
 ์ง€์›๋˜์ง€ ์•Š๋Š”  ๊ฒฝ์šฐ์ด๋ฉฐ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด CHARACTER SET์„ ๋ณ€๊ฒฝํ•  ์ˆ˜
 ์žˆ๋‹ค.
 
  ์ž‘์—…์„ ํ•˜๊ธฐ์ „์—๋Š” ๋งŒ์•ฝ์„ ์œ„ํ•ด์„œ DB ์ „์ฒด๋ฅผ ๋ฐฑ์—… ๋ฐ›์•„๋‘๋„๋ก ํ•œ๋‹ค.
  CHARACTER SET ์„ ์ž˜๋ชป ๋ณ€๊ฒฝํ•˜๋ฉด DB ๋ฅผ OPEN ํ• ์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  ---------------------------------------------------------------

1.  ๋‹ค์Œ์˜ Update๋ฌธ์„ ์‹คํ–‰ํ•˜์—ฌ CHARACTER SET์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

   UPDATE sys.props$
   SET value$ = 'WE8DEC'
   WHERE name = 'NLS_CHARACTERSET';

  Update ์‹œ์— NLS_CHARACTERSET์„ ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฐ’์œผ๋กœ ์ž˜๋ชป ์„ค์ •ํ•˜๊ฑฐ๋‚˜
 ์‹ค์ˆ˜๋กœ ์ฝ˜ํŠธ๋กค ๋ฌธ์ž ๊ฐ™์€ ๊ฒƒ์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด DB๊ฐ€ Shutdown ๋œ ๋‹ค์Œ์—๋Š”
 Startup ์ด ์•ˆ ๋˜๋ฏ€๋กœ Update ํ›„์— ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ํ™•์ธ์„ ํ•œ ๋‹ค์Œ์—  Commit์„
 ํ•˜๋„๋ก ํ•œ๋‹ค.

        select name, value$
        from sys.props$
        where value$ = 'KO16KSC5601';

 Select   ์ œ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๋ฉด Commit ํ•˜๊ณ  Shutdown ํ–ˆ๋‹ค๊ฐ€ Startup ํ•˜๊ฒŒ ๋˜๋ฉด
 ์ƒˆ๋กœ์šด CHARACTER SET ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค. SELECT๊ฐ€ ์•ˆ ๋˜๋ฉด ROLLBACKํ•˜๊ณ  UPDATE
 ๋ถ€ํ„ฐ ๋‹ค์‹œ ํ•˜๋„๋ก ํ•œ๋‹ค.

2. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ NLS_LANG ์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

.profile ( or .cshrc) ์—์„œ

NLS_LANG=American_America.KO16KSC5601; export NLS_LANG

or

setenv NLS_LANG American_America.KO16KSC5601






์ถœ์ฒ˜ : http://develop.sunshiny.co.kr/201



SQL ์ปค๋งจ๋“œ๋ผ์ธ์—๋‹ค๊ฐ€ 2์ค„์งœ๋ฆฌ ์ฟผ๋ฆฌ๋ฌธ์„ ํ•œ์ฐธ ์น˜๊ณ ์„œ ์‹คํ–‰ํ–ˆ๋”๋‹ˆ ์˜คํƒ€๊ฐ€ ๋‚˜์„œ ๋‹ค์‹œ ์ณ์•ผํ•œ๋‹ค๋ฉด???


OMG !!!!!!!!!!


์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ ์˜ค๋ผํด์€ SQL ์—๋””ํ„ฐ๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š”๊ฒŒ ๋ญ”์ง„ ๋ชฐ๋ผ๋„ ๋ถˆํŽธํ•˜๋‹ค.


์ข€๋” ํŽธํ•˜๊ฒŒ ํ•˜์ž๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์œ ๋‹‰์Šค ํ™˜๊ฒฝ์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” vi ์—๋””ํ„ฐ๋ฅผ SQL ๊ธฐ๋ณธ ์—๋””ํ„ฐ๋กœ ์„ค์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ


์ด ๊ธฐ๋ณธ ์—๋””ํ„ฐ๋Š” _editor ๋ผ๋Š” ๋ณ€์ˆ˜์•ˆ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค.


SQL > define _editor = vi  


์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ed๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฉ๊ธˆ ์ณค๋˜ ๋ช…๋ น์–ด๋ฅผ vi์—๋””ํ„ฐ ๋ชจ๋“œ์—์„œ ๋‹ค์‹œ ๋ณด์—ฌ์ค€๋‹ค.


์งฑ์ด๋‹ค.

DB์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€๋ณด๋ฉด ๋‚ด ๊ณ„์ •์— ์–ด๋–ค ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

๊ถŒํ•œ์ž์ฒด๊ฐ€ ์—†์œผ๋ฉด ์ž‘์—…์ž์ฒด๊ฐ€ ์•ˆ๋  ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ๊นŒ ๋ง์ด๋‹ค.

์•„๋ž˜ ์ฟผ๋ฆฌ๋ฌธ์€ ํ˜„์žฌ ์ ‘์†ํ•œ ๊ณ„์ •์˜ ๊ถŒํ•œ์„ ๋ณด์—ฌ์ค€๋‹ค.


select * from user_sys_privs;



user_sys_privs์™ธ์—๋„ ๊ถŒํ•œ๊ณผ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ํ…Œ์ด๋ธ” ๋˜๋Š” ๋ทฐ(view)๋“ค์ด ์žˆ๋‹ค. ์•„๋ž˜๋Š” ๊ทธ ํ…Œ์ด๋ธ”๋“ค์˜ ๋ชฉ๋ก์ด๋‹ค.

๊ฐ ํ…Œ์ด๋ธ”๋“ค์ด ์–ด๋– ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋Š” ํ•œ๋ฒˆ ๋ณด๋ฉด์„œ ํ™•์ธํ•ด๋ณด๊ธธ ๋ฐ”๋ž€๋‹ค.



ALL_COL_PRIVS
ALL_COL_PRIVS_MADE
ALL_COL_PRIVS_RECD
ALL_REPGROUP_PRIVILEGES
ALL_TAB_PRIVS
ALL_TAB_PRIVS_MADE
ALL_TAB_PRIVS_RECD


USER_AQ_AGENT_PRIVS
USER_COL_PRIVS
USER_COL_PRIVS_MADE
USER_COL_PRIVS_RECD
USER_REPGROUP_PRIVILEGES

TNAME
------------------------------
USER_ROLE_PRIVS
USER_SYS_PRIVS
USER_TAB_PRIVS
USER_TAB_PRIVS_MADE
USER_TAB_PRIVS_RECD


๐Ÿ’ป Programming/Oracle 11g

[Oracle/SQL] merge into

MERGE INTO bonuses b
USING (
  SELECT employee_id, salary, dept_no
  FROM employee
  WHERE dept_no =20) e
ON (b.employee_id = e.employee_id)
WHEN MATCHED THEN
  UPDATE SET b.bonus = e.salary * 0.1
  DELETE WHERE (e.salary < 40000)
WHEN NOT MATCHED THEN
  INSERT (b.employee_id, b.bonus) VALUES (e.employee_id, e.salary * 0.05) WHERE (e.salary > 40000);



----------------------------------------------------


MERGE INTO bonuses b
USING dual e
ON (b.employee_id = ? )
WHEN MATCHED THEN
  UPDATE SET b.bonus = e.salary * 0.1
WHEN NOT MATCHED THEN
  INSERT (b.employee_id, b.bonus) VALUES (e.employee_id, e.salary * 0.05) WHERE (e.salary > 40000);


์‚ฌ์šฉ์ž๊ณ„์ •์ด SAMPLE ์ธ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ์กฐํšŒํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

select privilege
from dba_sys_privs
where grantee='SAMPLE'
order by 1;

Oracle 10g Character set ๋ณ€๊ฒฝ(EUC-KR์„ UTF8 ๋ณ€๊ฒฝ) :
์ฐธ๊ณ 

kor ๋ฒ„์ „์œผ๋กœ ๋ฐ›๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜๋˜๋Š” ๋ฒ„์ „์€ KO16MSWIN949 ์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
ํ•œ๊ธ€์„ ์ง€์›ํ•˜๋Š” Character Set์„ ๋น„๊ตํ•ด๋ณด์ž.

1. KO16KSC5601
- ํ•œ๊ธ€ ์ง€์›์ƒํƒœ : ํ•œ๊ธ€ 2350์ž
- ์ง€์›๋ฒ„์ „ : 7.x
- ํ•œ๊ธ€๋ฐ”์ดํŠธ : 2๋ฐ”์ดํŠธ

2. KO16MSWIN949
- ํ•œ๊ธ€ ์ง€์›์ƒํƒœ : KO16KSC5601 + ํ™•์žฅ ( ์ด 11172์ž )
- ์ง€์›๋ฒ„์ „ : 8.0.6 ์ด์ƒ
- ํ•œ๊ธ€๋ฐ”์ดํŠธ : 2๋ฐ”์ดํŠธ

3. UTF8
- ํ•œ๊ธ€ ์ง€์›์ƒํƒœ : ํ•œ๊ธ€ 11172์ž
- ์ง€์›๋ฒ„์ „ : 8.0 ์ดํ›„
- ํ•œ๊ธ€๋ฐ”์ดํŠธ : 3๋ฐ”์ดํŠธ

4. AL32UTF8
- ํ•œ๊ธ€ ์ง€์›์ƒํƒœ : ํ•œ๊ธ€ 11172์ž
- ์ง€์›๋ฒ„์ „ : 9i Release 1 ์ด์ƒ
- ํ•œ๊ธ€๋ฐ”์ดํŠธ : 3๋ฐ”์ดํŠธ

UTF8์€ ๋งŽ์€ ๋ฌธ์ž๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ ํ•œ๊ธ€์„ 3๋ฐ”์ดํŠธ ์†Œ๋ชจํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.
(๋ชป๋Š๋‚„ ์ •๋„๋กœ ๋น ๋ฅธ ์ปดํ“จํ„ฐ๊ฐ€ ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†์„ ๋“ฏํ•จ)
์บ๋ฆญํ„ฐ์…‹์ด ์–ด๋–ป๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ๋‚˜ ํ™•์ธ ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹คใ…ฃ

SELECT * FROM sys.props$ where name='NLS_CHARACTERSET';

Oracle 10g Character Set ๋ณ€๊ฒฝ
SQLPLUS ์ ‘์†ํ›„ (system ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ ํ˜น์‹œ ๋ชจ๋ฅธ๋‹ค๋ฉด sqlplus /nolog; ํ›„ conn /as sysdba; ๋กœ ์ ‘์†ํ•œ๋‹ค)

C:\>sqlplus /nolog;
sql>conn /as sysdba;
๋ณ€๊ฒฝํ•˜๊ณ ์žํ•˜๋Š” ์บ๋ฆญํ„ฐ์…‹์„ ์ˆ˜์ •

sql>update sys.props$ set value$='UTF8' where name='NLS_CHARACTERSET';
sql>update sys.props$ set value$='UTF8' where name='NLS_NCHAR_CHARACTERSET';
sql>
update sys.props$ set value$='KOREAN_KOREA.UTF8' where name='NLS_LANGUAGE';
sql>commit;
sql>shutdown immediate;
sql>startup mount;
sql>alter system enable restricted session;
sql>alter system set job_queue_processes=0;
sql>alter system set aq_tm_processes=0;
sql>alter database open;
sql>alter database character set UTF8;
sql>shutdown immediate;
sql>startup;

์ถœ์ฒ˜ : http://www.garubi.com/11

ํ…Œ์ด๋ธ”์— ์–ด๋–ค ๋ฐ์ดํƒ€๋ฅผ insert, update, delete๋ฅผ ํ•  ๋•Œ๋Š” ํ•œ ๋ช…์˜ ์‚ฌ์šฉ์ž๋งŒ์ด ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.


๊ทธ ๋Ÿฐ๋ฐ ๋งŒ์•ฝ Aํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํƒ€๋ฅผ ์ฝ์–ด์™€์„œ ์ด์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ Bํ…Œ์ด๋ธ”์— ๋„ฃ์œผ๋ ค๋Š”๋ฐ Bํ…Œ์ด๋ธ”์— FK๋กœ Aํ…Œ์ด๋ธ”์˜ PK๊ฐ€ ๊ฑธ๋ ค์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ๋‚˜๋Š” Aํ…Œ์ด๋ธ”์—์„œ pk๊ฐ€ 1์ธ ๋ฐ์ดํƒ€๋ฅผ ์ฝ์–ด์™€์„œ Bํ…Œ์ด๋ธ”์— ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ ๋ฐ์ดํƒ€๋ฅผ ์ฝ์–ด์™€์„œ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์ „์— ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋ฐ์ดํƒ€ 1์„ ์ง€์›Œ๋ฒ„๋ ธ๋‹ค๋ฉด?? ๋‚ด๊ฐ€ Bํ…Œ์ด๋ธ”์— ์—…๋ฐ์ดํŠธ๋ฅผ ์‹œ๋„ํ•  ๋•Œ fk ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค. pk๊ฐ€ ์‚ฌ๋ผ์กŒ์œผ๋‹ˆ ๋ง์ด๋‹ค.


์ž ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด select ... for update ์ด๋‹ค.

 


for update๋Š” ์ฝ์–ด์˜จ ๋ชจ๋“  rows์™€ ๊ทธ rows์™€ ๊ด€๋ จ๋œ ์ธ๋ฑ์Šค ์—”ํŠธ๋ฆฌ๊นŒ์ง€ lock์„ ํ•œ๋‹ค. ์ด๋Š” update๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๋™์ผํ•œ ๊ฒƒ์ด๋‹ค. for update๋กœ ์‹คํ–‰๋œ select๋ฌธ์€ ๋‹ค๋ฅธ ๋ชจ๋“  ์—…๋ฐ์ดํŠธ ๊ด€๋ จ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋˜ ๋‹ค๋ฅธ select ... for update ๋ฌธ ์—ญ์‹œ ๊ธฐ์กด ์„ธ์…˜์—์„œ commit์ด๋‚˜ rollback์ด ์‹คํ–‰๋˜๊ธฐ ์ „๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ์ตœ์‹ ์ •๋ณด๋ฅผ selectํ•˜๊ฒŒ ๋œ๋‹ค.


์‚ฌ์šฉ๋ฒ•์€ ๋‹จ์ˆœํ•˜๋‹ค.


SQL > select * from emp for update;


SQL FOREIGN KEY Constraint on ALTER TABLE

- ์™ธ๋ž˜ํ‚ค ์ถ”๊ฐ€ํ•˜๊ธฐ

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)


- ์™ธ๋ž˜ํ‚ค ์ด๋ฆ„ ์ง€์ •ํ•ด์ฃผ๊ธฐ

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)


To DROP a FOREIGN KEY Constraint

- ์™ธ๋ž˜ํ‚ค ์‚ญ์ œํ•˜๊ธฐ 


MySQL:

ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders

SQL Server / Oracle / MS Access:

ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders


ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ํ…Œ์ด๋ธ” ๋ชจ๋ธ๋ง์„ ์ˆ˜์ •ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ์ €๋Ÿฐ ์ถ”๊ฐ€์š”๊ตฌ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ง์ด๋‹ค.

 

๊ทธ๋Ÿด ๋•Œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ SQL ํ•œ๋ฌธ์žฅ์œผ๋กœ ํ›„๋”ฑ ํ•ด์น˜์›Œ ๋ฒ„๋ฆฌ๋ฉด ๋œ๋‹ค.

 

ยง ์•„๋ž˜๋Š” ํ…Œ์ด๋ธ”์— ํ•˜๋‚˜์˜ ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ธฐ์œ„ํ•œ SQL๋ฌธ์ด๋‹ค.

 

ALTER TABLE table_name ADD column_name datatype

 

ยง ํ…Œ์ด๋ธ”์—์„œ ํ•„์š”์—†๋Š” ์ปฌ๋Ÿผ์„ ์‚ญ์ œํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

ALTER TABLE table_name DROP COLUMN column_name

ยง ๋˜ํ•œ ํŠน์ • ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํƒ€ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Oracle 10G and later: ALTER TABLE table_name MODIFY column_name datatype




ยง ์˜ค๋ผํด์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์˜ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

SQL Server / MS Access:

ALTER TABLE table_name ALTER COLUMN column_name datatype

My SQL / Oracle:

ALTER TABLE table_name MODIFY COLUMN column_name datatype


๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด ์„ฑ๋Šฅ๋ฌธ์ œ๋ฅผ ์ธ์‹ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๋‹ค. JMeter๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ…Œ์ŠคํŠธ ํ•˜๋ฉด์„œ ์„ฑ๋Šฅ๊ฐœ์„ ์„ ์œ„ํ•ด์„œ ์ด๊ฒƒ ์ €๊ฒƒ ํ•ด๋ณด๋‹ค๊ฐ€ ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” " Insert ๋ฌธ์˜ ์†๋„ "์— ๊ด€ํ•œ ๋ฌธ์„œ๋ฅผ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค.  

 

์•„๋ž˜ ๊ทธ ๋ฌธ์„œ์˜ ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉฐ insert๋ฌธ์˜ ์†๋„์™€ ๊ด€๋ จ๋œ ์‚ฌํ•ญ๋“ค ๋ฐ ์–ด๋–ป๊ฒŒ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…์„ ํ•˜๊ณ  ์žˆ๋‹ค. 

์ถœ์ฒ˜ : https://docs.oracle.com/cd/E17952_01/refman-5.1-en/insert-speed.html

Speed of INSERT Statements

The time required for inserting a row is determined by the following factors, where the numbers indicate approximate proportions:

  • Connecting: (3)

  • Sending query to server: (2)

  • Parsing query: (2)

  • Inserting row: (1 ร— size of row)

  • Inserting indexes: (1 ร— number of indexes)

  • Closing: (1)

This does not take into consideration the initial overhead to open tables, which is done once for each concurrently running query.

The size of the table slows down the insertion of indexes by log N, assuming B-tree indexes.

You can use the following methods to speed up inserts:

  • If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUES lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a nonempty table, you can tune the bulk_insert_buffer_size variable to make data insertion even faster. See Section 5.1.4, โ€œServer System Variablesโ€.

  • If multiple clients are inserting a lot of rows, you can get higher speed by using the INSERT DELAYED statement. See Section 13.2.5.2, โ€œINSERT DELAYED Syntaxโ€.

  • For a MyISAM table, you can use concurrent inserts to add rows at the same time that SELECT statements are running, if there are no deleted rows in middle of the data file. See Section 8.7.3, โ€œConcurrent Insertsโ€.

  • When loading a table from a text file, use LOAD DATA INFILE. This is usually 20 times faster than using INSERT statements. See Section 13.2.6, โ€œLOAD DATA INFILE Syntaxโ€.

  • With some extra work, it is possible to make LOAD DATA INFILE run even faster for a MyISAM table when the table has many indexes. Use the following procedure:

    1. Optionally create the table with CREATE TABLE.

    2. Execute a FLUSH TABLES statement or a mysqladmin flush-tables command.

    3. Use myisamchk --keys-used=0 -rq /path/to/db/tbl_name. This removes all use of indexes for the table.

    4. Insert data into the table with LOAD DATA INFILE. This does not update any indexes and therefore is very fast.

    5. If you intend only to read from the table in the future, use myisampack to compress it. See Section 14.5.3.3, โ€œCompressed Table Characteristicsโ€.

    6. Re-create the indexes with myisamchk -rq /path/to/db/tbl_name. This creates the index tree in memory before writing it to disk, which is much faster that updating the index during LOAD DATA INFILE because it avoids lots of disk seeks. The resulting index tree is also perfectly balanced.

    7. Execute a FLUSH TABLES statement or a mysqladmin flush-tables command.

    LOAD DATA INFILE performs the preceding optimization automatically if the MyISAM table into which you insert data is empty. The main difference between automatic optimization and using the procedure explicitly is that you can let myisamchk allocate much more temporary memory for the index creation than you might want the server to allocate for index re-creation when it executes the LOAD DATA INFILE statement.

    You can also disable or enable the nonunique indexes for a MyISAM table by using the following statements rather than myisamchk. If you use these statements, you can skip the FLUSH TABLE operations:

    ALTER TABLE tbl_name DISABLE KEYS;
    ALTER TABLE tbl_name ENABLE KEYS;
    
  • To speed up INSERT operations that are performed with multiple statements for nontransactional tables, lock your tables:

    LOCK TABLES a WRITE;
    INSERT INTO a VALUES (1,23),(2,34),(4,33);
    INSERT INTO a VALUES (8,26),(6,29);
    ...
    UNLOCK TABLES;
    

    This benefits performance because the index buffer is flushed to disk only once, after all INSERT statements have completed. Normally, there would be as many index buffer flushes as there are INSERT statements. Explicit locking statements are not needed if you can insert all rows with a single INSERT.

    To obtain faster insertions for transactional tables, you should use START TRANSACTION and COMMIT instead of LOCK TABLES.

    Locking also lowers the total time for multiple-connection tests, although the maximum wait time for individual connections might go up because they wait for locks. Suppose that five clients attempt to perform inserts simultaneously as follows:

    • Connection 1 does 1000 inserts

    • Connections 2, 3, and 4 do 1 insert

    • Connection 5 does 1000 inserts

    If you do not use locking, connections 2, 3, and 4 finish before 1 and 5. If you use locking, connections 2, 3, and 4 probably do not finish before 1 or 5, but the total time should be about 40% faster.

    INSERT, UPDATE, and DELETE operations are very fast in MySQL, but you can obtain better overall performance by adding locks around everything that does more than about five successive inserts or updates. If you do very many successive inserts, you could do a LOCK TABLES followed by an UNLOCK TABLES once in a while (each 1,000 rows or so) to permit other threads to access table. This would still result in a nice performance gain.

    INSERT is still much slower for loading data than LOAD DATA INFILE, even when using the strategies just outlined.

  • To increase performance for MyISAM tables, for both LOAD DATA INFILE and INSERT, enlarge the key cache by increasing the key_buffer_size system variable. See Section 8.9.2, โ€œTuning Server Parametersโ€.



Partitioned Tables and Indexes

This chapter describes partitioned tables and indexes. It covers the following topics:

Introduction to Partitioning

Partitioning addresses key issues in supporting very large tables and indexes by letting you decompose them into smaller and more manageable pieces called partitions. SQL queries and DML statements do not need to be modified in order to access partitioned tables. However, after partitions are defined, DDL statements can access and manipulate individuals partitions rather than entire tables or indexes. This is how partitioning can simplify the manageability of large database objects. Also, partitioning is entirely transparent to applications.

Each partition of a table or index must have the same logical attributes, such as column names, datatypes, and constraints, but each partition can have separate physical attributes such as pctfree, pctused, and tablespaces.

Partitioning is useful for many different types of applications, particularly applications that manage large volumes of data. OLTP systems often benefit from improvements in manageability and availability, while data warehousing systems benefit from performance and manageability.


Note:

All partitions of a partitioned object must reside in tablespaces of a single block size.


See Also:

Partitioning offers these advantages:

  • Partitioning enables data management operations such data loads, index creation and rebuilding, and backup/recovery at the partition level, rather than on the entire table. This results in significantly reduced times for these operations.
  • Partitioning improves query performance. In many cases, the results of a query can be achieved by accessing a subset of partitions, rather than the entire table. For some queries, this technique (called partition pruning) can provide order-of-magnitude gains in performance.
  • Partitioning can significantly reduce the impact of scheduled downtime for maintenance operations.

    Partition independence for partition maintenance operations lets you perform concurrent maintenance operations on different partitions of the same table or index. You can also run concurrent SELECT and DML operations against partitions that are unaffected by maintenance operations.

  • Partitioning increases the availability of mission-critical databases if critical tables and indexes are divided into partitions to reduce the maintenance windows, recovery times, and impact of failures.
  • Partitioning can be implemented without requiring any modifications to your applications. For example, you could convert a nonpartitioned table to a partitioned table without needing to modify any of the SELECT statements or DML statements which access that table. You do not need to rewrite your application code to take advantage of partitioning.

Figure 11-1 offers a graphical view of how partitioned tables differ from nonpartitioned tables.

Figure 11-1 A View of Partitioned Tables


Text description of the illustration cncpt162.gif


Partition Key

Each row in a partitioned table is unambiguously assigned to a single partition. The partition key is a set of one or more columns that determines the partition for each row. Oracle9i automatically directs insert, update, and delete operations to the appropriate partition through the use of the partition key. A partition key:

  • Consists of an ordered list of 1 to 16 columns
  • Cannot contain a LEVEL, ROWID, or MLSLABEL pseudocolumn or a column of type ROWID
  • Can contain columns that are NULLable

Partitioned Tables

Tables can be partitioned into up to 64,000 separate partitions. Any table can be partitioned except those tables containing columns with LONG or LONG RAW datatypes. You can, however, use tables containing columns with CLOB or BLOB datatypes.

Partitioned Index-Organized Tables

You can range partition index-organized tables. This feature is very useful for providing improved manageability, availability and performance for index-organized tables. In addition, data cartridges that use index-organized tables can take advantage of the ability to partition their stored data. Common examples of this are the Image and interMedia cartridges.

For partitioning an index-organized table:

  • Only range and hash partitioning are supported
  • Partition columns must be a subset of primary key columns
  • Secondary indexes can be partitioned -- locally and globally
  • OVERFLOW data segments are always equipartitioned with the table partitions

Partitioning Methods

Oracle provides the following partitioning methods:

Figure 11-2 offers a graphical view of the methods of partitioning.

Figure 11-2 List, Range, and Hash Partitioning


Text description of the illustration cncpt158.gif


Composite partitioning is a combination of other partitioning methods. Oracle currently supports range-hash and range-list composite partitioning. Figure 11-3 offers a graphical view of range-hash and range-list composite partitioning.

Figure 11-3 Composite Partitioning


Text description of the illustration cncpt168.gif


Range Partitioning

Range partitioning maps data to partitions based on ranges of partition key values that you establish for each partition. It is the most common type of partitioning and is often used with dates. For example, you might want to partition sales data into monthly partitions.

When using range partitioning, consider the following rules:

  • Each partition has a VALUES LESS THAN clause, which specifies a noninclusive upper bound for the partitions. Any binary values of the partition key equal to or higher than this literal are added to the next higher partition.
  • All partitions, except the first, have an implicit lower bound specified by the VALUES LESS THAN clause on the previous partition.
  • A MAXVALUE literal can be defined for the highest partition. MAXVALUE represents a virtual infinite value that sorts higher than any other possible value for the partition key, including the null value.

A typical example is given in the following section. The statement creates a table (sales_range) that is range partitioned on the sales_date field.

Range Partitioning Example

CREATE TABLE sales_range 
(salesman_id  NUMBER(5), 
salesman_name VARCHAR2(30), 
sales_amount  NUMBER(10), 
sales_date    DATE)
PARTITION BY RANGE(sales_date) 
(
PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','DD/MM/YYYY')),
PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','DD/MM/YYYY')),
PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','DD/MM/YYYY')),
PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','DD/MM/YYYY'))
);

List Partitioning

List partitioning enables you to explicitly control how rows map to partitions. You do this by specifying a list of discrete values for the partitioning key in the description for each partition. This is different from range partitioning, where a range of values is associated with a partition and from hash partitioning, where a hash function controls the row-to-partition mapping. The advantage of list partitioning is that you can group and organize unordered and unrelated sets of data in a natural way.

The details of list partitioning can best be described with an example. In this case, let's say you want to partition a sales table by region. That means grouping states together according to their geographical location as in the following example.

List Partitioning Example

CREATE TABLE sales_list
(salesman_id  NUMBER(5), 
salesman_name VARCHAR2(30),
sales_state   VARCHAR2(20),
sales_amount  NUMBER(10), 
sales_date    DATE)
PARTITION BY LIST(sales_state)
(
PARTITION sales_west VALUES('California', 'Hawaii'),
PARTITION sales_east VALUES ('New York', 'Virginia', 'Florida'),
PARTITION sales_central VALUES('Texas', 'Illinois')
PARTITION sales_other VALUES(DEFAULT)
);

A row is mapped to a partition by checking whether the value of the partitioning column for a row falls within the set of values that describes the partition. For example, the rows are inserted as follows:

  • (10, 'Jones', 'Hawaii', 100, '05-JAN-2000') maps to partition sales_west
  • (21, 'Smith', 'Florida', 150, '15-JAN-2000') maps to partition sales_east
  • (32, 'Lee', 'Colorado', 130, '21-JAN-2000') does not map to any partition in the table

Unlike range and hash partitioning, multicolumn partition keys are not supported for list partitioning. If a table is partitioned by list, the partitioning key can only consist of a single column of the table.

The DEFAULT partition enables you to avoid specifying all possible values for a list-partitioned table by using a default partition, so that all rows that do not map to any other partition do not generate an error.

Hash Partitioning

Hash partitioning enables easy partitioning of data that does not lend itself to range or list partitioning. It does this with a simple syntax and is easy to implement. It is a better choice than range partitioning when:

  • You do not know beforehand how much data maps into a given range
  • The sizes of range partitions would differ quite substantially or would be difficult to balance manually
  • Range partitioning would cause the data to be undesirably clustered
  • Performance features such as parallel DML, partition pruning, and partition-wise joins are important

The concepts of splitting, dropping or merging partitions do not apply to hash partitions. Instead, hash partitions can be added and coalesced.

Hash Partitioning Example

CREATE TABLE sales_hash
(salesman_id  NUMBER(5), 
salesman_name VARCHAR2(30), 
sales_amount  NUMBER(10), 
week_no       NUMBER(2)) 
PARTITION BY HASH(salesman_id) 
PARTITIONS 4 
STORE IN (data1, data2, data3, data4);

The preceding statement creates a table sales_hash, which is hash partitioned on salesman_id field. The tablespace names are data1, data2, data3, and data4.

Composite Partitioning

Composite partitioning partitions data using the range method, and within each partition, subpartitions it using the hash or list method. Composite range-hash partitioning provides the improved manageability of range partitioning and the data placement, striping, and parallelism advantages of hash partitioning. Composite range-list partitioning provides the manageability of range partitioning and the explicit control of list partitioning for the subpartitions.

Composite partitioning supports historical operations, such as adding new range partitions, but also provides higher degrees of parallelism for DML operations and finer granularity of data placement through subpartitioning.

Composite Partitioning Range-Hash Example

CREATE TABLE sales_composite 
(salesman_id  NUMBER(5), 
 salesman_name VARCHAR2(30), 
 sales_amount  NUMBER(10), 
 sales_date    DATE)
PARTITION BY RANGE(sales_date) 
SUBPARTITION BY HASH(salesman_id)
SUBPARTITION TEMPLATE(
SUBPARTITION sp1 TABLESPACE data1,
SUBPARTITION sp2 TABLESPACE data2,
SUBPARTITION sp3 TABLESPACE data3,
SUBPARTITION sp4 TABLESPACE data4)
(PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','DD/MM/YYYY'))
 PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','DD/MM/YYYY'))
 PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','DD/MM/YYYY'))
 PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','DD/MM/YYYY'))
 PARTITION sales_may2000 VALUES LESS THAN(TO_DATE('06/01/2000','DD/MM/YYYY')));

This statement creates a table sales_composite that is range partitioned on the sales_date field and hash subpartitioned on salesman_id. When you use a template, Oracle names the subpartitions by concatenating the partition name, an underscore, and the subpartition name from the template. Oracle places this subpartition in the tablespace specified in the template. In the previous statement, sales_jan2000_sp1 is created and placed in tablespace data1 while sales_jan2000_sp4 is created and placed in tablespace data4. In the same manner, sales_apr2000_sp1 is created and placed in tablespace data1 while sales_apr2000_sp4 is created and placed in tablespace data4. Figure 11-4 offers a graphical view of the previous example.

Figure 11-4 Composite Range-Hash Partitioning


Text description of the illustration cncpt157.gif


Composite Partitioning Range-List Example

CREATE TABLE bimonthly_regional_sales
(deptno NUMBER, 
 item_no VARCHAR2(20),
 txn_date DATE, 
 txn_amount NUMBER, 
 state VARCHAR2(2))
PARTITION BY RANGE (txn_date)
SUBPARTITION BY LIST (state)
SUBPARTITION TEMPLATE(


SUBPARTITION east VALUES('NY', 'VA', 'FL') TABLESPACE ts1,
SUBPARTITION west VALUES('CA', 'OR', 'HI') TABLESPACE ts2,
SUBPARTITION central VALUES('IL', 'TX', 'MO') TABLESPACE ts3)
( PARTITION janfeb_2000 VALUES LESS THAN (TO_DATE('1-MAR-2000','DD-MON-YYYY')), PARTITION marapr_2000 VALUES LESS THAN (TO_DATE('1-MAY-2000','DD-MON-YYYY')), PARTITION mayjun_2000 VALUES LESS THAN (TO_DATE('1-JUL-2000','DD-MON-YYYY')) );

This statement creates a table bimonthly_regional_sales that is range partitioned on the txn_date field and list subpartitioned on state. When you use a template, Oracle names the subpartitions by concatenating the partition name, an underscore, and the subpartition name from the template. Oracle places this subpartition in the tablespace specified in the template. In the previous statement, janfeb_2000_east is created and placed in tablespace ts1 while janfeb_2000_central is created and placed in tablespace ts3. In the same manner, mayjun_2000_east is placed in tablespace ts1 while mayjun_2000_central is placed in tablespace ts3. Figure 11-5 offers a graphical view of the table bimonthly_regional_sales and its 9 individual subpartitions.

Figure 11-5 Composite Range-List Partitioning


Text description of the illustration cncpt167.gif


When to Partition a Table

Here are some suggestions for when to partition a table:

  • Tables greater than 2GB should always be considered for partitioning.
  • Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month's data is updatable and the other 11 months are read-only.

Partitioned Indexes

Just like partitioned tables, partitioned indexes improve manageability, availability, performance, and scalability. They can either be partitioned independently (global indexes) or automatically linked to a table's partitioning method (local indexes).

See Also:

Oracle9i Data Warehousing Guide for more information about partitioned indexes

Local Partitioned Indexes

Local partitioned indexes are easier to manage than other types of partitioned indexes. They also offer greater availability and are common in DSS environments. The reason for this is equipartitioning: each partition of a local index is associated with exactly one partition of the table. This enables Oracle to automatically keep the index partitions in sync with the table partitions, and makes each table-index pair independent. Any actions that make one partition's data invalid or unavailable only affect a single partition.

You cannot explicitly add a partition to a local index. Instead, new partitions are added to local indexes only when you add a partition to the underlying table. Likewise, you cannot explicitly drop a partition from a local index. Instead, local index partitions are dropped only when you drop a partition from the underlying table.

A local index can be unique. However, in order for a local index to be unique, the partitioning key of the table must be part of the index's key columns. Unique local indexes are useful for OLTP environments.

Figure 11-6 offers a graphical view of local partitioned indexes.

Figure 11-6 Local Partitioned Index


Text description of the illustration cncpt161.gif


Global Partitioned Indexes

Global partitioned indexes are flexible in that the degree of partitioning and the partitioning key are independent from the table's partitioning method. They are commonly used for OLTP environments and offer efficient access to any individual record.

The highest partition of a global index must have a partition bound, all of whose values are MAXVALUE. This ensures that all rows in the underlying table can be represented in the index. Global prefixed indexes can be unique or nonunique.

You cannot add a partition to a global index because the highest partition always has a partition bound of MAXVALUE. If you wish to add a new highest partition, use the ALTER INDEX SPLIT PARTITION statement. If a global index partition is empty, you can explicitly drop it by issuing the ALTER INDEX DROP PARTITION statement. If a global index partition contains data, dropping the partition causes the next highest partition to be marked unusable. You cannot drop the highest partition in a global index.

Maintenance of Global Partitioned Indexes

By default, the following operations on partitions on a heap-organized table mark all global indexes as unusable:

ADD (HASH) 
COALESCE (HASH) 
DROP 
EXCHANGE 
MERGE 
MOVE 
SPLIT 
TRUNCATE 

These indexes can be maintained by appending the clause UPDATE GLOBAL INDEXES to the SQL statements for the operation. The two advantages to maintaining global indexes:

  • The index remains available and online throughout the operation. Hence no other applications are affected by this operation.
  • The index doesn't have to be rebuilt after the operation.
Example:
ALTER TABLE DROP PARTITION P1 UPDATE GLOBAL INDEXES


Note:

This feature is supported only for heap organized tables.


See Also:

Oracle9i SQL Reference for more information about the UPDATE GLOBAL INDEX clause

Figure 11-7 offers a graphical view of global partitioned indexes.

Figure 11-7 Global Partitioned Index


Text description of the illustration cncpt160.gif


Global Nonpartitioned Indexes

Global nonpartitioned indexes behave just like a nonpartitioned index. They are commonly used in OLTP environments and offer efficient access to any individual record.

Figure 11-8 offers a graphical view of global nonpartitioned indexes.

Figure 11-8 Global Nonpartitioned Index


Text description of the illustration cncpt159.gif


Partitioned Index Examples

Example of Index Creation: Starting Table Used for Examples

CREATE TABLE employees
(employee_id NUMBER(4) NOT NULL,
 last_name VARCHAR2(10), 
 department_id NUMBER(2))
PARTITION BY RANGE (department_id)
(PARTITION employees_part1 VALUES LESS THAN (11) TABLESPACE part1, 
 PARTITION employees_part2 VALUES LESS THAN (21) TABLESPACE part2, 
 PARTITION employees_part3 VALUES LESS THAN (31) TABLESPACE part3);

Example of a Local Index Creation

CREATE INDEX employees_local_idx ON employees (employee_id) LOCAL;

Example of a Global Index Creation

CREATE INDEX employees_global_idx ON employees(employee_id);

Example of a Global Partitioned Index Creation

CREATE INDEX employees_global_part_idx ON employees(employee_id)
GLOBAL PARTITION BY RANGE(employee_id)
(PARTITION p1 VALUES LESS THAN(5000),
 PARTITION p2 VALUES LESS THAN(MAXVALUE));

Example of a Partitioned Index-Organized Table Creation

CREATE TABLE sales_range
(
salesman_id   NUMBER(5), 
salesman_name VARCHAR2(30), 
sales_amount  NUMBER(10), 
sales_date    DATE, 
PRIMARY KEY(sales_date, salesman_id)) 
ORGANIZATION INDEX INCLUDING salesman_id 
OVERFLOW TABLESPACE tabsp_overflow 
PARTITION BY RANGE(sales_date)
(PARTITION sales_jan2000 VALUES LESS THAN(TO_DATE('02/01/2000','DD/MM/YYYY'))
 OVERFLOW TABLESPACE p1_overflow, 
 PARTITION sales_feb2000 VALUES LESS THAN(TO_DATE('03/01/2000','DD/MM/YYYY'))
 OVERFLOW TABLESPACE p2_overflow, 
 PARTITION sales_mar2000 VALUES LESS THAN(TO_DATE('04/01/2000','DD/MM/YYYY'))
 OVERFLOW TABLESPACE p3_overflow, 
 PARTITION sales_apr2000 VALUES LESS THAN(TO_DATE('05/01/2000','DD/MM/YYYY'))
 OVERFLOW TABLESPACE p4_overflow);

Miscellaneous Information about Creating Indexes on Partitioned Tables

You can create bitmap indexes on partitioned tables, with the restriction that the bitmap indexes must be local to the partitioned table. They cannot be global indexes.

Global indexes can be unique. Local indexes can only be unique if the partitioning key is a part of the index key.

Using Partitioned Indexes in OLTP Applications

Here are a few guidelines for OLTP applications:

  • Global indexes and unique, local indexes provide better performance than nonunique local indexes because they minimize the number of index partition probes.
  • Local indexes offer better availability when there are partition or subpartition maintenance operations on the table.

Using Partitioned Indexes in Data Warehousing and DSS Applications

Here are a few guidelines for data warehousing and DSS applications:

  • Local indexes are preferable because they are easier to manage during data loads and during partition-maintenance operations.
  • Local indexes can improve performance because many index partitions can be scanned in parallel by range queries on the index key.

Partitioned Indexes on Composite Partitions

Here are a few points to remember when using partitioned indexes on composite partitions:

  • Only range partitioned global indexes are supported.
  • Subpartitioned indexes are always local and stored with the table subpartition by default.
  • Tablespaces can be specified at either index or index subpartition levels.

Partitioning to Improve Performance

Partitioning can help you improve performance and manageability. Some topics to keep in mind when using partitioning for these reasons are:

Partition Pruning

The Oracle server explicitly recognizes partitions and subpartitions. It then optimizes SQL statements to mark the partitions or subpartitions that need to be accessed and eliminates (prunes) unnecessary partitions or subpartitions from access by those SQL statements. In other words, partition pruning is the skipping of unnecessary index and data partitions or subpartitions in a query.

For each SQL statement, depending on the selection criteria specified, unneeded partitions or subpartitions can be eliminated. For example, if a query only involves March sales data, then there is no need to retrieve data for the remaining eleven months. Such intelligent pruning can dramatically reduce the data volume, resulting in substantial improvements in query performance.

If the optimizer determines that the selection criteria used for pruning are satisfied by all the rows in the accessed partition or subpartition, it removes those criteria from the predicate list (WHERE clause) during evaluation in order to improve performance. However, the optimizer cannot prune partitions if the SQL statement applies a function to the partitioning column (with the exception of the TO_DATE function). Similarly, the optimizer cannot use an index if the SQL statement applies a function to the indexed column, unless it is a function-based index.

Pruning can eliminate index partitions even when the underlying table's partitions cannot be eliminated, but only when the index and table are partitioned on different columns. You can often improve the performance of operations on large tables by creating partitioned indexes that reduce the amount of data that your SQL statements need to access or modify.

Equality, range, LIKE, and IN-list predicates are considered for partition pruning with range or list partitioning, and equality and IN-list predicates are considered for partition pruning with hash partitioning.

Partition Pruning Example

We have a partitioned table called orders. The partition key for orders is order_date. Let's assume that orders has six months of data, January to June, with a partition for each month of data. If the following query is run:

SELECT SUM(value) 
FROM orders 
WHERE order_date BETWEEN '28-MAR-98' AND '23-APR-98'

Partition pruning is achieved by:

  • First, partition elimination of January, February, May, and June data partitions. Then either:
    • An index scan of the March and April data partition due to high index selectivity

      or

    • A full scan of the March and April data partition due to low index selectivity

Partition-wise Joins

A partition-wise join is a join optimization that you can use when joining two tables that are both partitioned along the join column(s). With partition-wise joins, the join operation is broken into smaller joins that are performed sequentially or in parallel. Another way of looking at partition-wise joins is that they minimize the amount of data exchanged among parallel slaves during the execution of parallel joins by taking into account data distribution.

See Also:

Oracle9i Data Warehousing Guide for more information about partitioning methods and partition-wise joins

Parallel DML

Parallel execution dramatically reduces response time for data-intensive operations on large databases typically associated with decision support systems and data warehouses. In addition to conventional tables, you can use parallel query and parallel DML with range- and hash-partitioned tables. By doing so, you can enhance scalability and performance for batch operations.

The semantics and restrictions for parallel DML sessions are the same whether you are using index-organized tables or not.

See Also:

Oracle9i Data Warehousing Guide for more information about parallel DML and its use with partitioned tables