I have a sqlite table that has two columns: COL_A and COL_B (both integer). Now i want to select some records where col_a has values from one large (hundreds, maybe few thousands) set and col_b has values from another large set:
SELECT *
FROM my_table
WHERE
col_1 IN (<large set of integers>) AND
col_2 IN (<another large set of integers>)
This will result in error:
DBD::SQLite::db prepare_cached failed: too many SQL variables
My attempt to go around this problem resulted in this code.
use List::MoreUtils qw/natatime/;
sub select_in_chunks {
my ($self, $set_1, $set_2) = @_;
my @ret;
my $chunk_size = 450;
my $it1 = natatime $chunk_size, @$set_1;
while (my @a = $it1->()) {
my $it2 = natatime $chunk_size, @$set_2;
while (my @b = $it2->()) {
# previous query with subsets
my $records = $self->query(\@a, \@b)
push @ret, @$records;
}
}
return \@ret;
}
This actually works but i wonder if there is a more convenient (and faster!) way to do selects with large IN statements. I can't use BETWEEN operator since those values aren't continuous.
Any ideas?
-
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.