일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- IPO
- 주식
- php
- JavaScript
- 7월 공모주 청약 일정
- codeigniter
- jquery
- Stock ipo
- SQL
- java
- 자바
- Oracle
- 오라클
- 공모주
- linux
- Stock
- 제이쿼리
- 자바스크립트
- 주식 청약
- css
- 주식 청약 일정
- 공모주 청약 일정
- html
- 맥
- MYSQL
- 코드이그나이터
- 6월 공모주 청약 일정
- Eclipse
- 리눅스
- 공모주 청약
- Today
- Total
개발자의 끄적끄적
[SQL] Oracle 필수 스크립트 모음(딕셔너리, SQL 문법, 성능 분석)[펌] 본문
[SQL] Oracle 필수 스크립트 모음(딕셔너리, SQL 문법, 성능 분석)[펌]
출처: http://estenpark.tistory.com/364 [전문가로가는길]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 |
--====================================================================================== --#. 01 테이블스페이스별 파일 목록을 보기 --======================================================================================
SELECT SUBSTRB(TABLESPACE_NAME, 1, 10) AS "테이블스페이스" ,SUBSTRB(FILE_NAME, 1, 50) AS "파일명" ,TO_CHAR(BLOCKS, '999,999,990') AS "블럭수" ,TO_CHAR(BYTES, '99,999,999') AS "크기" FROM DBA_DATA_FILES ORDER BY TABLESPACE_NAME, FILE_NAME;
--====================================================================================== --#. 02 테이블스페이스별 정보 보기 --======================================================================================
SELECT A.TABLESPACE_NAME AS "TABLESPACE" ,A.INITIAL_EXTENT / 1024 AS "INIT(K)" ,A.NEXT_EXTENT / 1024 AS "NEXT(K)" ,A.MIN_EXTENTS AS "MIN" ,A.MAX_EXTENTS AS "MAX" ,A.PCT_INCREASE AS "PCT_INC(%)" ,B.FILE_NAME AS "FILE_NAME" ,B.BLOCKS * C.VALUE / 1024 / 1024 AS "SIZE(M)" ,B.STATUS AS "STATUS" FROM DBA_TABLESPACES A ,DBA_DATA_FILES B ,V$PARAMETER C WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME AND C.NAME = 'db_block_size' ORDER BY 1, 2;
--====================================================================================== --#. 03 테이블스페이스별 사용하는 파일의 크기 합 보기 --======================================================================================
SELECT SUBSTRB(TABLESPACE_NAME, 1, 10) AS TABLESPACE ,TO_CHAR(SUM(BYTES), '9,999,999,999,990') AS BYTES ,TO_CHAR(SUM(BLOCKS), '9,999,999,990') AS BLOCKS FROM DBA_DATA_FILES GROUP BY TABLESPACE_NAME UNION ALL SELECT '총계', TO_CHAR(SUM(BYTES), '9,999,999,999,990') AS BYTES, TO_CHAR(SUM(BLOCKS), '9,999,999,990') AS BLOCKS FROM DBA_DATA_FILES;
--====================================================================================== --#. 04 테이블스페이스별 디스크 사용량 보기 --======================================================================================
SELECT A.TABLESPACE_NAME AS "TABLESPACE" ,A.INIT AS "INIT(K)" ,A.NEXT AS "NEXT(K)" ,A.MIN AS "MIN" ,A.MAX AS "MAX" ,A.PCT_INC AS "PCT_INC(%)" ,TO_CHAR(B.TOTAL, '999,999,999,990') AS "총량(바이트)" ,TO_CHAR(C.FREE, '999,999,999,990') AS "남은량(바이트)" ,TO_CHAR(B.BLOCKS, '9,999,990') AS "총블럭" ,TO_CHAR(D.BLOCKS, '9,999,990') AS "사용블럭" ,TO_CHAR(100 * NVL(D.BLOCKS, 0) / B.BLOCKS, '999.99') AS "사용율%" FROM (SELECT TABLESPACE_NAME ,INITIAL_EXTENT / 1024 AS INIT ,NEXT_EXTENT / 1024 AS NEXT ,MIN_EXTENTS AS MIN ,MAX_EXTENTS AS MAX ,PCT_INCREASE AS PCT_INC FROM DBA_TABLESPACES) A ,(SELECT TABLESPACE_NAME, SUM(BYTES) AS TOTAL, SUM(BLOCKS) AS BLOCKS FROM DBA_DATA_FILES GROUP BY TABLESPACE_NAME) B ,(SELECT TABLESPACE_NAME, SUM(BYTES) AS FREE FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME) C ,(SELECT TABLESPACE_NAME, SUM(BLOCKS) AS BLOCKS FROM DBA_EXTENTS GROUP BY TABLESPACE_NAME) D WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME(+) AND A.TABLESPACE_NAME = C.TABLESPACE_NAME(+) AND A.TABLESPACE_NAME = D.TABLESPACE_NAME(+) ORDER BY A.TABLESPACE_NAME;
--====================================================================================== --#. 05 테이블스페이스의 테이블 명 보기 --======================================================================================
SELECT TABLESPACE_NAME, TABLE_NAME FROM USER_TABLES WHERE TABLESPACE_NAME = UPPER('&테이블스페이스명') ORDER BY TABLESPACE_NAME, TABLE_NAME;
--====================================================================================== --#. 06 ROLLBACK SEGMENT의 사용상황 보기 --====================================================================================== --: EXTENTS = 현재 할당된 EXTENT의 수 --: EXTENDS = 마지막 트랜잭션에 의해 할당된 EXTENT의 수
SELECT SUBSTRB(A.SEGMENT_NAME, 1, 10) AS SEGMENT_NAME ,SUBSTRB(A.TABLESPACE_NAME, 1, 10) AS TABLESPACE_NAME ,TO_CHAR(A.SEGMENT_ID, '99,999') AS SEG_ID ,TO_CHAR(A.MAX_EXTENTS, '999,999') AS MAX_EXT ,TO_CHAR(B.EXTENTS, '999,999') AS EXTENTS ,TO_CHAR(B.EXTENDS, '999,999') AS EXTENDS ,TO_CHAR((A.INITIAL_EXTENT + (B.EXTENTS - 1) * A.NEXT_EXTENT) / 1000000, '9,999.999') AS "ALLOC(MB)" ,TO_CHAR(XACTS, '9,999') AS XACTS FROM DBA_ROLLBACK_SEGS A ,V$ROLLSTAT B WHERE A.SEGMENT_ID = B.USN(+) ORDER BY 1;
--====================================================================================== --#. 07 CONSTRAINT 보기 --======================================================================================
SELECT DECODE(A.CONSTRAINT_TYPE, 'P', 'Primary Key', 'R', 'Foreign Key', 'C', 'Table Check', 'V', 'View Check', 'U', 'Unique', '?') AS "유형" ,SUBSTRB(A.CONSTRAINT_NAME, 1, 25) AS CONSTRAINT_NAME ,B.POSITION ,SUBSTRB(B.COLUMN_NAME, 1, 25) AS COLUMN_NAME FROM DBA_CONSTRAINTS A ,DBA_CONS_COLUMNS B WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = 'E_LUCIS' AND A.TABLE_NAME = UPPER('&테이블명') ORDER BY 1, 2, 3;
--====================================================================================== --#. 08 INDEX 보기 --======================================================================================
SELECT A.INDEX_NAME ,A.UNIQUENESS ,TO_CHAR(COLUMN_POSITION, '999') AS POS ,SUBSTRB(COLUMN_NAME, 1, 33) AS COLUMN_NAME FROM USER_INDEXES A ,USER_IND_COLUMNS B WHERE A.INDEX_NAME = B.INDEX_NAME AND A.TABLE_OWNER = UPPER('E_LUCIS') AND A.TABLE_NAME = UPPER('&테이블명') ORDER BY 1, 3;
--====================================================================================== --#. 09 전체 INDEX 보기 --======================================================================================
SELECT SUBSTRB(A.TABLE_NAME, 1, 22) AS TABLE_NAME ,SUBSTRB(A.INDEX_NAME, 1, 23) AS INDEX_NAME ,SUBSTRB(A.UNIQUENESS, 1, 7) AS UNIQUE ,TO_CHAR(COLUMN_POSITION, '999') AS POS ,SUBSTRB(COLUMN_NAME, 1, 20) AS COLUMN_NAME FROM DBA_INDEXES A ,DBA_IND_COLUMNS B WHERE A.INDEX_NAME = B.INDEX_NAME AND A.TABLE_OWNER = B.TABLE_OWNER AND A.TABLE_OWNER = 'E_LUCIS' ORDER BY 1, 2, 3;
--====================================================================================== --#. 10 인덱스에 대한 컬럼 조회 --======================================================================================
SELECT TABLE_NAME ,INDEX_NAME ,COLUMN_POSITION ,COLUMN_NAME FROM USER_IND_COLUMNS ORDER BY TABLE_NAME, INDEX_NAME, COLUMN_POSITION;
--====================================================================================== --#. 11 테이블에 LOCK이 걸렸는지를 보기 --======================================================================================
SELECT A.SID ,A.SERIAL# ,SUBSTRB(A.USERNAME, 1, 16) AS USERNAME ,SUBSTRB(A.MACHINE, 1, 30) AS MACHINE ,A.TERMINAL ,A.OSUSER ,A.PROGRAM ,SUBSTRB(TO_CHAR(A.LOGON_TIME, 'MM/DD HH24:MI:SS'), 1, 14) AS LOGON_TIME ,SUBSTRB(C.OBJECT_NAME, 1, 58) AS OBJECT_NAME FROM V$SESSION A ,V$LOCK B ,DBA_OBJECTS C WHERE A.SID = B.SID AND B.ID1 = C.OBJECT_ID AND B.TYPE = 'TM' AND C.OBJECT_NAME LIKE UPPER('&테이블명');
--====================================================================================== --#. 12 Lock을 잡고있는 세션과 기다리는 세션 조회 --======================================================================================
SELECT DECODE(B.LOCKWAIT, NULL, ' ', 'w') AS WW ,B.SID ,B.SERIAL# AS SER# ,SUBSTR(B.MACHINE, 1, 10) AS MACHINE ,SUBSTR(B.PROGRAM, 1, 15) AS PROGRAM ,SUBSTR(A.OBJECT_NAME, 1, 17) AS OBJ_NAME ,SUBSTR(B.STATUS, 1, 1) AS S ,DECODE(B.COMMAND, 0, NULL, 2, 'INSERT', 6, 'UPDATE', 7, 'DELETE', B.COMMAND) AS SQLCMD ,B.PROCESS AS PGM_PSS FROM V$SESSION B ,(SELECT A.SID, DECODE(B.OWNER, NULL, A.TYPE || '..ing', B.OWNER || '.' || B.OBJECT_NAME) AS OBJECT_NAME FROM V$LOCK A ,DBA_OBJECTS B WHERE A.ID1 = B.OBJECT_ID(+) GROUP BY A.SID, DECODE(B.OWNER, NULL, A.TYPE || '..ing', B.OWNER || '.' || B.OBJECT_NAME)) A WHERE B.SID = A.SID AND B.TADDR IS NOT NULL;
--====================================================================================== --#. 13 테이블에 걸린 비정상적 LOCK 풀기 --====================================================================================== ALTER SYSTEM KILL SESSION '&SID,&SERIAL';
--====================================================================================== --#. 14 연결되어 있는 OS 사용자 및 프로그램 조회 --======================================================================================
SELECT SID ,SERIAL# ,OSUSER ,SUBSTRB(USERNAME, 1, 10) AS USER_NAME ,SUBSTRB(PROGRAM, 1, 30) AS PROGRAM_NAME ,STATUS ,TO_CHAR(LOGON_TIME, 'YYYY/MM/DD HH:MI') AS LOGON_TIME FROM V$SESSION WHERE TYPE != ‘BACKGROUND’ AND STATUS = ‘ACTIVE’;
--====================================================================================== --#. 15 위치별 space를 아는 방법 --======================================================================================
SELECT SUBSTRB(A.FILE_NAME, 1, 40) AS FILE_NAME ,A.FILE_ID ,B.FREE_BYTES / 1024 AS FREE_BYTES ,B.MAX_BYTES / 1024 AS MAX_BYTES FROM DBA_DATA_FILES A ,(SELECT FILE_ID, SUM(BYTES) AS FREE_BYTES, MAX(BYTES) AS MAX_BYTES FROM DBA_FREE_SPACE GROUP BY FILE_ID) B WHERE A.FILE_ID = B.FILE_ID AND A.TABLESPACE_NAME = UPPER('&테이블스페이스명') ORDER BY A.FILE_NAME;
--====================================================================================== --#. 16 DB Link 보기 --======================================================================================
SELECT SUBSTRB(U.NAME, 1, 10) AS OWNER ,SUBSTRB(L.NAME, 1, 20) AS DB_LINK ,SUBSTRB(L.HOST, 1, 10) AS HOST ,SUBSTRB(L.USERID || '/' || L.PASSWORD, 1, 15) AS USERPASS FROM SYS.LINK$ L ,SYS.USER$ U WHERE L.OWNER# = U.USER#;
--====================================================================================== --#. 17 테이블 생성일자 보기 --======================================================================================
SELECT SUBSTRB(OBJECT_NAME, 1, 15) AS OBJECT_NAME ,CREATED ,LAST_DDL_TIME ,TIMESTAMP ,STATUS FROM USER_OBJECTS WHERE OBJECT_NAME = UPPER('&테이블명') AND OBJECT_TYPE = 'TABLE';
--====================================================================================== --#. 18 테이블의 크기 및 블록 보기 --======================================================================================
SELECT SUBSTR(SEGMENT_NAME, 1, 20), BYTES, BLOCKS FROM USER_SEGMENTS WHERE SEGMENT_NAME = UPPER('&테이블명');
--====================================================================================== --#. 19 View의 정의 내역 보기 --====================================================================================== SET LONG 100000
SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME LIKE UPPER('&뷰_이름');
--====================================================================================== --#. 20 파티션 테이블의 파티션 범위 보기 --======================================================================================
SELECT SUBSTRB(PARTITION_NAME, 1, 30) AS PARTITION_NAME, SUBSTRB(TABLESPACE_NAME, 1, 30) AS TABLESPACE_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS WHERE TABLE_NAME = UPPER('&테이블명');
--====================================================================================== --#. 21 PRIMARY KEY 재생성 방법 --====================================================================================== --- PRIMARY KEY DROP
ALTER TABLE EMP DROP PRIMARY KEY;
-- PRIMARY KEY 생성 ALTER TABLE EMP ADD CONSTRAINT EMP_PK PRIMARY KEY(EMPNO) USING INDEX STORAGE(INITIAL 1M NEXT 1M PCTINCREASE 0) TABLESPACE USERS;
--====================================================================================== --#. 22 PRIMARY KEY를 REFERENCE 하는 FOREIGN KEY 찾기 --======================================================================================
SELECT C.NAME CONSTRAINT_NAME FROM DBA_OBJECTS A ,CDEF$ B ,CON$ C WHERE A.OBJECT_NAME = UPPER('&테이블명') AND A.OBJECT_ID = B.ROBJ# AND B.CON# = C.CON#;
--====================================================================================== --#. 23 동일한 자료 삭제 방법 --====================================================================================== DELETE FROM EMP E WHERE E.ROWID > ( SELECT MIN(X.ROWID) FROM EMP X WHERE X.EMPNO = E.EMPNO );
--====================================================================================== --#. 24 1시간 이상 유휴 상태인 세션 --======================================================================================
SELECT SID ,SERIAL# ,USERNAME ,TRUNC(LAST_CALL_ET / 3600, 2) || ' HR' LAST_CALL_ET FROM V$SESSION WHERE LAST_CALL_ET > 3600 AND USERNAME IS NOT NULL;
--====================================================================================== --#. 25 Oracle Process의 정보 --======================================================================================
SELECT S.STATUS "STATUS" ,S.SERIAL# "SERIAL#" ,S.TYPE "TYPE" ,S.USERNAME "DB USER" ,S.OSUSER "CLIENT USER" ,S.SERVER "SERVER" ,S.MACHINE "MACHINE" ,S.MODULE "MODULE" ,S.TERMINAL "TERMINAL" ,S.PROGRAM "PROGRAM" ,P.PROGRAM "O.S. PROGRAM" ,S.LOGON_TIME "CONNECT TIME" ,LOCKWAIT "LOCK WAIT" ,SI.PHYSICAL_READS "PHYSICAL READS" ,SI.BLOCK_GETS "BLOCK GETS" ,SI.CONSISTENT_GETS "CONSISTENT GETS" ,SI.BLOCK_CHANGES "BLOCK CHANGES" ,SI.CONSISTENT_CHANGES "CONSISTENT CHANGES" ,S.PROCESS "PROCESS" ,P.SPID ,P.PID ,S.SERIAL# ,SI.SID ,S.SQL_ADDRESS "ADDRESS" ,S.SQL_HASH_VALUE "SQL HASH" ,S.ACTION FROM V$SESSION S ,V$PROCESS P ,SYS.V_$SESS_IO SI WHERE S.PADDR = P.ADDR(+) AND SI.SID(+) = S.SID AND S.USERNAME IS NOT NULL AND NVL(S.OSUSER, 'X') <> 'SYSTEM' AND S.TYPE <> 'BACKGROUND' ORDER BY 3;
--====================================================================================== --#. 26 중복인덱스 체크 --======================================================================================
SELECT O1.NAME || '.' || N1.NAME REDUNDANT_INDEX, O2.NAME || '.' || N2.NAME SUFFICIENT_INDEX FROM SYS.ICOL$ IC1 ,SYS.ICOL$ IC2 ,SYS.IND$ I1 ,SYS.OBJ$ N1 ,SYS.OBJ$ N2 ,SYS.USER$ O1 ,SYS.USER$ O2 WHERE IC1.POS# = 1 AND IC2.BO# = IC1.BO# AND IC2.OBJ# != IC1.OBJ# AND IC2.POS# = 1 AND IC2.INTCOL# = IC1.INTCOL# AND I1.OBJ# = IC1.OBJ# AND BITAND(I1.PROPERTY, 1) = 0 AND (SELECT MAX(POS#) * (MAX(POS#) + 1) / 2 FROM SYS.ICOL$ WHERE OBJ# = IC1.OBJ#) = (SELECT SUM(XC1.POS#) FROM SYS.ICOL$ XC1 ,SYS.ICOL$ XC2 WHERE XC1.OBJ# = IC1.OBJ# AND XC2.OBJ# = IC2.OBJ# AND XC1.POS# = XC2.POS# AND XC1.INTCOL# = XC2.INTCOL#) AND N1.OBJ# = IC1.OBJ# AND N2.OBJ# = IC2.OBJ# AND O1.USER# = N1.OWNER# AND O2.USER# = N2.OWNER#;
--====================================================================================== --#. 27 공간의 90% 이상을 사용하고 있는 Tablespace --======================================================================================
SELECT X.TABLESPACE_NAME ,TOTAL_SIZE / 1024 / 1024 TOTAL_SIZE ,USED_SIZE / 1024 / 1024 USED_SIZE ,(ROUND(USED_SIZE / TOTAL_SIZE, 2)) * 100 USED_RATIO FROM (SELECT TABLESPACE_NAME, SUM(BYTES) TOTAL_SIZE FROM DBA_DATA_FILES GROUP BY TABLESPACE_NAME) X ,(SELECT TABLESPACE_NAME, SUM(BYTES) USED_SIZE FROM DBA_EXTENTS GROUP BY TABLESPACE_NAME) Y WHERE X.TABLESPACE_NAME = Y.TABLESPACE_NAME(+) AND Y.USED_SIZE > .9 * X.TOTAL_SIZE;
--====================================================================================== --#. 28 현재 Extension 횟수가 MaxExtents의 80% 이상인 경우 --======================================================================================
SELECT TABLESPACE_NAME ,OWNER ,SEGMENT_NAME ,SEGMENT_TYPE ,EXTENTS ,MAX_EXTENTS FROM SYS.DBA_SEGMENTS S WHERE EXTENTS / MAX_EXTENTS > .8 AND MAX_EXTENTS > 0 ORDER BY TABLESPACE_NAME, OWNER, SEGMENT_NAME;
--====================================================================================== --#. 29 Active Session 중 Idle Time이 긴 작업 --======================================================================================
SELECT VS.SID || ',' || VS.SERIAL# " SID" ,VP.SPID ,VS.MACHINE ,VS.PROGRAM ,VS.MODULE ,VS.STATUS ,TO_CHAR(VS.LOGON_TIME, 'MM/DD HH24:MI') LOGIN_TIME ,ROUND(VS.LAST_CALL_ET / 60) "IDLE" FROM V$SESSION VS ,V$PROCESS VP WHERE VS.STATUS = 'ACTIVE' AND VS.SID NOT IN (1, 2, 3, 4, 5, 6, 7) AND VS.PADDR = VP.ADDR ORDER BY 8;
--====================================================================================== --#. 30 DBUser 별로 Session 정보를 조회 --======================================================================================
SELECT S.USERNAME ,S.SID ,S.SERIAL# ,P.SPID ,S.OSUSER ,S.MACHINE ,S.PROGRAM ,TO_CHAR(S.LOGON_TIME, 'MM/DD HH24:MI') "LOGON_TIME" ,ROUND(S.LAST_CALL_ET / 60) "IDLE" FROM V$SESSION S ,V$PROCESS P WHERE S.PADDR = P.ADDR AND S.USERNAME LIKE UPPER('&DBUSER%') ORDER BY 9;
--====================================================================================== --#. 31 사용자 session 중에서 2시간 이상 idle 상태가 지속되는 session을 kill --====================================================================================== SET PAGESIZE 0 SPOOL KILLIDLE3.SQL
SELECT DISTINCT '!KILL -9 ' || B.SPID, 'ALTER SYSTEM KILL SESSION ''' || A.SID || ',' || A.SERIAL# || ''' ;' FROM V$SESSION A ,V$PROCESS B WHERE A.PADDR IN (SELECT S.PADDR FROM V$SESSION S WHERE STATUS = 'INACTIVE' GROUP BY S.PADDR HAVING MIN(ROUND(LAST_CALL_ET / 60)) > 120) AND A.PADDR = B.ADDR AND A.STATUS = 'INACTIVE';
SPOOL OFF
--====================================================================================== --#. 32 사용자별 오브젝트 수 --======================================================================================
SELECT OWNER AS "OWNER" ,SUM(DECODE(OBJECT_TYPE, 'TABLE', 1, 0)) AS "TABLE" ,SUM(DECODE(OBJECT_TYPE, 'INDEX', 1, 0)) AS "INDEX" ,SUM(DECODE(OBJECT_TYPE, 'SYNONYM', 1, 0)) AS "SYNONYMS" ,SUM(DECODE(OBJECT_TYPE, 'SEQUENCE', 1, 0)) AS "SEQUENCES" ,SUM(DECODE(OBJECT_TYPE, 'VIEW', 1, 0)) AS "VIEWS" ,SUM(DECODE(OBJECT_TYPE, 'CLUSTER', 1, 0)) AS "CLUSTERS" ,SUM(DECODE(OBJECT_TYPE, 'DATABASE LINK', 1, 0)) AS "DBLINKS" ,SUM(DECODE(OBJECT_TYPE, 'PACKAGE', 1, 0)) AS "PACKAGES" ,SUM(DECODE(OBJECT_TYPE, 'PACKAGE BODY', 1, 0)) AS "PACKAGE_BODY" ,SUM(DECODE(OBJECT_TYPE, 'PROCEDURE', 1, 0)) AS "PROCEDURES" ,SUM(DECODE(OBJECT_TYPE, 'FUNCTION', 1, 0)) AS "FUNCTION" FROM DBA_OBJECTS GROUP BY OWNER;
--====================================================================================== --#. 33 Object별 테이블스페이스 및 데이터파일 --======================================================================================
SELECT DISTINCT E.SEGMENT_NAME, E.TABLESPACE_NAME, F.FILE_NAME FROM DBA_EXTENTS E ,DBA_DATA_FILES F WHERE E.FILE_ID = F.FILE_ID AND E.SEGMENT_TYPE = 'TABLE' AND E.TABLESPACE_NAME NOT IN ('SYSTEM', 'TOOLS');
--====================================================================================== --#. 34 작업 중인 데이터베이스 트랜잭션 조회 --======================================================================================
SELECT S.SID ,S.SERIAL# ,S.STATUS ,S.OSUSER ,S.USERNAME ,T.STATUS ,T.START_TIME FROM V$SESSION S ,V$TRANSACTION T ,DBA_ROLLBACK_SEGS R WHERE S.TADDR = T.ADDR AND T.XIDUSN = R.SEGMENT_ID;
--====================================================================================== --#. 35 열려 있는 커서 조회 --======================================================================================
SELECT A.SID ,A.OSUSER ,COUNT(B.SID) AS "CURSOR" ,A.PROGRAM ,A.STATUS FROM V$SESSION A ,V$OPEN_CURSOR B WHERE A.SID = B.SID(+) GROUP BY A.SID, A.OSUSER, A.PROGRAM, A.STATUS;
--====================================================================================== --#. 36 잠금 발생 유형 조회 --======================================================================================
SELECT A.SID ,DECODE(A.TYPE ,'MR', 'MEDIA RECOVERY' ,'RT', 'REDO THREAD' ,'UN', 'USER_NAME' ,'TX', 'TRANSACTION' ,'TM', 'DML' ,'UL', 'PL/SQL USER LOCK' ,'DX', 'DISTRIBUTED XACTION' ,'CF', 'CONTROL FILE' ,'IS', 'INSTANCE STATE' ,'FS', 'FILE SET' ,'IR', 'INSTANCE RECOVERY' ,'FS', 'FILE SET' ,'ST', 'DISK SPACE TRANSACTION' ,'TS', 'TEMP SEGMENT' ,'IV', 'LIBRARY CACHE INVAILDATION' ,'LS', 'LOG START OR SWITCH' ,'RW', 'ROW WAIT' ,'SQ', 'SEQUENCE NUMBER' ,'TE', 'EXTEND TABLE' ,'TT', 'TEMP TABLE' ,A.TYPE ) AS "LOCK_TYPE" ,DECODE(A.LMODE ,0, 'NONE' ,1, 'NULL' ,2, 'ROW-S(SS)' ,3, 'ROW-X(SX)' ,4, 'SHARE' ,5, 'S/ROW-X(SSX)' ,6, 'EXCLUSIVE' ,TO_CHAR(A.LMODE) ) AS "MODE_HELD" ,DECODE(A.REQUEST ,0, 'NONE' ,1, 'NULL' ,2, 'ROW-S(SS)' ,3, 'ROW-X(SX)' ,4, 'SHARE' ,5, 'S/ROW-X(SSX)' ,6, 'EXCLUSIVE' ,TO_CHAR(A.REQUEST) ) AS "MODE_REQUESTED" ,TO_CHAR(A.ID1) AS "LOCK_ID1" ,TO_CHAR(A.ID2) AS "LOCK_ID2" ,DECODE(BLOCK, 0, 'NOT BLOCKING', 1, 'BLOCKING', 2, 'GLOBAL', TO_CHAR(BLOCK)) AS "BLOCKING_OTHERS" FROM V$LOCK A WHERE (ID1, ID2) IN (SELECT B.ID1, ID2 FROM V$LOCK B WHERE B.ID1 = A.ID1);
--====================================================================================== --#. 37 테이블의 PK를 구성하는 컬럼 조회 --======================================================================================
SELECT A.TABLE_NAME, B.CONSTRAINT_NAME, C.COLUMN_NAME FROM USER_TABLES A ,USER_CONSTRAINTS B ,USER_CONS_COLUMNS C WHERE A.TABLE_NAME = B.TABLE_NAME AND B.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE = 'P';
--====================================================================================== --#. 38 오브젝트에 접속되어 있는 프로그램 조회 --======================================================================================
SELECT SUBSTR(B.OBJECT, 1, 15) AS OBJECT, SUBSTR(A.PROGRAM, 1, 15) AS PROGRAM, COUNT(*) AS CNT FROM V$SESSION A ,V$ACCESS B WHERE A.SID = B.SID AND B.OWNER NOT IN ('SYS') AND A.TYPE != 'BACKGROUND' AND B.OBJECT LIKE UPPER('&OBJECT_NAME') || '%' GROUP BY B.OBJECT, SUBSTR(A.PROGRAM, 1, 15);
--====================================================================================== --#. 39 잠금 상태 오브젝트 조회 --======================================================================================
SELECT A.SESSION_ID ,B.SERIAL# ,A.OS_USER_NAME ,A.ORACLE_USERNAME ,C.OBJECT_NAME ,A.LOCKED_MODE ,A.XIDUSN FROM V$LOCKED_OBJECT A ,V$SESSION B ,DBA_OBJECTS C WHERE A.OBJECT_ID = C.OBJECT_ID AND A.SESSION_ID = B.SID;
--====================================================================================== --#. 40 잠금 SQL 구문 조회 --======================================================================================
SELECT B.USERNAME AS USERNAME ,C.SID AS SID ,C.OWNER AS OBJECT_OWNER ,C.OBJECT AS OBJECT ,B.LOCKWAIT ,A.PIECE ,A.SQL_TEXT AS SQL FROM V$SQLTEXT A ,V$SESSION B ,V$ACCESS C WHERE A.ADDRESS = B.SQL_ADDRESS AND A.HASH_VALUE = B.SQL_HASH_VALUE AND B.SID = C.SID AND C.OWNER != 'SYS';
--====================================================================================== --#. 41 롤백 세그먼트 경합 조회 --======================================================================================
SELECT NAME T0 ,GETS T1 ,WAITS T2 ,TO_CHAR(TRUNC(WAITS / GETS * 100, 2), 099.99) || ' %' T3 ,TO_CHAR(ROUND(RSSIZE / 1024)) T4 ,SHRINKS T5 ,EXTENDS T6 FROM V$ROLLSTAT ,V$ROLLNAME WHERE V$ROLLSTAT.USN = V$ROLLNAME.USN;
--====================================================================================== --#. 42 CPU를 많이 사용하는 세션의 식별 --======================================================================================
SELECT A.SID ,C.SERIAL# ,A.VALUE ,C.USERNAME ,C.STATUS ,C.PROGRAM FROM V$SESSTAT A ,V$STATNAME B ,V$SESSION C WHERE A.STATISTIC# = B.STATISTIC# AND A.SID = C.SID AND B.NAME = 'CPU used by this session' AND A.VALUE > 0 ORDER BY A.VALUE DESC;
--====================================================================================== --#. 43 Tablespace별 Table, Index 개수 --======================================================================================
SELECT OWNER ,TABLESPACE_NAME ,SUM(DECODE(SEGMENT_TYPE, 'TABLE', 1, 0)) ,SUM(DECODE(SEGMENT_TYPE, 'INDEX', 1, 0)) FROM DBA_SEGMENTS WHERE SEGMENT_TYPE IN ('TABLE', 'INDEX') GROUP BY OWNER, TABLESPACE_NAME;
--====================================================================================== --#. 44 Disk Read 가 많은 SQL문 찾기 --======================================================================================
SELECT DISK_READS, SQL_TEXT FROM V$SQLAREA WHERE DISK_READS > 100 ORDER BY DISK_READS DESC;
--====================================================================================== --#. 45 Rollback Segment를 사용하고 있는 SQL문 조회 --======================================================================================
SELECT A.NAME ,B.XACTS ,C.SID ,C.SERIAL# ,C.USERNAME ,D.SQL_TEXT FROM V$ROLLNAME A ,V$ROLLSTAT B ,V$SESSION C ,V$SQLTEXT D ,V$TRANSACTION E WHERE A.USN = B.USN AND B.USN = E.XIDUSN AND C.TADDR = E.ADDR AND C.SQL_ADDRESS = D.ADDRESS AND C.SQL_HASH_VALUE = D.HASH_VALUE ORDER BY A.NAME, C.SID, D.PIECE;
--====================================================================================== --#. 46 Index가 없는 Table 조회 --======================================================================================
SELECT OWNER, TABLE_NAME FROM (SELECT OWNER, TABLE_NAME FROM DBA_TABLES MINUS SELECT TABLE_OWNER, TABLE_NAME FROM DBA_INDEXES) WHERE OWNER NOT IN ('SYS', 'SYSTEM') ORDER BY OWNER, TABLE_NAME;
--====================================================================================== --#. 47 오래도록 수행되는 Full Table Scan를 모니터링 --======================================================================================
SELECT SID ,SERIAL# ,OPNAME ,TO_CHAR(START_TIME, 'HH24:MI:SS') AS "START" ,(SOFAR / TOTALWORK) * 100 AS "PERCENT_COMPLETE" FROM V$SESSION_LONGOPS;
--====================================================================================== --#. 48 System 테이블스페이스에 비시스템 세그먼트 조회 --======================================================================================
SELECT OWNER ,SEGMENT_NAME ,SEGMENT_TYPE ,TABLESPACE_NAME FROM DBA_SEGMENTS WHERE OWNER NOT IN ('SYS', 'SYSTEM') AND TABLESPACE_NAME = 'SYSTEM';
--====================================================================================== --#. 49 인덱스의 Delete Space 조회 --======================================================================================
SELECT NAME ,LF_ROWS ,DEL_LF_ROWS ,(DEL_LF_ROWS / LF_ROWS) * 100 AS "DELETE SPACE %" FROM INDEX_STATS WHERE NAME = UPPER('&INDEX_NAME');
--Delete Space % 값이 20 % 가 넘으면, 그 인덱스는 다시 작성하는 것이 좋다.
--====================================================================================== --#. 50 Session별 사용 명령어 --======================================================================================
SELECT SESS.SID ,SESS.SERIAL# ,SUBSTR(SESS.USERNAME, 1, 10) "USER NAME" ,SUBSTR(OSUSER, 1, 11) "OS USER" ,SUBSTR(SESS.MACHINE, 1, 15) "MACHINE NAME" ,STATUS ,UPPER( DECODE(NVL(COMMAND, 0) ,0, '---' ,1, 'CREATE TABLE' ,2, 'INSERT -' ,3, 'SELECT -' ,4, 'CREATE CLUST' ,5, 'ALTER CLUST' ,6, 'UPDATE -' ,7, 'DELETE -' ,8, 'DROP -' ,9, 'CREATE INDEX' ,10, 'DROP INDEX' ,11, 'ALTER INDEX' ,12, 'DROP TABLE' ,13, 'CREATE SEQ' ,14, 'ALTER SEQ' ,15, 'ALTER TABLE' ,16, 'DROP SEQ' ,17, 'GRANT' ,18, 'REVOKE' ,19, 'CREATE SYN' ,20, 'DROP SYN' ,21, 'CREATE VIEW' ,22, 'DROP VIEW' ,23, 'VALIDATE IX' ,24, 'CREATE PROC' ,25, 'ALTER PROC' ,26, 'LOCK TABLE' ,27, 'NO OPERATION' ,28, 'RENAME' ,29, 'COMMENT' ,30, 'AUDIT' ,31, 'NOAUDIT' ,32, 'CREATE DBLINK' ,33, 'DROP DB LINK' ,34, 'CREATE DATABASE' ,35, 'ALTER DATABASE' ,36, 'CREATE RBS' ,37, 'ALTER RBS' ,38, 'DROP RBS' ,39, 'CREATE TABLESPACE' ,40, 'ALTER TABLESPACE' ,41, 'DROP TABLESPACE' ,42, 'ALTER SESSION' ,43, 'ALTER USER' ,44, 'COMMIT' ,45, 'ROLLBACK' ,47, 'PL/SQL EXEC' ,48, 'SET TRANSACTION' ,49, 'SWITCH LOG' ,50, 'EXPLAIN' ,51, 'CREATE USER' ,52, 'CREATE ROLE' ,53, 'DROP USER' ,54, 'DROP ROLE' ,55, 'SET ROLE' ,56, 'CREATE SCHEMA' ,58, 'ALTER TRACING' ,59, 'CREATE TRIGGER' ,61, 'DROP TRIGGER' ,62, 'ANALYZE TABLE' ,63, 'ANALYZE INDEX' ,69, 'DROP PROCEDURE' ,71, 'CREATE SNAP LOG' ,72, 'ALTER SNAP LOG' ,73, 'DROP SNAP LOG' ,74, 'CREATE SNAPSHOT' ,75, 'ALTER SNAPSHOT' ,76, 'DROP SNAPSHOT' ,85, 'TRUNCATE TABLE' ,88, 'ALTER VIEW' ,91, 'CREATE FUNCTION' ,92, 'ALTER FUNCTION' ,93, 'DROP FUNCTION' ,94, 'CREATE PACKAGE' ,95, 'ALTER PACKAGE' ,96, 'DROP PACKAGE' ,46, 'SAVEPOINT' ) ) COMMAND ,SESS.PROCESS "C.PROC" ,PROC.SPID "S.PROC" ,TO_CHAR(SESS.LOGON_TIME, 'YYYY-MM-DD HH24:MI') FROM V$SESSION SESS ,V$SESSTAT STAT ,V$STATNAME NAME ,V$PROCESS PROC WHERE SESS.SID = STAT.SID AND STAT.STATISTIC# = NAME.STATISTIC# AND SESS.USERNAME IS NOT NULL AND NAME.NAME = 'RECURSIVE CALLS' AND SESS.PADDR = PROC.ADDR ORDER BY 3, 1, 2;
--====================================================================================== --#. 51 딕셔너리/뷰 정보 조회 --======================================================================================
SELECT A.TABLE_NAME, B.COLUMN_NAME FROM DICTIONARY A ,DICT_COLUMNS B WHERE A.TABLE_NAME = B.TABLE_NAME;
--====================================================================================== --#. 52 패키지 검색 1 - 특정 오라클 사용자 중에서 패키지 소스와 일치하는 텍스트를 조회 --====================================================================================== -- 오라클 사용자에서 사용하는 패키지를 보여는 방법 ---- :IN_OWNER : 오라클 사용자 ---- :IN_OBJECT_NAME : 패키지 이름
SELECT OBJECT_NAME FROM DBA_OBJECTS WHERE OWNER = :IN_OWNER AND OBJECT_NAME LIKE '%' || :IN_OBJECT_NAME || '%' AND OBJECT_TYPE = 'PACKAGE';
-- 오라클 사용자의 패키지 중에서 텍스트 내용을 검색하여 패키지 정보를 추출 ---- :IN_OWNER : 오라클 사용자 ---- :IN_TEXT : 패키지소 스에서 검색할 텍스트
SELECT NAME -- 패키지 이름 , LINE -- 라인 수 , TEXT -- 패키지 BODY에 수정된 내용 FROM DBA_SOURCE WHERE OWNER = :IN_OWNER AND TEXT LIKE '%' || :IN_TEXT || '%';
--====================================================================================== --#. 53 특정 사용자의 패키지 내에서 주석처리가 되지 않은 항목을 조회 --======================================================================================
/* 오라클 사용자가 사용하는 패키지의 BODY 소스를 검색하여 주식이 없거나 패턴이 맞지 않는 항목을 조회 한다. */
SELECT * FROM DBA_OBJECTS B WHERE B.OWNER = :IN_OWNER AND B.OBJECT_TYPE = 'PACKAGE BODY' AND B.STATUS <> 'INVALID' -- VALID 상태만 조회, 만약 INVALID 된다고 해도 패키지를 수행하는 순간 컴파일 됨. AND NOT EXISTS (SELECT 1 FROM DBA_SOURCE A WHERE A.OWNER = B.OWNER AND A.TYPE = B.OBJECT_TYPE AND A.NAME = B.OBJECT_NAME AND A.LINE <= 5 AND A.TEXT LIKE '%NAME%');
--====================================================================================== --#. 54 테이블의 익스텐트 정보 조회 --======================================================================================
/* 오라클에서 스토리지 구조는 아래와 같다. 테이블 스페이스 -> 세그먼트 -> 익스텐트 -> 블록 -> OS 범위 -> 데이터 파일 -> 운영체제 블록
세그먼트의 이름, 해당 세그먼트의 최대 익스텐트 개수, 익스텐트 아이디 해당 세그먼트의 최대 익스텐트 개수 - 최대 익스텐트 아이디
딕셔너리 관리 테이블스페이스로 생성한 것으로 조회를 한다.
*/
SELECT B.SEGMENT_NAME ,B.MAX_EXTENTS ,MAX(C.EXTENT_ID) AS EXTENT_ID ,B.MAX_EXTENTS - MAX(C.EXTENT_ID) AS DIFF FROM USER_TABLESPACES A ,USER_SEGMENTS B ,USER_EXTENTS C WHERE A.EXTENT_MANAGEMENT = 'DICTIONARY' AND B.TABLESPACE_NAME = A.TABLESPACE_NAME AND C.SEGMENT_NAME = B.SEGMENT_NAME GROUP BY B.SEGMENT_NAME, B.MAX_EXTENTS HAVING B.MAX_EXTENTS - MAX(C.EXTENT_ID) <= 50 ORDER BY B.MAX_EXTENTS - MAX(C.EXTENT_ID);
--====================================================================================== --#. 55 특정 테이블의 스키마 구조 확인 --======================================================================================
/* 보통 토드나 기타 오라클 클라이언트 툴을 이용해서 테이블 구조를 확인 해도 됩니다. 하지만 수많은 테이블을 전체 보고 싶을 경우 아래 쿼리를 이용하면 한번에 확인이 가능합니다. */
--: 관리자용 SELECT OWNER ,TABLE_NAME ,COLUMN_NAME ,PK ,COLUMN_NAME ,DATA_TYPE || '( ' || NVL(DATA_TYPE_2, DATA_LENGTH) || ' )' DATA_TYPE ,NULLABLE ,COMMENTS FROM (SELECT A.OWNER ,A.TABLE_NAME ,A.COLUMN_ID ,B.POSITION PK ,A.COLUMN_NAME ,A.DATA_TYPE ,A.DATA_PRECISION || DECODE(A.DATA_SCALE, NULL, NULL, ',' || A.DATA_SCALE) DATA_TYPE_2 ,A.DATA_LENGTH ,A.DATA_PRECISION ,A.DATA_SCALE ,A.NULLABLE ,A.COMMENTS ,ROW_NUMBER() OVER (PARTITION BY A.OWNER, A.TABLE_NAME, A.COLUMN_ID ORDER BY A.COLUMN_ID, B.POSITION) RN FROM (SELECT COL.OWNER ,COL.TABLE_NAME ,COL.COLUMN_ID ,COL.COLUMN_NAME ,COL.DATA_TYPE ,COL.DATA_LENGTH ,COL.DATA_PRECISION ,COL.DATA_SCALE ,COL.NULLABLE ,COM.COMMENTS FROM DBA_TAB_COLUMNS COL ,DBA_COL_COMMENTS COM WHERE COL.COLUMN_NAME = COM.COLUMN_NAME AND COL.OWNER = COM.OWNER AND COL.TABLE_NAME = COM.TABLE_NAME AND COM.OWNER = :IN_OWNER AND COM.TABLE_NAME LIKE :IN_TABLE_NAME || '%') A ,DBA_CONS_COLUMNS B WHERE B.TABLE_NAME(+) = A.TABLE_NAME AND B.COLUMN_NAME(+) = A.COLUMN_NAME) X WHERE X.RN = 1 ORDER BY X.TABLE_NAME, X.COLUMN_ID;
--: 일반 사용자 용
SELECT TABLE_NAME ,COLUMN_NAME ,PK ,COLUMN_NAME ,DATA_TYPE || '( ' || NVL(DATA_TYPE_2, DATA_LENGTH) || ' )' DATA_TYPE ,NULLABLE ,COMMENTS FROM (SELECT A.TABLE_NAME ,A.COLUMN_ID ,B.POSITION PK ,A.COLUMN_NAME ,A.DATA_TYPE ,A.DATA_PRECISION || DECODE(A.DATA_SCALE, NULL, NULL, ',' || A.DATA_SCALE) DATA_TYPE_2 ,A.DATA_LENGTH ,A.DATA_PRECISION ,A.DATA_SCALE ,A.NULLABLE ,A.COMMENTS ,ROW_NUMBER() OVER (PARTITION BY A.TABLE_NAME, A.COLUMN_ID ORDER BY A.COLUMN_ID, B.POSITION) RN FROM (SELECT COL.TABLE_NAME ,COL.COLUMN_ID ,COL.COLUMN_NAME ,COL.DATA_TYPE ,COL.DATA_LENGTH ,COL.DATA_PRECISION ,COL.DATA_SCALE ,COL.NULLABLE ,COM.COMMENTS FROM USER_TAB_COLUMNS COL ,USER_COL_COMMENTS COM WHERE COL.COLUMN_NAME = COM.COLUMN_NAME AND COL.TABLE_NAME = COM.TABLE_NAME AND COM.TABLE_NAME LIKE :IN_TABLE_NAME || '%') A ,USER_CONS_COLUMNS B WHERE B.TABLE_NAME(+) = A.TABLE_NAME AND B.COLUMN_NAME(+) = A.COLUMN_NAME) X WHERE X.RN = 1 ORDER BY X.TABLE_NAME, X.COLUMN_ID;
--====================================================================================== --#. 56 특정 테이블의 인덱스 확인 --======================================================================================
/* 인덱스를 확인 하고자 할때 사용하는 쿼리 */
SELECT C.TABLE_NAME ,C.INDEX_NAME ,C.COLUMN_NAME ,C.COLUMN_POSITION ,T.NUM_ROWS FROM ALL_IND_COLUMNS C ,(SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES WHERE OWNER = 'ESTDBA' AND TABLE_NAME IN (SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME LIKE :IN_TABLE_NAME || '%') AND NUM_ROWS > 0) T WHERE C.TABLE_NAME = T.TABLE_NAME ORDER BY T.NUM_ROWS DESC, C.TABLE_NAME, C.INDEX_NAME, C.COLUMN_POSITION;
--====================================================================================== --#. 57 다중 UPDATE 쿼리 --======================================================================================
/* BYPASS_UJVC 힌트를 이용하면 특정 뷰를 만들어서 컬럼 대 컬럼을 대입 할 수 있습니다.
아래 쿼리는 메타정보관리용 유저로서 코멘트의 내용 중에서 정규표현식을 이용하여 관련 데이터를 조작 하는 방법 입니다. */
UPDATE /*+ BYPASS_UJVC */ ( SELECT X.* ,CASE WHEN UNIT_INSTR > 0 THEN SUBSTR(X.COLUMN_DESC, X.UNIT_INSTR + 3, 1) END B_UNIT FROM (SELECT A.SERVER ,A.OWNER ,A.TABLE_NAME ,A.COLUMN_NAME ,A.COLUMN_DESC ,A.GRP_CD A_GRP_CD ,A.UNIT A_UNIT ,REGEXP_INSTR(COLUMN_DESC, '단위\:') UNIT_INSTR FROM DBA_MYCOL A WHERE A.SERVER = :IN_SERVER AND A.OWNER = :IN_OWNER AND A.TABLE_NAME = :IN_TABLE_NAME) X) SET A_UNIT = B_UNIT UPDATE_DT = SYSDATE;
--====================================================================================== --#. 58 상호 DB간에 컬럼 이름 비교 --======================================================================================
/* 양쪽 DB에서 사용하는 테이블 중에서 컬럼 이름 다른 항목을 찾는다. */ SELECT A.TABLE_NAME ,A.COLUMN_NAME ,A.COLUMN_ID ,A.DATA_TYPE || '(' || A.DATA_LENGTH || ')' DATA_TYPE FROM USER_TAB_COLUMNS@LINK_ESTDB A WHERE A.TABLE_NAME = :IN_TABLE_NAME AND NOT EXISTS (SELECT 'X' FROM USER_TAB_COLUMNS B WHERE B.TABLE_NAME = A.TABLE_NAME AND B.COLUMN_NAME = A.COLUMN_NAME);
SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_ID FROM USER_TAB_COLUMNS@LINK_ESTDB A WHERE A.TABLE_NAME = :IN_TABLE_NAME MINUS SELECT A.TABLE_NAME, A.COLUMN_NAME, A.COLUMN_ID FROM USER_TAB_COLUMNS A WHERE A.TABLE_NAME = :IN_TABLE_NAME --====================================================================================== --#. 59 해당 테이블의 세션을 제거하는 쿼리 --====================================================================================== /* 특정 테이블이 락을 발생하고 있으면 세션을 찾아서 중단시킨다. */ SELECT 'ALTER SYSTEM KILL SESSION ''' || S.SID||','||S.SERIAL# ||''';' FROM V$LOCK L, DBA_OBJECTS O, V$SESSION S WHERE L.ID1 = O.OBJECT_ID AND S.SID = L.SID AND O.OWNER = 'ESTDBA' AND O.OBJECT_NAME = 'TMP_GSYM2'
--====================================================================================== --#. 60 CPU를 많이 사용하는 세션의 식별(SQL TEXT 조회) --====================================================================================== SELECT A.* ,(SELECT SS.SQL_TEXT FROM V$SQLAREA SS WHERE SS.ADDRESS = A.SQL_ADDRESS AND ROWNUM <= 1 ) AS SQL_TEST FROM ( SELECT A.SID ,C.SERIAL# ,A.VALUE ,C.USERNAME ,C.STATUS ,C.PROGRAM ,C.SQL_ADDRESS ,ROW_NUMBER() OVER (ORDER BY A.VALUE DESC) RN FROM V$SESSTAT A ,V$STATNAME B ,V$SESSION C WHERE A.STATISTIC# = B.STATISTIC# AND A.SID = C.SID AND B.NAME = 'CPU used by this session' AND A.VALUE > 0 AND C.STATUS = 'ACTIVE' AND C.USERNAME IS NOT NULL ) A WHERE A.RN <= 10;
--====================================================================================== --#. 61 Blocking Lock Session 확인 --====================================================================================== SELECT B.BLOCKING_SESSION AS BLOCKING_SESSION_SID ,C.SID AS LOCK_SESSION_SID ,C.OWNER AS OBJECT_OWNER ,C.OBJECT AS OBJECT ,B.LOCKWAIT ,A.PIECE ,A.SQL_TEXT AS SQL FROM V$SQLTEXT A ,V$SESSION B ,V$ACCESS C WHERE A.ADDRESS = B.SQL_ADDRESS AND A.HASH_VALUE = B.SQL_HASH_VALUE AND B.SID = C.SID AND B.BLOCKING_SESSION IS NOT NULL AND C.OWNER NOT IN ('SYS', 'PUBLIC') AND C.OBJECT NOT IN ('TOAD_PLAN_TABLE') ORDER BY A.PIECE;
--====================================================================================== --#. 62 현재 세션에서 10초이상 걸리는 쿼리 조회 (SELECT절) --======================================================================================
SELECT ABS(SYSDATE - A.LAST_LOAD_TIME) * 24 * 60 * 60 AS SEC_TIEM, A.* FROM V$SQLAREA A ,V$SESSION B WHERE A.SQL_TEXT LIKE '%SELECT%' AND A.ADDRESS = B.SQL_ADDRESS AND B.STATUS = 'ACTIVE' AND A.ELAPSED_TIME >= 10 * 1000000 -- 실행계획에서 10초 이상 걸리는 쿼리를 조회(실제 걸리는 시간은 아님.) AND A.PARSING_SCHEMA_NAME NOT IN ('SYS', 'SYSTEM', 'SYSMAN') AND B.USERNAME IS NOT NULL;
--====================================================================================== --#. 63 현재 세션에서 PGA, UGA, CPU 사용량 세션별로 조회하는 쿼리 --======================================================================================
SELECT B.USERNAME ,A.SID ,A.PGA_USAGE ,A.UGA_USAGE ,A.CPU_USAGE_SECONDS ,B.MACHINE ,B.PROGRAM ,B.MODULE FROM (SELECT B.SID ,MAX(DECODE(C.NAME, 'session pga memory', TRUNC(B.VALUE / 1024 / 1024) || 'MB', 0)) AS PGA_USAGE ,MAX(DECODE(C.NAME, 'session uga memory', TRUNC(B.VALUE / 1024 / 1024) || 'MB', 0)) AS UGA_USAGE ,MAX(DECODE(C.NAME, 'CPU used by this session', (B.VALUE / 100) || ' Sec', 0)) AS CPU_USAGE_SECONDS FROM V$SESSTAT B ,V$STATNAME C WHERE B.STATISTIC# = C.STATISTIC# GROUP BY B.SID) A ,V$SESSION B WHERE B.SID = A.SID AND B.STATUS = 'ACTIVE' AND B.USERNAME IS NOT NULL; |
cs |
출처: https://epthffh.tistory.com/entry/SQL-Oracle-필수-스크립트-모음딕셔너리-SQL-문법-성능-분석 [물고기 개발자의 블로그]
'개발 > sql' 카테고리의 다른 글
[sql] MSSQL TOP 1 MYSQL LIMIT 1 사용법 (0) | 2020.05.26 |
---|---|
[sql/tibero] Tibero_tibero is stopped or disabled.[펌] (0) | 2020.05.23 |
[sql] MSSQL 무료 접속 툴 다운로드 (0) | 2020.05.20 |
[sql] REPLACE : 특정 문자를 찾아 바꾸기 [펌] (0) | 2020.05.13 |
[Tibero] AGGR_CONCAT, REPLACE, CHR 함수를 이용한 쿼리 만들기[펌] (0) | 2020.05.13 |