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

  1. /************************************************************************/
  2. /* DATABASE PERFORMANCE CONCEPTS AND TECHNIQUES DEMONSTRATION PROGRAM   */
  3. /* MODULE: INDEX USE                                                    */
  4. /* source file: INDICES.SQC                                             */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /* HEADER FILES - USER DEFINED                                          */
  9. /************************************************************************/
  10. #include "db.h"
  11. /************************************************************************/
  12. /*     FUNCTION PROTOTYPES                                              */
  13. /************************************************************************/
  14. short far indices(void);
  15. short indx(void);
  16. short noindex(void);
  17.  
  18. /**************************/
  19.  double q1time, q2time;
  20.  struct timeb dbeg, dend;
  21.  
  22. /***************************************************************************/
  23. /*           indx query                                                    */
  24. /***************************************************************************/
  25.  
  26. /*************************************************/
  27. /*  SQL HOST VARIABLE DECLARATIONS               */
  28. /*************************************************/
  29. EXEC SQL BEGIN DECLARE SECTION;
  30. char st_cu_ssn[12];
  31. char st_cu_name[31];
  32. double s_bal;
  33. char c_accnumb[9];
  34. char st_cu_addr1[26];
  35. char st_cu_addr2[26];
  36. char st_cu_zip[6];
  37. EXEC SQL END DECLARE SECTION;
  38.  
  39. short num_rows;
  40.  
  41. /************************************************************************/
  42. /* FUNCTION  indx()                                                     */
  43. /************************************************************************/
  44. short indx(void)
  45. {
  46.  
  47.      q1time = 0.0;
  48.      num_rows = 0;
  49.  
  50. /*** SQL error handling ***/
  51.   EXEC SQL WHENEVER SQLERROR GO TO error;
  52.   EXEC SQL WHENEVER NOT FOUND GO TO ext;
  53.  
  54. /****** DECLARE CURSOR *********/
  55.      ftime (&dbeg);
  56.  
  57.      EXEC SQL DECLARE Cindx CURSOR FOR
  58.          SELECT CU_SSN FROM BMCHUGH.PERFDAT3
  59.          WHERE CU_SSN   BETWEEN  '111-11-1020' AND '111-11-1140';
  60.  
  61.      ftime(&dend);
  62.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  63.  
  64.      get_error( &sqlca, " declare cursor");
  65.  
  66. /****** OPEN CURSOR *********/
  67.      ftime (&dbeg);
  68.     EXEC SQL OPEN Cindx;
  69.      ftime(&dend);
  70.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  71.  
  72. //     get_error( &sqlca, " open cursor");
  73.  
  74. /****** FETCH ROWS till no more left *********/
  75.     while (sqlca.sqlcode == 0) {
  76.  
  77.      ftime (&dbeg);
  78.        EXEC SQL FETCH Cindx
  79.        INTO :st_cu_ssn;
  80.      ftime(&dend);
  81.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  82.  
  83.      if ((sqlca.sqlcode == 0) ) num_rows++;
  84.     }/*end while more rows */
  85.  
  86.  ext:
  87. /****** CLOSE CURSOR *********/
  88.      ftime (&dbeg);
  89.    EXEC SQL CLOSE Cindx;
  90.      ftime(&dend);
  91.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  92.  
  93.     return(0);
  94.  
  95.  error:
  96.  error_sql = sqlca.sqlcode; // copy the sql code into external variable
  97.                             // to be printed on user screen by dbdemo
  98.  return(-1);
  99.  
  100. }/* end indx */
  101.  
  102. /************************************************************************/
  103. /* FUNCTION noindex()                                                   */
  104. /************************************************************************/
  105.  
  106. short noindex()
  107. {
  108. /* the same queries executed with no index */
  109.  
  110.   q2time= 0.0;
  111.      num_rows = 0;
  112. /*** SQL error handling ***/
  113.   EXEC SQL WHENEVER SQLERROR GO TO error;
  114.   EXEC SQL WHENEVER NOT FOUND GO TO ext;
  115.  
  116.  
  117. /****** DECLARE CURSOR *********/
  118.      ftime (&dbeg);
  119.      EXEC SQL DECLARE CNOINDEX CURSOR FOR
  120.          SELECT * FROM BMCHUGH.PERFDAT3
  121.          WHERE CU_SSN   BETWEEN  '111-11-1020' AND '111-11-1140';
  122.  
  123.      ftime(&dend);
  124.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  125.    get_error( &sqlca, " prepare cursor");
  126.  
  127.      ftime (&dbeg);
  128.     EXEC SQL OPEN CNOINDEX;
  129.      ftime(&dend);
  130.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  131.     get_error( &sqlca, " open cursor");
  132.  
  133.     while (sqlca.sqlcode == 0) {
  134.  
  135. /****** FETCH ROWS till no more left *********/
  136.      ftime (&dbeg);
  137.        EXEC SQL FETCH CNOINDEX
  138.           INTO :st_cu_ssn;
  139.      ftime(&dend);
  140.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  141.      if ((sqlca.sqlcode == 0) ) num_rows++;
  142.     }/*end while more rows */
  143.  
  144.  ext:
  145. /****** CLOSE CURSOR *********/
  146.      ftime (&dbeg);
  147.    EXEC SQL CLOSE CNOINDEX;
  148.      ftime(&dend);
  149.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  150. /*     printf("\nnoindex time = %7.3f", q2time);
  151. */
  152.     return(0);
  153.  
  154.  error:
  155.  error_sql = sqlca.sqlcode;// copy the sql code into external variable
  156.                            // to be printed on user screen by dbdemo
  157.  return(-1);
  158. }/* end noindex */
  159. /***************************************************************************/
  160. /***************************************************************************/
  161. /*              MAIN PROGRAM                                               */
  162. /*      1.  indx                                                           */
  163. /*      2.  noindex                                                            */
  164. /***************************************************************************/
  165. /************************************************************************/
  166. /* FUNCTION indices()                                                   */
  167. /* This function calls the functions indx() and noindex(),              */
  168. /* num_run times.                                                       */
  169. /* gets the timing info from external variables q1time and              */
  170. /* q2time  and applies the averaging logic, depending upon              */
  171. /* user entered variable num_runs.                                      */
  172. /* i.e. if num_runs = 1 then q1avg = q1time and q2avg=q2time            */
  173. /* otherwise, it disregards the first value and averages the rest       */
  174. /************************************************************************/
  175. /************************************************************************/
  176. short far indices()
  177. {
  178.  short i;
  179.  
  180.  
  181.   q1avg=0.0;
  182.   q2avg=0.0;
  183.     // SET THE NUMBER OF DEMOS FOR THIS SECTION 1 OR 2.. 2 IS MAX!!!!
  184.        Timetext[0].test_num = 2;
  185.  
  186.   // call the functions num_runs time
  187.     for  (i=1; i<=num_runs; i++)
  188.         {
  189.             if (indx())
  190.                 return(3);
  191.             if (i!=1)
  192.                q1avg =q1avg + q1time;
  193.         }/*end for */
  194.     if (num_runs==1)
  195.        q1avg = q1time;
  196.     else
  197.        q1avg = q1avg/(num_runs-1);
  198.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 1
  199.        strcpy(Timetext[0].demoname,"with index    ");
  200.        Timetext[0].demotime = q1avg;
  201.        Timetext[0].rows = num_rows;
  202.  
  203.   // call the functions num_runs time
  204.     for  (i=1; i<=num_runs; i++)
  205.         {
  206.             if (noindex())
  207.                 return(3);
  208.         if (i!=1)
  209.                q2avg = q2avg+ q2time;
  210.         }/*end for */
  211.     if (num_runs==1)
  212.        q2avg = q2time;
  213.     else
  214.        q2avg = q2avg/(num_runs-1);
  215.  
  216.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 2
  217.        strcpy(Timetext[1].demoname,"no index      ");
  218.        Timetext[1].demotime = q2avg;
  219.        Timetext[1].rows = num_rows;
  220.  
  221.  return(0);
  222. }/* end main */
  223.