473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

RUNSTATS from SQL for UDB

We are on 8.1.6 currently on AIX. Has anyone been able to come up
with a solution that allows the ability to do a RUNSTATS from SQL
prompt instead of the command line? I see lots of posts about writing
a C procedure or something along those lines. Has anyone done this?
Does IBM have the proc already built? Does this get easier in 8.2
with extended support for the SQL procedural language?
Spencer
Nov 12 '05 #1
11 6097
On Wed, 22 Sep 2004 07:31:14 -0700, Spencer wrote:
Has anyone been able to come up with a
solution that allows the ability to do a RUNSTATS from SQL prompt instead
of the command line?


Doesn't a command like this work?:

db2 'runstats on table db2inst1.tablename'

(Exchange "db2inst1" with yourr schema name.)

--
Greetings from Troels Arvin, Copenhagen, Denmark

Nov 12 '05 #2
That is from the db2 command line interface not the sql interface. I
need to be able to execute the runstats from a sql interface because
of security setup on a particular application. The application can
make an odbc connection to the database to issue sql commands it
cannot issue command line interface commands.
Spencer
Troels Arvin <tr****@arvin.dk> wrote in message news:<pa****************************@arvin.dk>...
On Wed, 22 Sep 2004 07:31:14 -0700, Spencer wrote:
Has anyone been able to come up with a
solution that allows the ability to do a RUNSTATS from SQL prompt instead
of the command line?


Doesn't a command like this work?:

db2 'runstats on table db2inst1.tablename'

(Exchange "db2inst1" with yourr schema name.)

Nov 12 '05 #3
Spencer wrote:
That is from the db2 command line interface not the sql interface. I
need to be able to execute the runstats from a sql interface because
of security setup on a particular application. The application can
make an odbc connection to the database to issue sql commands it
cannot issue command line interface commands.


Why don't you write a stored procedure that uses the "db2Runstats" API call
internally? Then you could simply do this:

db2 "CALL myRunstatsProc(...)"

and that is normal SQL which can be run through ODBC.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #4
Right....like I said has anyone done this? Any good info out there on
the DB2 SQL Procedural Language. Everything I find is extremely
vague.
Spencer
Knut Stolze <st****@de.ibm.com> wrote in message news:<ci**********@fsuj29.rz.uni-jena.de>...
Spencer wrote:
That is from the db2 command line interface not the sql interface. I
need to be able to execute the runstats from a sql interface because
of security setup on a particular application. The application can
make an odbc connection to the database to issue sql commands it
cannot issue command line interface commands.


Why don't you write a stored procedure that uses the "db2Runstats" API call
internally? Then you could simply do this:

db2 "CALL myRunstatsProc(...)"

and that is normal SQL which can be run through ODBC.

Nov 12 '05 #5
Spencer wrote:
Right....like I said has anyone done this?
I haven't, but I don't see any particular problem. Do you need some help
coding this?
Any good info out there on
the DB2 SQL Procedural Language. Everything I find is extremely
vague.


Hmmm... did you have a look at the SQL Reference or do you know about this
book: http://www.informit.com/title/0131007726

p.s: SQL/PL won't help you with invoking the db2Runstats API.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #6
Sure if you want to code it for me that would be great! So if I am
not writing it in SQL/PL then I suppose it is C? Any primers in
writing stored proc's for DB2. I have not written any for DB2.
spencer

Knut Stolze <st****@de.ibm.com> wrote in message news:<ci**********@fsuj29.rz.uni-jena.de>...
Spencer wrote:
Right....like I said has anyone done this?


I haven't, but I don't see any particular problem. Do you need some help
coding this?
Any good info out there on
the DB2 SQL Procedural Language. Everything I find is extremely
vague.


Hmmm... did you have a look at the SQL Reference or do you know about this
book: http://www.informit.com/title/0131007726

p.s: SQL/PL won't help you with invoking the db2Runstats API.

Nov 12 '05 #7
Spencer wrote:
Sure if you want to code it for me that would be great! So if I am
not writing it in SQL/PL then I suppose it is C? Any primers in
writing stored proc's for DB2. I have not written any for DB2.


Sorry that it took so long...

Here is the procedure code. It is pretty basic and does not contain any
serious error handling. But you should be able to build on that.
--------------------------------------
#include <sql.h>
#include <sqludf.h>
#include <db2ApiDf.h>

