16.10.2015 Views

Getting Started with DB2 Express-C

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 – Maintenance Tasks 207<br />

Example:<br />

RUNSTATS ON TABLE myschema.employee<br />

12.1.3 BIND / REBIND<br />

After successfully running a RUNSTATS command, not all queries will use the latest<br />

statistics. Static SQL access plans are determined when you first issue a BIND command,<br />

so the statistics used at that time may not be the same as the current ones. Figure 12.2<br />

helps illustrate this idea.<br />

Figure 12.2 – Static SQL bind process<br />

In Figure 12.2 an embedded C program (stored as a file <strong>with</strong> a “sqc” extension) is<br />

precompiled. After pre-compilation, two files are generated, a “.c” file containing the C code<br />

<strong>with</strong> all the SQL commented out; and a “.bnd” file containing all the SQL statements. The C<br />

file <strong>with</strong> the “.c” extension is compiled as usual <strong>with</strong> a C compiler, creating a “library” as<br />

shown in the top right hand side of the figure. The “.bnd” file is similarly bound, generating<br />

a package that is stored in the database. Binding is equivalent to compiling the SQL<br />

statements where the best access plan is determined based on the statistics available at<br />

the time, and then storing them in the package.<br />

Now, what happens if 1 million rows are inserted into a table used in the SQL for this<br />

embedded C program? After the insertion, if a RUNSTATS is performed, the statistics will<br />

be updated; however the package will not be automatically updated to recalculate the<br />

access path based on the latest statistics. The db2rbind command can be used to rebind<br />

all the existing packages to take into account the latest stats.<br />

Syntax:<br />

db2rbind database_alias -l

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!