SQLite is a database, and you need to define indexes for columns that you will be selecting. (You may want to read a good database book for examples of when, and when NOT to use an index... for that matter, when you want to use a database and when to use a delimited text file...) Adding an index improves performace, at least at select time. Not tested for replace time, but I leave that as an exercise to the user...
Example code:
#!/usr/local/bin/perl
use strict;
use DBI;
use DBD::SQLite;
use Benchmark;
my $dbh = DBI->connect("dbi:SQLite:dbname=testdb","","");
print "Connected to Database OK.\n";
print "Creating Tables && INSERTING\n";
my $sql = qq{CREATE TABLE A (A INT, B VAL)};
my $sth = $dbh->prepare($sql);
$sth->execute();
my $i=0;
while ($i<10000){
my $sql = qq{INSERT INTO A(A,B) VALUES (?,?)};
my $sth = $dbh->prepare($sql);
$sth->execute($i,$i);
$i++;
}
print "DONE\n";
sub doSelect{
# Yes, I could pre cache the query, but this won't affect the result
+s
my $sql = qq{SELECT A FROM A WHERE B = ?};
my $sth = $dbh->prepare($sql);
my $i =int( rand 10000);
$sth->execute($i);
my @ary = $sth->fetchrow_array();
}
timethese (10000,{'MethodOne'=>'&doSelect'});
my $sql2 = qq{CREATE INDEX FOO ON A(B)};
my $sth2 = $dbh->prepare($sql2);
$sth2->execute();
timethese (10000,{'MethodOne'=>'&doSelect'});
Results:
Connected to Database OK.
Creating Tables && INSERTING
DONE
Benchmark: timing 10000 iterations of MethodOne...
MethodOne: 100 wallclock secs (71.48 usr + 18.18 sys = 89.66 CPU) @ 1
+11.53/s (n=10000)
Benchmark: timing 10000 iterations of MethodOne...
MethodOne: 5 wallclock secs ( 2.83 usr + 1.11 sys = 3.94 CPU) @ 25
+38.07/s (n=10000)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.