/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
**
** The following sample of source code ("Sample") is owned by International
** Business Machines Corporation or one of its subsidiaries ("IBM") and is
** copyrighted and licensed, not sold. You may use, copy, modify, and
** distribute the Sample in any form without payment to IBM, for the purpose of
** assisting you in the development of your applications.
**
** The Sample code is provided to you on an "AS IS" basis, without warranty of
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
** not allow for the exclusion or limitation of implied warranties, so the above
** limitations or exclusions may not apply to you. IBM shall not be liable for
** any damages you suffer as a result of using, copying, modifying or
** distributing the Sample, even if IBM has been advised of the possibility of
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: tbrunstats.sqc
**
** SAMPLE: How to perform runstats on a table
**
** DB2 APIs USED:
** db2Runstats -- Runstats
**
** SQL STATEMENT USED:
** SELECT
**
** STRUCTURES USED:
**
** db2ColumnData
** sqlca
**
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
**
** For information on using SQL statements, see the SQL Reference.
**
** For information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, building, and running DB2
** applications, visit the DB2 Information Center:
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include <db2ApiDf.h>
#include "utilemb.h"
int TbRunstats(void);
int SchemaNameGet(void); /* support function */
EXEC SQL BEGIN DECLARE SECTION;
char dbAlias[15];
char user[129];
char pswd[15];
char tableName[129];
char schemaName[129];
EXEC SQL END DECLARE SECTION;
int main(int argc, char *argv[])
{
int rc = 0;
char dbAlias[SQL_ALIAS_SZ + 1];
char user[USERID_SZ + 1];
char pswd[PSWD_SZ + 1];
char nodeName[SQL_INSTNAME_SZ + 1];
/* check the command line arguments */
rc = CmdLineArgsCheck3(argc, argv, dbAlias, nodeName, user, pswd);
if (rc != 0)
{
return rc;
}
printf("\nTHIS SAMPLE SHOWS ");
printf("HOW TO PERFROM RUNSTATS ON A TABLE.\n");
/* connect to database */
rc = DbConn(dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}
rc = TbRunstats();
/* disconnect from the database */
rc = DbDisconn(dbAlias);
if (rc != 0)
{
return rc;
}
return 0;
} /* main */
/* Gets the schema name of the table */
int SchemaNameGet(void)
{
struct sqlca sqlca;
/* get table schema name */
EXEC SQL SELECT tabschema INTO :schemaName
FROM syscat.tables
WHERE tabname = :tableName;
EMB_SQL_CHECK("table schema name -- get");
/* get rid of spaces from the end of schemaName */
strtok(schemaName, " ");
return 0;
} /* SchemaNameGet */
/* Performs Runstats on the table */
int TbRunstats(void)
{
int rc = 0;
struct sqlca sqlca;
char fullTableName[258];
db2Uint32 versionNumber = db2Version970;
db2RunstatsData runStatData;
db2ColumnData *columnList;
printf("\n-----------------------------------------------------------");
printf("\nUSE THE DB2 APIs:\n");
printf(" db2Runstats -- Runstats\n");
printf("TO UPDATE THE STATISTICS OF A TABLE.\n");
/* get fully qualified name of the table */
strcpy(tableName, "EMPLOYEE");
rc = SchemaNameGet();
if (rc != 0)
{
return rc;
}
strcpy(fullTableName, schemaName);
strcat(fullTableName, ".");
strcat(fullTableName, tableName);
printf(" Update statistics for the table: %s\n", fullTableName);
columnList = (struct db2ColumnData *)malloc(sizeof(struct db2ColumnData));
columnList->piColumnName = (unsigned char *)"empno";
columnList->iColumnFlags = DB2RUNSTATS_COLUMN_LIKE_STATS;
printf("\nPerforming runstats on table EMPLOYEE for column EMPNO\n");
printf("with the following options:\n");
printf(" Distribution statistics for all partitions\n");
printf(" Frequent values for table set to 30\n");
printf(" Quantiles for table set to -1 (NUM_QUANTILES as in DB Cfg)\n");
printf(" Allow others to have read-only while gathering statistics\n");
/* runstats on table */
runStatData.iSamplingOption = 0;
runStatData.piTablename = (unsigned char *) fullTableName;
runStatData.piColumnList = &columnList;
runStatData.piColumnDistributionList = NULL;
runStatData.piColumnGroupList = NULL;
runStatData.piIndexList = NULL;
runStatData.iRunstatsFlags = DB2RUNSTATS_KEY_COLUMNS |
DB2RUNSTATS_DISTRIBUTION | DB2RUNSTATS_ALLOW_READ;
runStatData.iNumColumns = 1;
runStatData.iNumColdist = 0;
runStatData.iNumColGroups = 0;
runStatData.iNumIndexes = 0;
runStatData.iParallelismOption = 0;
runStatData.iTableDefaultFreqValues = 30;
runStatData.iTableDefaultQuantiles = -1;
runStatData.iUtilImpactPriority = 100;
db2Runstats(versionNumber, &runStatData, &sqlca);
DB2_API_CHECK("table -- runstats");
free(columnList);
printf("\nMake sure to rebind all packages that use this table.\n");
return rc;
} /* TbRunstats */