int runstats_proc(
SQLUDF_VARCHAR *tableName,
SQLUDF_NULLIND *tableName_ind,
SQLUDF_TRAIL_ARGS)
{
SQL_API_RC rc = SQL_RC_OK;
db2RunstatsData data;
struct sqlca sqlca;

if (SQLUDF_NULL(tableName_ind)) {
goto cleanup;
}

/* initialize data structures */
data.iSamplingOption = 0;
data.piTablename = (unsigned char *)tableName;
data.piColumnList = NULL;
data.piColumnDistributionList = NULL;
data.piColumnGroupList = NULL;
data.piIndexList = NULL;
data.iRunstatsFlags = DB2RUNSTATS_ALL_COLUMNS |
DB2RUNSTATS_DISTRIBUTION | DB2RUNSTATS_ALL_INDEXES |
DB2RUNSTATS_EXT_INDEX | DB2RUNSTATS_ALLOW_READ;
data.iNumColumns = 0;
data.iNumColdist = 0;
data.iNumColGroups = 0;
data.iNumIndexes = 0;
data.iParallelismOption = 0;
data.iTableDefaultFreqValues = -1; /* use default */
data.iTableDefaultQuantiles = -1; /* use default */
data.iUtilImpactPriority = 0;
data.iSamplingRepeatable = 0; /* unused */
memset(&sqlca, 0x00, sizeof sqlca);

rc = db2Runstats(db2Version820, &data, &sqlca);
if (rc != SQL_RC_OK || SQLCODE != SQL_RC_OK) {
memset(SQLUDF_STATE, "38XXX", SQLUDF_SQLSTATE_LEN);
sprintf(SQLUDF_MSGTX, "Error code %d encountered",
rc == SQL_RC_OK ? SQLCODE : rc);
goto cleanup;
}

cleanup:
return SQLZ_DISCONNECT_PROC;
}
---------------------------------------------------
And to register the procedure in the database, run the following SQL
statement:
---------------------------------------------------
CREATE PROCEDURE runstats(IN tableName VARCHAR(256))
SPECIFIC runstats
DYNAMIC RESULT SETS 0
MODIFIES SQL DATA
NOT DETERMINISTIC
CALLED ON NULL INPUT
LANGUAGE C
EXTERNAL NAME 'runstats_proc!runstats_proc'
NOT FENCED THREADSAFE
NO EXTERNAL ACTION
PARAMETER STYLE SQL;
---------------------------------------------------

Then I ran the procedure on an existing table:

db2 "call runstats('stolze.a')"

and verified that the statistics were indeed updated:
---------------------------------------------------
$ db2 "select * from sysstat.tables where tabname in ('A', 'B')"

TABSCHEMA TABNAME CARD NPAGES FPAGES OVERFLOW CLUSTERED ACTIVE_BLOCKS
--------- ------- ---- ------ ------ -------- --------- -------------
STOLZE A 0 0 1 0 - 0
STOLZE B -1 -1 -1 -1 - -1

2 record(s) selected.
You might also want to read up on the "db2Runstats" API. For example, that
API causes a few locks to be taken and it is suggested that you run a
COMMIT after the API call. The current procedure code lets the client
application do the commit.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #8
Knut Stolze wrote:
Spencer wrote:
Sure if you want to code it for me that would be great! So if I am
not writing it in SQL/PL then I suppose it is C? Any primers in
writing stored proc's for DB2. I have not written any for DB2.


Sorry that it took so long...

Here is the procedure code. It is pretty basic and does not contain any
serious error handling. But you should be able to build on that.
--------------------------------------
#include <sql.h>
#include <sqludf.h>
#include <db2ApiDf.h>

int runstats_proc(
SQLUDF_VARCHAR *tableName,
SQLUDF_NULLIND *tableName_ind,
SQLUDF_TRAIL_ARGS)
{
SQL_API_RC rc = SQL_RC_OK;
db2RunstatsData data;
struct sqlca sqlca;

if (SQLUDF_NULL(tableName_ind)) {
goto cleanup;
}

/* initialize data structures */
data.iSamplingOption = 0;
data.piTablename = (unsigned char *)tableName;
data.piColumnList = NULL;
data.piColumnDistributionList = NULL;
data.piColumnGroupList = NULL;
data.piIndexList = NULL;
data.iRunstatsFlags = DB2RUNSTATS_ALL_COLUMNS |
DB2RUNSTATS_DISTRIBUTION | DB2RUNSTATS_ALL_INDEXES |
DB2RUNSTATS_EXT_INDEX | DB2RUNSTATS_ALLOW_READ;
data.iNumColumns = 0;
data.iNumColdist = 0;
data.iNumColGroups = 0;
data.iNumIndexes = 0;
data.iParallelismOption = 0;
data.iTableDefaultFreqValues = -1; /* use default */
data.iTableDefaultQuantiles = -1; /* use default */
data.iUtilImpactPriority = 0;
data.iSamplingRepeatable = 0; /* unused */
memset(&sqlca, 0x00, sizeof sqlca);

rc = db2Runstats(db2Version820, &data, &sqlca);
if (rc != SQL_RC_OK || SQLCODE != SQL_RC_OK) {
memset(SQLUDF_STATE, "38XXX", SQLUDF_SQLSTATE_LEN);
sprintf(SQLUDF_MSGTX, "Error code %d encountered",
rc == SQL_RC_OK ? SQLCODE : rc);
goto cleanup;
}

cleanup:
return SQLZ_DISCONNECT_PROC;
}
---------------------------------------------------

