home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / DBDEMO.ZIP / DEMOFLS.ZIP / RBLOCK.SQC < prev    next >
Text File  |  1991-06-30  |  7KB  |  187 lines

  1. /************************************************************************/
  2. /* DATABASE PERFORMANCE CONCEPTS AND TECHNIQUES DEMONSTRATION PROGRAM   */
  3. /* MODULE: RECORD BLOCKING                                              */
  4. /* source file: RBLOCK.SQC                                              */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /* HEADER FILES - USER DEFINED                                          */
  9. /************************************************************************/
  10. #include "db.h"
  11.  
  12. /************************************************************************/
  13. /*     FUNCTION PROTOTYPES                                              */
  14. /************************************************************************/
  15. short far rblockno();
  16. int exit_err(void);
  17. int blocking(void);
  18.  
  19. struct timeb dbeg, dend;
  20. extern double qtime_noblock;  // filled in rblockno()
  21. double qtime_block;
  22. /***************************************************************************/
  23. /*      1.  rblock                                                         */
  24. /*      2.  block                                                          */
  25. /***************************************************************************/
  26.  
  27. /*************************************************/
  28. /*  SQL HOST VARIABLE DECLARATIONS               */
  29. /*************************************************/
  30. EXEC SQL BEGIN DECLARE SECTION;
  31.      double balance;
  32.      short num_rows;
  33. EXEC SQL END DECLARE SECTION;
  34.  
  35. /************************************************************************/
  36. /* FUNCTION    rblock()                                                 */
  37. /* This function calls the functions blocking() and noblock(),          */
  38. /* num_run times.                                                       */
  39. /* gets the timing info from external variables qtime_block and         */
  40. /* qtime_noblock  and applies the averaging logic, depending upon       */
  41. /* user entered variable num_runs.                                      */
  42. /* i.e. if num_runs = 1 then q1avg = qtime_block and q2avg=qtime_noblock*/
  43. /* otherwise, it disregards the first value and averages the rest       */
  44. /************************************************************************/
  45. short far rblock()
  46. {
  47.    short i;
  48.       qtime_block = 0.0; // initialize timing variable
  49.       q1avg = 0.0;
  50.       q2avg = 0.0;
  51.  
  52.   // call the functions num_runs time
  53.     for  (i=1; i<=num_runs; i++)
  54.         {
  55.         if (blocking())
  56.             return(3);
  57.  
  58.         if (i!=1)
  59.             q1avg =q1avg + qtime_block;
  60.         }/*end for */
  61.  
  62.     if (num_runs==1)
  63.        q1avg = qtime_block;
  64.     else
  65.        q1avg = q1avg/(num_runs-1);
  66.  
  67.  
  68.     // SET THE NUMBER OF DEMOS FOR THIS SECTION = 2 !!!!
  69.        Timetext[0].test_num = 2;
  70.  
  71.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 1
  72.        strcpy(Timetext[0].demoname,"Record Blocking");
  73.        Timetext[0].demotime = q1avg;
  74.        Timetext[0].rows = num_rows;
  75.  
  76.        qtime_noblock = 0.0;
  77.  
  78.     for  (i=1; i<=num_runs; i++)
  79.         {
  80.         if (rblockno())
  81.           return(3);
  82.  
  83.         if (i!=1)
  84.             q2avg =q2avg + qtime_noblock;
  85.         }/*end for */
  86.  
  87.     if (num_runs==1)
  88.        q2avg = qtime_noblock;
  89.     else
  90.        q2avg = q2avg/(num_runs-1);
  91.  
  92.     // SET THE NUMBER OF DEMOS FOR THIS SECTION 1 OR 2.. 2 IS MAX!!!!
  93.  
  94.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 2
  95.        strcpy(Timetext[1].demoname,"No Blocking   ");
  96.        Timetext[1].demotime = q2avg;
  97.        Timetext[1].rows = num_rows;
  98.  
  99.  
  100. /*    sqlestop(&sqlca);  */     /* stop databse manager */
  101.  return(0);
  102. }/* end main */
  103.  
  104. /***************************************************************************/
  105. /*       blocking                                                          */
  106. /***************************************************************************/
  107.  
  108. /************************************************************************/
  109. /* FUNCTION  blocking                                                   */
  110. /* Calculates the median value of CU_ENDBAL column  from CHECKING TABLE */
  111. /* uses record blocking because this module has been precompiled and    */
  112. /* bound with /K= ALL option                                            */
  113. /************************************************************************/
  114. int blocking()
  115. {
  116.   char error_from[20];  // used for debugging to chk which query errored
  117.   short cur_row;        // which row the cursor is on
  118.   qtime_block = 0.0;    // initialize timing variable
  119.  
  120. /*** SQL error handling ***/
  121.   EXEC SQL WHENEVER SQLERROR GO TO error;
  122.   EXEC SQL WHENEVER NOT FOUND GO TO ext;
  123.  
  124. /*** Find the total number of rows ***/
  125.      strcpy(error_from,"select count * ");
  126.      ftime (&dbeg);
  127.      EXEC SQL SELECT COUNT(*)
  128.          INTO :num_rows
  129.          FROM BMCHUGH.CHECKING;
  130.      ftime(&dend);
  131.      qtime_block += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  132.  
  133. /*    printf("\ntotal number of rows = %d", num_rows);
  134. */
  135.  
  136. /****** DECLARE CURSOR *********/
  137.      strcpy(error_from," declare cursor");
  138.      ftime (&dbeg);
  139.      EXEC SQL DECLARE CBLOCK CURSOR FOR
  140.         SELECT C_ENDBAL FROM BMCHUGH.CHECKING
  141.         ORDER BY C_ENDBAL;
  142.      ftime(&dend);
  143.      qtime_block += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  144.  
  145. /****** OPEN CURSOR *********/
  146.     strcpy(error_from," open cursor");
  147.      ftime (&dbeg);
  148.     EXEC SQL OPEN CBLOCK;
  149.      ftime(&dend);
  150.      qtime_block += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  151.     cur_row = 1;
  152.  
  153. /****** FETCH num_rows/2 ROWS  *********/
  154.     while ((sqlca.sqlcode == 0) && (cur_row <= num_rows/2) ) {
  155.  
  156.        cur_row++;
  157.  
  158.        ftime (&dbeg);
  159.        EXEC SQL FETCH CBLOCK
  160.        INTO :balance;
  161.        ftime(&dend);
  162.  
  163.        qtime_block += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  164.  
  165. /*       printf("%7.3f\n", balance);
  166. */
  167.     }/*end while more rows */
  168.  
  169. /****** CLOSE CURSOR *********/
  170.      ftime (&dbeg);
  171.     EXEC SQL CLOSE CBLOCK;
  172.      ftime(&dend);
  173.      qtime_block += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  174.  
  175. /*    printf("\nmedian balance = %7.3f",balance);
  176.     printf("\ntotal time = %7.3f", qtime_block);
  177. */
  178.  ext:
  179.     return(0);
  180.  
  181.  error:
  182. /*   get_error( &sqlca, error_from);
  183. */
  184.  error_sql = sqlca.sqlcode;
  185. return(-1);
  186. }/* end blocking */
  187.