Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ************************************************** */
  2. /* This script creates Index-organized version of     */
  3. /* mother and heap organized version of child for use
  4. /* in the exercises on table                          */
  5. /* organization. It also generates a realistic        */
  6. /* distribution of data in the tables                 */
  7. /*                                                    */
  8. /* NOTE: you need to change the schema name in the    */
  9. /* two calls to DBMS_STATS to reflect your own        */
  10. /* schema instead of mine                             */
  11. /*                                                    */
  12. /* For the exercises, you need to change the          */
  13. /* organization of the tables as specified in the     */
  14. /* exercise text by modifying the DDL part of this    */
  15. /* script as needed. The procedure to create the      */
  16. /* data will run unchanged regardless of the table    */
  17. /* organization                                       */
  18. /*                                                    */
  19. /*                      Bo Brunsgaard, september 2016 */
  20. /* ************************************************** */
  21.  
  22. DROP TABLE child purge
  23. ;
  24. DROP TABLE mother purge
  25. ;
  26. DROP SEQUENCE sqMother
  27. ;
  28. DROP SEQUENCE sqChild
  29. ;
  30.  
  31. -- 1 - USING Index organized
  32. CREATE TABLE mother
  33. (
  34.     id       NUMBER(10,0)
  35.              NOT NULL
  36.              CONSTRAINT coPKMother
  37.                PRIMARY KEY
  38.                USING INDEX
  39.                
  40.   , firstName VARCHAR(50)
  41.               NOT NULL
  42.              
  43.   , lastName  VARCHAR(50)
  44.               NOT NULL
  45.              
  46.   , gender    CHAR(01)
  47.               NOT NULL
  48.               CONSTRAINT coCHMotherGender
  49.                 CHECK (gender = 'F')
  50.                
  51.   , dateBorn  DATE
  52.               NOT NULL
  53.              
  54.  , hairColor   VARCHAR2(20)
  55.                NOT NULL
  56.                
  57.  , meaninglessData CHAR(100)
  58.                    NOT NULL
  59. )
  60. ORGANIZATION INDEX
  61. ;
  62.  
  63. CREATE TABLE child
  64.  (
  65.      id      NUMBER(10,0)
  66.              NOT NULL
  67.              CONSTRAINT coPKChild
  68.                PRIMARY KEY
  69.                USING INDEX
  70.                
  71.   , firstName VARCHAR(50)
  72.               NOT NULL
  73.              
  74.   , lastName  VARCHAR(50)
  75.               NOT NULL
  76.              
  77.   , gender    CHAR(01)
  78.               NOT NULL
  79.               CONSTRAINT coCHChildGender
  80.                 CHECK (gender IN ('F','M'))
  81.              
  82.   , yearBorn  NUMBER(4,0)
  83.              
  84.   , motherId  NUMBER(10,0)
  85.               NOT NULL
  86.               CONSTRAINT coFKChildMother
  87.                 references mother
  88.              
  89.   , meaninglessData CHAR(100)
  90.                     NOT NULL
  91.    )
  92. ;
  93. CREATE INDEX ixChildMotherID
  94.   ON  child(motherID)
  95.   ;
  96.  
  97. CREATE SEQUENCE sqMother
  98.   START WITH 1
  99.   INCREMENT BY 1
  100.   noMaxValue
  101.   cache 20
  102. ;
  103.  
  104. CREATE SEQUENCE sqChild
  105.   START WITH 1
  106.   INCREMENT BY 1
  107.   noMaxValue
  108.   cache 20
  109. ;
  110. COMMIT
  111. ;
  112. /
  113. DECLARE
  114.  
  115.   noOfMothers           CONSTANT int := 5000;
  116.   noOfChildrenPerMother CONSTANT int := 5;
  117.  
  118. BEGIN
  119.  
  120.   FOR m IN 1..noOfMothers LOOP
  121.  
  122.      INSERT
  123.        INTO mother (id, firstname, lastname, gender, dateBorn, hairColor, meaninglessData)
  124.        VALUES      (  sqMother.NEXTVAL
  125.                     , 'First name ' || TO_CHAR(sqMother.CURRVAL)
  126.                     , 'Last Name ' || TO_CHAR(sqMother.CURRVAL)
  127.                     , 'F'
  128.                     , TO_DATE('1980-01-01 00:00:00','YYYY-MM-DD hh24:mi:ss')
  129.                        + TO_YMINTERVAL(TO_CHAR(FLOOR(dbms_random.VALUE(1,30))) || '-' || TO_CHAR(FLOOR(dbms_random.VALUE(1,12))) )
  130.                     ,  CASE MOD(m,50)
  131.                           WHEN 0 THEN 'blond'
  132.                           ELSE        'brown'
  133.                        END
  134.                     , 'x');
  135.                
  136.   END LOOP;
  137.  
  138.     FOR c IN 1..noOfChildrenPerMother LOOP
  139.        FOR m IN (SELECT id FROM mother) LOOP
  140.  
  141.          INSERT
  142.          INTO child (id, firstname, lastname, gender, yearBorn, motherID, meaninglessdata)
  143.          VALUES     (sqChild.NEXTVAL, 'Child first ' || TO_CHAR(sqChild.CURRVAL) ,
  144.                      'Child no ' || TO_CHAR(c) || 'of mother ' || TO_CHAR(m.id),
  145.                      'M'
  146.                      , EXTRACT(YEAR FROM (ADD_MONTHS(SYSDATE, -12 * c))), m.id, 'y' );
  147.    
  148.     END LOOP;
  149.  
  150.   END LOOP;
  151.  
  152.   COMMIT;
  153.  
  154. END;
  155. /
  156. -- -------------------------------------------------------
  157. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('MSKN','MOTHER');
  158. /
  159. EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('MSKN','CHILD');
  160. /
  161. --
  162. -- Check that the distributions of values are
  163. -- sort of reasonable
  164. --
  165. SELECT yearBorn
  166.      , COUNT(*)
  167.   FROM child
  168.   GROUP BY yearBorn
  169.   ORDER BY yearBorn
  170. ;
  171. SELECT SUBSTR(haircolor,1,10) AS haircolor
  172.      , COUNT(*)
  173.   FROM mother
  174.   GROUP BY haircolor
  175.   ;
  176.  
  177. -- 2 Check data storage (using DBMS_ROWID)
  178. --  2.1. How many blocks does the mother table use?
  179. --  2.2. Ditto for the child table
  180. --  2.3. Find the total number of blocks used to store the two tables.
  181.  
  182. -- 2.1
  183. SELECT LEAF_BLOCKS FROM DBA_INDEXES WHERE TABLE_NAME = 'MOTHER';
  184.  
  185.  
  186. SELECT COUNT(DISTINCT DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
  187.               FROM MOTHER;
  188.              
  189. -- 2.2
  190. SELECT COUNT(DISTINCT DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
  191.               FROM CHILD;
  192.  
  193. -- 2.3
  194. SELECT DISTINCT DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
  195.               FROM MOTHER
  196. UNION
  197. SELECT DISTINCT DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
  198.               FROM CHILD;
  199.  
  200. SELECT COUNT(*) FROM
  201. (
  202. SELECT COUNT(LEAF_BLOCKS) FROM DBA_INDEXES WHERE TABLE_NAME = 'MOTHER'
  203. UNION
  204. SELECT DISTINCT DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
  205.               FROM CHILD
  206.               )x;
  207.              
  208. -- 2.4. Choose one specific mother (say, motherID = 2500)
  209. --  2.4.1. In which block in the mother table is her data stored?
  210. --  2.4.2. In which blocks in the child table are her children stored?
  211.  
  212. -- 2.4.1
  213. SELECT ID, DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
  214.             FROM mother WHERE id = 2500
  215.             ORDER BY ID;
  216.            
  217. --  2.4.2
  218. SELECT ID, DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID)
  219.             FROM child WHERE motherid = 2500
  220.             ORDER BY ID;
  221.  
  222. -- 3. Retrieving mother data by table scan
  223. --  3.1. Write an SQL statement that retrieves the number of blond mothers
  224. --  3.2. Find AND SAVE the execution plan. How does Oracle resolve the query?
  225. --  3.3. Find the IO cost (in consistent gets) for executing that statement.
  226.  
  227. -- 3.1 - 3.2
  228. SET autotrace ON EXPLAIN
  229. /
  230. SELECT ID FROM MOTHER WHERE HAIRCOLOR = 'blond';
  231. /
  232. SET autotrace off
  233. /
  234.  
  235. -- 3.3
  236. SELECT ID FROM MOTHER WHERE HAIRCOLOR = 'blond';
  237.  
  238. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  239.        FROM V$STATNAME INNER JOIN V$SESSTAT
  240.        ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  241.        WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  242.        AND V$STATNAME.NAME = 'consistent gets';
  243.  
  244. -- 1114760, 1114871, 1114982 -> 111
  245.  
  246.  
  247. -- 4. Retrieving child data by table scan
  248. --  4.1. Write an SQL statement that retrieves the count of children born in the
  249. --  year 2012
  250. --  4.2. Find AND SAVE the execution plan. How does Oracle resolve the query?
  251. --  4.3. Find the IO cost (in consistent gets) for executing that statement.
  252.  
  253. -- 4.1 - 4.2
  254. SET autotrace ON EXPLAIN
  255. /
  256. SELECT ID FROM CHILD WHERE YEARBORN = 2012;
  257. /
  258. SET autotrace off
  259. /
  260.  
  261. -- 4.3
  262. SELECT ID FROM CHILD WHERE YEARBORN = 2012;
  263.  
  264. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  265.        FROM V$STATNAME INNER JOIN V$SESSTAT
  266.        ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  267.        WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  268.        AND V$STATNAME.NAME = 'consistent gets';
  269.  
  270. -- 1115595, 1116208, 1116821 -> 613
  271.  
  272. -- 5. Retrieving rows from both tables based on join (indexed)
  273. --  5.1. Write an SQL statement (join) that retrieves the name of a mother and
  274. --  the years of birth for all of her children. Reuse the mother that you
  275. --  decided on above in exercise 1.4
  276. --  5.2. Find AND SAVE the execution plan. How does Oracle resolve the query?
  277. --  5.3. Find the IO cost (in consistent gets) for executing that statement.
  278.  
  279. -- 5.1 - 5.2
  280.  
  281. SET autotrace ON EXPLAIN
  282. /
  283. SELECT MOTHER.FIRSTNAME, CHILD.YEARBORN
  284.     FROM MOTHER INNER JOIN CHILD ON (MOTHER.ID = CHILD.MOTHERID)
  285.     WHERE MOTHER.ID = 2500;
  286. /
  287. SET autotrace off
  288. /
  289.  
  290. -- 5.3
  291. SELECT MOTHER.FIRSTNAME, CHILD.YEARBORN
  292.     FROM MOTHER INNER JOIN CHILD ON (MOTHER.ID = CHILD.MOTHERID)
  293.     WHERE MOTHER.ID = 2500;
  294.  
  295. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  296.        FROM V$STATNAME INNER JOIN V$SESSTAT
  297.        ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  298.        WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  299.        AND V$STATNAME.NAME = 'consistent gets';
  300.        
  301. -- 1116939, 1116948, 1116957 -> 9
  302.  
  303. -- 6.1 - 6.2
  304.  
  305. SET autotrace ON EXPLAIN
  306. /
  307. SELECT FIRSTNAME FROM MOTHER
  308.        WHERE ID IN (1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900);
  309. /
  310. SET autotrace off
  311. /      
  312.  
  313. -- 6.3
  314. SELECT FIRSTNAME FROM MOTHER
  315.        WHERE ID IN (1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900);
  316.  
  317. SELECT SUM(V$SESSTAT.VALUE) AS "Total value"
  318.        FROM V$STATNAME INNER JOIN V$SESSTAT
  319.        ON (V$STATNAME.STATISTIC# = V$SESSTAT.STATISTIC#)
  320.        WHERE V$SESSTAT.SID = SYS_CONTEXT('userenv', 'sid')
  321.        AND V$STATNAME.NAME = 'consistent gets';
  322.        
  323. -- 1708946, 1708958, 1708970 -> 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement