Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- DYNAMIC PERFORMANCE VIEWS
  2. -- SESSION #3
  3.  
  4. DROP TABLE child purge
  5. ;
  6. DROP TABLE mother purge
  7. ;
  8. DROP SEQUENCE sqMother
  9. ;
  10. DROP SEQUENCE sqChild
  11. ;
  12.  
  13. CREATE TABLE mother
  14. (
  15.    ID    NUMBER(10,0)
  16.          NOT NULL
  17.          CONSTRAINT coPKMother
  18.          PRIMARY KEY
  19.          
  20.  , name  VARCHAR2(100)
  21.          NOT NULL
  22.          
  23.  , gender CHAR(01)
  24.           NOT NULL
  25.           CONSTRAINT coCHMotherGender
  26.           CHECK (gender IN ('F'))
  27.          
  28.  , dateOfBirth  DATE
  29.                 NOT NULL
  30.                
  31.  , meaninglessData CHAR(1000)
  32. )
  33. ;
  34. CREATE TABLE child
  35. (
  36.    ID    NUMBER(10,0)
  37.          NOT NULL
  38.          CONSTRAINT coPKChild
  39.            PRIMARY KEY
  40.            
  41.  , motherID NUMBER(10,0)
  42.             NOT NULL
  43.             CONSTRAINT coFKChildMother
  44.               references mother
  45.              
  46.  , name     VARCHAR(100)
  47.             NOT NULL
  48.            
  49.  , gender   CHAR(01)
  50.             NOT NULL
  51.             CONSTRAINT coCHChildGender
  52.               CHECK  (gender IN ('M','F'))
  53.              
  54.  , dateOfBirth DATE
  55.                NOT NULL
  56.                
  57.  , meaninglessData CHAR(1000)
  58.                    NOT NULL
  59. )
  60. ;
  61. CREATE SEQUENCE sqMother
  62.   START WITH 1
  63.   INCREMENT BY 1
  64.   noMaxValue
  65.   cache 100
  66.   ;
  67.   CREATE SEQUENCE sqChild
  68.   START WITH 1
  69.   INCREMENT BY 1
  70.   noMaxValue
  71.   cache 100
  72.  
  73. /
  74. DECLARE
  75.  
  76.   noOfMothers  CONSTANT int := 1000;
  77.   noChildrenPerMother CONSTANT int := 5;
  78.  
  79. BEGIN
  80.  
  81.   EXECUTE IMMEDIATE 'truncate table child';
  82.   EXECUTE IMMEDIATE 'truncate table mother';
  83.   FOR m IN 1..noOfMothers LOOP
  84.  
  85.      INSERT INTO mother (id,name,gender,dateOfBirth,meaninglessData)
  86.      VALUES (    sqMother.NEXTVAL
  87.               , 'Mother ' || TO_CHAR(sqMother.CURRVAL)
  88.               , 'F'
  89.               , SYSDATE
  90.               , 'x'
  91.               );
  92.  
  93.      FOR c IN 1..noChildrenPerMother LOOP
  94.        INSERT INTO child (id, motherID, name, gender, dateOfBirth, meaninglessData)
  95.         VALUES (
  96.                    sqChild.NEXTVAL
  97.                 ,  sqMother.CURRVAL
  98.                 ,  'Child number ' || TO_CHAR(c) || ' of mother ' || TO_CHAR(sqMother.CURRVAL)
  99.                 ,  'F'
  100.                 ,  SYSDATE
  101.                 , 'x'
  102.                );
  103.      END LOOP;
  104.    
  105.   END LOOP;
  106.   COMMIT;
  107. END;
  108.  
  109.  
  110. -- 1) Use the static
  111. -- data dictionary to obtain a list of the dynamic performance views.
  112.  
  113. SELECT VIEW_NAME FROM DBA_VIEWS WHERE VIEW_NAME LIKE 'V_$%';
  114.  
  115. -- 2) Using the V$SESSION view, find the processes running on your instance. Can you identify
  116. -- which process is yourself? And what might some of the others be?
  117. SELECT PROCESS, USERNAME FROM V$SESSION;
  118.  
  119. -- 3) Use the V$SGA view to calculate how much memory your
  120. -- instance is using for Shared Global Area. And, once you have that number, could you
  121. -- please return it in Gigabytes as well?
  122. SELECT NAME, VALUE FROM V$SGA;
  123. SELECT (SUM(VALUE) / 1073741824) AS "Total memory" FROM V$SGA;
  124.  
  125. -- 4) Session memory usage is exposed on a session-by-by-session
  126. -- level in the view V$SESSTAT, so we will need to sum this for all sessions. Join
  127. -- V$SESSTAT with the view V$STATNAME in order to limited the search for the statistic named
  128. -- 'session pga memory'
  129. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  130. FROM V$STATNAME INNER JOIN V$SESSTAT
  131. ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  132. WHERE V$STATNAME.NAME = 'session pga memory';
  133.  
  134. -- 5) SCRIPT TO CREATE AND POPULATE THE TABLES
  135.  
  136. -- 6) Write a piece of SQL that retrieves one mother (with some ID) and all of her children (so
  137. -- we are clearly in join-territory here)
  138. SELECT m.ID, m.NAME, c.NAME AS "Child name"
  139. FROM MOTHER m INNER JOIN CHILD c ON (c.MOTHERID = m.ID)
  140. WHERE m.ID = 22;
  141.  
  142. -- 7) . Expand the code from question 4
  143. -- 7.1. Change it so it shows ALL the different resource counters for your session. You can
  144. -- identify your own session by adding the line where sid =
  145. -- sys_context(‘userenv’,’sid’) to the SQL.
  146. -- 7.2. Take a peek at the results: how many different types of ressources can we track the
  147. -- consumption of
  148.  
  149. SELECT DISTINCT V$STATNAME.NAME, SUM(V$SESSTAT.VALUE) AS "Total value"
  150. FROM V$STATNAME INNER JOIN V$SESSTAT
  151. ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  152. WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  153. GROUP BY V$STATNAME.NAME;
  154.  
  155. SELECT COUNT(DISTINCT V$STATNAME.NAME) AS "Number of resources"
  156. FROM V$STATNAME INNER JOIN V$SESSTAT
  157. ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  158. WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid');
  159.  
  160. -- 8) One of the resource counters is named 'consistent gets' This records the number of
  161. -- times your session has requested a block of data to be read.
  162. -- 8.1. Change the SQL to retrieve only that value
  163. -- 8.2. Make a note of the value that you find
  164. -- ### 86735
  165.  
  166. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  167. FROM V$STATNAME INNER JOIN V$SESSTAT
  168. ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  169. WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  170. AND V$STATNAME.NAME = 'consistent gets';
  171.  
  172. -- 9
  173. SELECT m.ID, m.NAME, c.NAME AS "Child name"
  174. FROM MOTHER m INNER JOIN CHILD c ON (c.MOTHERID = m.ID)
  175. WHERE m.ID = 22;
  176.  
  177. -- 10, 11)
  178. --Find the IO cost of retrieving the rows:
  179. --10.1. Re-run the SQL that retrieves the value of 'consistent gets'
  180. --10.2. Calculate the difference between the value that you now got and the one from point
  181. -- The difference is the number of blocks that Oracle had to read to return your data
  182.  
  183. /*
  184. 88499, 89381, DIFF 882
  185. 89381, 90263, DIFF 882
  186. 90263, 91145, DIFF 882
  187. */
  188.  
  189. -- 12) Above, we got the IO cost for retrieving a mother and her children. Let try to improve that.
  190. --12.1. Create an index on the foreign key in the Child table (use the create index name on
  191. -- tablename (columname) syntax
  192. --12.2. Make sure that the query optimizer knows about the index. Use the execute
  193. -- DBMS_STATS.GATHER_TABLE_STATS ('owner','tablename') syntax for both tables
  194. --12.3. Clear out any old execution plans. Use the alter system flush shared_pool syntax
  195. --12.4. Repeat steps 8 through 11
  196. --12.5. Compare the IO cost without and with the index in place. Was the index a good idea?
  197.  
  198. CREATE INDEX ixMotherChild ON CHILD (MOTHERID);
  199.  
  200. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('mircea','CHILD');
  201. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('mircea','MOTHER');
  202.  
  203. ALTER SYSTEM FLUSH SHARED_POOL;
  204.  
  205. SELECT m.ID, m.NAME, c.NAME AS "Child name"
  206. FROM MOTHER m INNER JOIN CHILD c ON (c.MOTHERID = m.ID)
  207. WHERE m.ID = 22;
  208.  
  209. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  210. FROM V$STATNAME INNER JOIN V$SESSTAT
  211. ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  212. WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  213. AND V$STATNAME.NAME = 'consistent gets';
  214.  
  215. /*
  216. 18600, 18607, DIFF - 7 (no index: 882)
  217. */
  218.  
  219. -- 13) Write SQL that joins the two views for your own session (check question 7 for hints),
  220. -- showing which SQL statements that you are running/just ran.
  221. SELECT SQL_TEXT FROM V$SESSION INNER JOIN V$SQL
  222. ON (V$SESSION.SQL_ID = V$SQL.SQL_ID OR V$SQL.SQL_ID = V$SESSION.PREV_SQL_ID)
  223. WHERE SID = SYS_CONTEXT('userenv','SID');
  224.  
  225. --- *** EXTRA
  226. -- 14)
  227. --14.1. Reset the entire thing. Go back and rerun the script from step 5.
  228. --14.2. Explain the statement that retrieves a mother and her children. SAVE the execution
  229. --plan that you get
  230. --14.3. Recreate the index, update statistics and all that. In other words, repeat the code
  231. --from steps 12.1 and 12.2
  232. --14.4. Explain the statement again. Save the execution plan
  233. --14.5. Compare the two execution plans, the one without the index and the one with the
  234. --index. How does Oracle resolve the query in the two cases, and why is one much
  235. --cheaper than the other
  236.  
  237. DROP INDEX ixMotherChild;
  238.  
  239. SET AUTOTRACE ON EXPLAIN
  240. /
  241. SELECT m.ID, m.NAME, c.NAME AS "Child name"
  242. FROM MOTHER m INNER JOIN CHILD c ON (c.MOTHERID = m.ID)
  243. WHERE m.ID = 22;
  244. /
  245. SET AUTOTRACE OFF
  246. /
  247.  
  248. /*
  249. -------------------------------------------------------------------------------------------
  250. | Id  | Operation                    | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
  251. -------------------------------------------------------------------------------------------
  252. |   0 | SELECT STATEMENT             |            |     5 |   240 |   240   (0)| 00:00:01 |
  253. |   1 |  NESTED LOOPS                |            |     5 |   240 |   240   (0)| 00:00:01 |
  254. |   2 |   TABLE ACCESS BY INDEX ROWID| MOTHER     |     1 |    15 |     2   (0)| 00:00:01 |
  255. |*  3 |    INDEX UNIQUE SCAN         | COPKMOTHER |     1 |       |     1   (0)| 00:00:01 |
  256. |*  4 |   TABLE ACCESS FULL          | CHILD      |     5 |   165 |   238   (0)| 00:00:01 |
  257. -------------------------------------------------------------------------------------------
  258. */
  259.  
  260. CREATE INDEX ixMotherChild ON CHILD (MOTHERID);
  261.  
  262. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('mircea','CHILD');
  263. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('mircea','MOTHER');
  264.  
  265. SET AUTOTRACE ON EXPLAIN
  266. /
  267. SELECT m.ID, m.NAME, c.NAME AS "Child name"
  268. FROM MOTHER m INNER JOIN CHILD c ON (c.MOTHERID = m.ID)
  269. WHERE m.ID = 22;
  270. /
  271. SET AUTOTRACE OFF
  272. /
  273.  
  274. /*
  275. ------------------------------------------------------------------------------------------------------
  276. | Id  | Operation                            | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
  277. ------------------------------------------------------------------------------------------------------
  278. |   0 | SELECT STATEMENT                     |               |     5 |   240 |     4   (0)| 00:00:01 |
  279. |   1 |  NESTED LOOPS                        |               |     5 |   240 |     4   (0)| 00:00:01 |
  280. |   2 |   TABLE ACCESS BY INDEX ROWID        | MOTHER        |     1 |    15 |     2   (0)| 00:00:01 |
  281. |*  3 |    INDEX UNIQUE SCAN                 | COPKMOTHER    |     1 |       |     1   (0)| 00:00:01 |
  282. |   4 |   TABLE ACCESS BY INDEX ROWID BATCHED| CHILD         |     5 |   165 |     2   (0)| 00:00:01 |
  283. |*  5 |    INDEX RANGE SCAN                  | IXMOTHERCHILD |     5 |       |     1   (0)| 00:00:01 |
  284. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement