/usr/share/postgresql-9.1-postgis/utils/test_geography_joinestimation.pl is in postgresql-9.1-postgis 1.5.3-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | #!/usr/bin/perl -w
# $Id: test_joinestimation.pl 1633 2005-04-18 13:50:14Z strk $
#
# TODO:
#
# Apply oid filters on table2
#
use Pg;
$VERBOSE = 0;
sub usage
{
local($me) = `basename $0`;
chop($me);
print STDERR "$me [-v] [-vacuum] <table1> <table2> [<col1>] [<col2>]\n";
}
$TABLE1='';
$TABLE2='';
$COLUMN1='';
$COLUMN2='';
for ($i=0; $i<@ARGV; $i++)
{
if ( $ARGV[$i] =~ m/^-/ )
{
if ( $ARGV[$i] eq '-v' )
{
$VERBOSE++;
}
elsif ( $ARGV[$i] eq '-vacuum' )
{
$VACUUM=1;
}
else
{
print STDERR "Unknown option $ARGV[$i]:\n";
usage();
exit(1);
}
}
elsif ( ! $TABLE1 )
{
$TABLE1 = $ARGV[$i];
}
elsif ( ! $TABLE2 )
{
$TABLE2 = $ARGV[$i];
}
elsif ( ! $COLUMN1 )
{
$COLUMN1 = $ARGV[$i];
}
elsif ( ! $COLUMN2 )
{
$COLUMN2 = $ARGV[$i];
}
else
{
print STDERR "Too many options:\n";
usage();
exit(1);
}
}
if ( ! $TABLE1 || ! $TABLE2 )
{
usage();
exit 1;
}
$SCHEMA1 = 'public';
$COLUMN1 = 'the_geom' if ( $COLUMN1 eq '' );
if ( $TABLE1 =~ /(.*)\.(.*)/ )
{
$SCHEMA1 = $1;
$TABLE1 = $2;
}
$SCHEMA2 = 'public';
$COLUMN2 = 'the_geom' if ( $COLUMN2 eq '' );
if ( $TABLE2 =~ /(.*)\.(.*)/ )
{
$SCHEMA2 = $1;
$TABLE2 = $2;
}
#connect
$conn = Pg::connectdb("");
if ( $conn->status != PGRES_CONNECTION_OK ) {
print STDERR $conn->errorMessage;
exit(1);
}
if ( $VERBOSE )
{
print "Table1: \"$SCHEMA1\".\"$TABLE1\".\"$COLUMN1\"\n";
print "Table2: \"$SCHEMA2\".\"$TABLE2\".\"$COLUMN2\"\n";
}
# Get geometry types
#$TYPE1 = get_geometry_type($SCHEMA1, $TABLE1, $COLUMN1);
#$TYPE2 = get_geometry_type($SCHEMA2, $TABLE2, $COLUMN2);
# vacuum analyze table
if ( $VACUUM )
{
print "VACUUM ANALYZE\n";
vacuum($SCHEMA1, $TABLE1);
vacuum($SCHEMA2, $TABLE2);
}
# get number of features from pg_class.ntuples
# (correct if vacuum have been run after last insertion/deletions)
$NROWS1 = get_num_rows($SCHEMA1, $TABLE1);
$NROWS2 = get_num_rows($SCHEMA2, $TABLE2);
$TOTROWS = $NROWS1*$NROWS2;
# Test join selectivity
($est,$real) = test_join();
$delta = $est-$real;
$error = $delta/$TOTROWS;
$error = int(($error)*10000)/100;
#print " Types: $TYPE1 - $TYPE2\n";
print " Rows: $NROWS1 x $NROWS2 = $TOTROWS\n";
print " Estimated: ".$est."\n";
print " Real: ".$real."\n";
print " Error: ".$error."%\n";
##################################################################
sub print_extent
{
local($ext) = shift;
local($s);
$s = $ext->{'xmin'}." ".$ext->{'ymin'}." ";
$s .= $ext->{'xmax'}." ".$ext->{'ymax'};
return $s;
}
sub split_extent
{
local($ext) = shift;
local($bps) = shift;
local($width, $height, $cell_width, $cell_height);
local($x,$y);
local(@stack);
$width = $ext->{'xmax'} - $ext->{'xmin'};
$height = $ext->{'ymax'} - $ext->{'ymin'};
$cell_width = $width / $bps;
$cell_height = $height / $bps;
if ($VERBOSE)
{
print "cell_w: $cell_width\n";
print "cell_h: $cell_height\n";
}
@stack = ();
for ($x=0; $x<$bps; $x++)
{
for($y=0; $y<$bps; $y++)
{
local(%cell);
$cell{'xmin'} = $ext->{'xmin'}+$x*$cell_width;
$cell{'ymin'} = $ext->{'ymin'}+$y*$cell_height;
$cell{'xmax'} = $ext->{'xmin'}+($x+1)*$cell_width;
$cell{'ymax'} = $ext->{'ymin'}+($y+1)*$cell_height;
print "cell: ".print_extent(\%cell)."\n" if ($VERBOSE);
push(@stack, \%cell);
}
}
return @stack;
}
sub test_join
{
local($ext) = shift;
# Test whole extent query
$query = 'explain analyze select count(1) from "'.
$SCHEMA1.'"."'.$TABLE1.'" t1, "'.$SCHEMA2.'"."'.$TABLE2.
'" t2 WHERE t1."'.$COLUMN1.'" && '.
' t2."'.$COLUMN2.'"';
print $query."\n";
$res = $conn->exec($query);
if ( $res->resultStatus != PGRES_TUPLES_OK ) {
print STDERR $conn->errorMessage;
exit(1);
}
while ( ($row=$res->fetchrow) )
{
if ( ($row =~ /.* rows=([0-9]+) .* rows=([0-9]+) /) && ! ($row =~ /.*Aggregate.*/) )
{
$est = $1;
$real = $2;
last;
}
}
return ($est,$real);
}
sub get_geometry_type
{
my $schema = shift;
my $table = shift;
my $col = shift;
my $query = 'select distinct geometrytype("'.$col.'") from "'.$schema.'"."'.$table.'"';
my $res = $conn->exec($query);
if ( $res->resultStatus != PGRES_TUPLES_OK ) {
print STDERR $conn->errorMessage;
exit(1);
}
if ( $res->ntuples() > 1 ) {
print STDERR "Mixed geometry types in \"$SCHEMA1\".\"$TABLE1\".\"$COLUMN1\"\n";
exit(1);
}
return $res->getvalue(0, 0);
}
sub vacuum
{
my $SCHEMA = shift;
my $TABLE = shift;
my $query = 'vacuum analyze "'.$SCHEMA.'"."'.$TABLE.'"';
my $res = $conn->exec($query);
if ( $res->resultStatus != PGRES_COMMAND_OK ) {
print STDERR $conn->errorMessage;
exit(1);
}
}
sub get_num_rows
{
my $SCHEMA = shift;
my $TABLE = shift;
my $query = 'SELECT c.reltuples FROM pg_class c, pg_namespace n '.
"WHERE c.relnamespace = n.oid AND n.nspname = '$SCHEMA' ".
" AND c.relname = '$TABLE'";
my $res = $conn->exec($query);
if ( $res->resultStatus != PGRES_TUPLES_OK ) {
print STDERR $conn->errorMessage;
exit(1);
}
return $res->getvalue(0, 0);
}
#
# $Log$
# Revision 1.3 2005/04/18 13:50:14 strk
# Fixed bug in table2 schema parsing.
#
# Revision 1.2 2004/12/23 14:48:25 strk
# Fixed help string, and added a TODO item
#
# Revision 1.1 2004/12/22 17:02:17 strk
# initial revision
#
# Revision 1.8 2004/03/08 17:21:57 strk
# changed error computation code to delta/totrows
#
# Revision 1.7 2004/03/06 18:02:48 strk
# Comma-separated bps values accepted
#
# Revision 1.6 2004/03/05 21:06:04 strk
# Added -vacuum switch
#
# Revision 1.5 2004/03/05 21:03:18 strk
# Made the -bps switch specify the exact level(s) at which to run the test
#
# Revision 1.4 2004/03/05 16:40:30 strk
# rewritten split_extent to be more datatype-conservative
#
# Revision 1.3 2004/03/05 16:01:02 strk
# added -bps switch to set maximun query level. reworked command line parsing
#
# Revision 1.2 2004/03/05 15:29:35 strk
# more verbose output
#
# Revision 1.1 2004/03/05 11:52:24 strk
# initial import
#
#
|