Oh, I forgot: you can compile the procedure using the bldrtn script from
sqllib/samples/c

cd sqllib/samples/c
../bldrtn runstats_proc

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #9
Hello Knut;
I just wanted to thank you for this sample. We have successfully
implemented it and it opens up lots of potential for API-related procs.
So - thanks!

Pete H
Knut Stolze wrote:
Knut Stolze wrote:
Spencer wrote:
Sure if you want to code it for me that would be great! So if I am not writing it in SQL/PL then I suppose it is C? Any primers in
writing stored proc's for DB2. I have not written any for DB2.
Sorry that it took so long...

Here is the procedure code. It is pretty basic and does not contain any serious error handling. But you should be able to build on that.
--------------------------------------
#include <sql.h>
#include <sqludf.h>
#include <db2ApiDf.h>

int runstats_proc(
SQLUDF_VARCHAR *tableName,
SQLUDF_NULLIND *tableName_ind,
SQLUDF_TRAIL_ARGS)
{
SQL_API_RC rc = SQL_RC_OK;
db2RunstatsData data;
struct sqlca sqlca;

if (SQLUDF_NULL(tableName_ind)) {
goto cleanup;
}

/* initialize data structures */
data.iSamplingOption = 0;
data.piTablename = (unsigned char *)tableName;
data.piColumnList = NULL;
data.piColumnDistributionList = NULL;
data.piColumnGroupList = NULL;
data.piIndexList = NULL;
data.iRunstatsFlags = DB2RUNSTATS_ALL_COLUMNS |
DB2RUNSTATS_DISTRIBUTION | DB2RUNSTATS_ALL_INDEXES |
DB2RUNSTATS_EXT_INDEX | DB2RUNSTATS_ALLOW_READ;
data.iNumColumns = 0;
data.iNumColdist = 0;
data.iNumColGroups = 0;
data.iNumIndexes = 0;
data.iParallelismOption = 0;
data.iTableDefaultFreqValues = -1; /* use default */
data.iTableDefaultQuantiles = -1; /* use default */
data.iUtilImpactPriority = 0;
data.iSamplingRepeatable = 0; /* unused */
memset(&sqlca, 0x00, sizeof sqlca);

rc = db2Runstats(db2Version820, &data, &sqlca);
if (rc != SQL_RC_OK || SQLCODE != SQL_RC_OK) {
memset(SQLUDF_STATE, "38XXX", SQLUDF_SQLSTATE_LEN);
sprintf(SQLUDF_MSGTX, "Error code %d encountered",
rc == SQL_RC_OK ? SQLCODE : rc);
goto cleanup;
}

cleanup:
return SQLZ_DISCONNECT_PROC;
}
---------------------------------------------------

Oh, I forgot: you can compile the procedure using the bldrtn script

from sqllib/samples/c

cd sqllib/samples/c
./bldrtn runstats_proc

--
Knut Stolze
Information Integration
IBM Germany / University of Jena


Nov 12 '05 #10
peteh wrote:
Hello Knut;
I just wanted to thank you for this sample. We have successfully
implemented it and it opens up lots of potential for API-related procs.
So - thanks!

Pete H
Knut Stolze wrote:
Knut Stolze wrote:

Spencer wrote:
Sure if you want to code it for me that would be great! So if I
am
not writing it in SQL/PL then I suppose it is C? Any primers in
writing stored proc's for DB2. I have not written any for DB2.

Sorry that it took so long...

Here is the procedure code. It is pretty basic and does not
contain any
serious error handling. But you should be able to build on that.
--------------------------------------
#include <sql.h>
#include <sqludf.h>
#include <db2ApiDf.h>

