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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.