int runstats_proc(
SQLUDF_VARCHAR *tableName,
SQLUDF_NULLIND *tableName_ind,
SQLUDF_TRAIL_ARGS)
{
SQL_API_RC rc = SQL_RC_OK;
db2RunstatsData data;
struct sqlca sqlca;

if (SQLUDF_NULL(tableName_ind)) {
goto cleanup;
}

/* initialize data structures */
data.iSamplingOption = 0;
data.piTablename = (unsigned char *)tableName;
data.piColumnList = NULL;
data.piColumnDistributionList = NULL;
data.piColumnGroupList = NULL;
data.piIndexList = NULL;
data.iRunstatsFlags = DB2RUNSTATS_ALL_COLUMNS |
DB2RUNSTATS_DISTRIBUTION | DB2RUNSTATS_ALL_INDEXES |
DB2RUNSTATS_EXT_INDEX | DB2RUNSTATS_ALLOW_READ;
data.iNumColumns = 0;
data.iNumColdist = 0;
data.iNumColGroups = 0;
data.iNumIndexes = 0;
data.iParallelismOption = 0;
data.iTableDefaultFreqValues = -1; /* use default */
data.iTableDefaultQuantiles = -1; /* use default */
data.iUtilImpactPriority = 0;
data.iSamplingRepeatable = 0; /* unused */
memset(&sqlca, 0x00, sizeof sqlca);

rc = db2Runstats(db2Version820, &data, &sqlca);
if (rc != SQL_RC_OK || SQLCODE != SQL_RC_OK) {
memset(SQLUDF_STATE, "38XXX", SQLUDF_SQLSTATE_LEN);
sprintf(SQLUDF_MSGTX, "Error code %d encountered",
rc == SQL_RC_OK ? SQLCODE : rc);
goto cleanup;
}

cleanup:
return SQLZ_DISCONNECT_PROC;
}
---------------------------------------------------


Oh, I forgot: you can compile the procedure using the bldrtn script


from
sqllib/samples/c

cd sqllib/samples/c
./bldrtn runstats_proc

--
Knut Stolze
Information Integration
IBM Germany / University of Jena


Knut,

have you placed this on developerWorks? Also do you have an example for
LOAD? Wrappers for the LOAD and runstats API are probably FAQ.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #11
Serge Rielau wrote:
Knut,

have you placed this on developerWorks? Also do you have an example for
LOAD? Wrappers for the LOAD and runstats API are probably FAQ.


Serge, it's on my TODO list. Actually, I want to finish this and a few
other articles by the end of this month. I'll include some examples and
code for LOAD as well, no big deal. ;-)

If you (or anyone else) has some other wishes of APIs that should be made
available via SQL and are not yet natively, just let us know.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jan Arickx | last post by:
My situation is as follows. I have several big SQL queries in a data warehouse using 1 big fact tables and 10 dimension tables. The queries join all of them together but the optimizer doesn't care...
7
by: Todd McNeill | last post by:
Hi- Something was just pointed out to me this morning. According to the V8 Command Reference, the RUNSTATS command no longer uses the SHRLEVEL CHANGE/REFERENCE clauses, and it looks to be...
3
by: Gilda | last post by:
Does anyone know what entity (utility?) checks if statistics collections are required, if the Automatic RUNSTATS is enabled, in version 8.2? How often are these checks done? Thanks.
1
by: raqfg | last post by:
Hi. I am trying to test out the automatic maintanance with notification feature of DB2 v8.2. I have enabled the auto maint with notification. The problem I am facing is that I only get email...
5
by: Jari Korkiakoski | last post by:
Hello, What kind of difference does it make to update statistics via 'REORGCHK UPDATE STATISTICS ON TABLE ALL' or by running directly 'runstats on table xxx.yyy with distribution and detailed...
4
by: kenfar | last post by:
I've got a large table on db2 8.2.1 that I rarely perform runstats on. It has about 600 million rows organized in a single MDC time dimension on a non-dpf warehouse. Anyhow, we recently ran...
4
by: db2udb | last post by:
Hi, I have just taken over as the DBA for a database that has not had any reorgs/runstats run against it for the last three years. As a first step, I have just run reorg/runstats against the system...
3
by: Norm | last post by:
If RUNSTATS is called directly, we can specify UTIL_IMPACT_PRIORITY to limit CPU consumption. For automatic runstats, there is no place to specify this paramater. Will it use the DBM value of...
1
by: satish mullapudi | last post by:
Hi Am using DB2 UDB ESE on AIX platform. I used to take a full/ incremental online backup regularly. This process used to go on since years. But recently, whenever am trying to take the same, am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.