<?xml version="1.0" encoding="windows-1252"?>
<node id="929567" title="For a better efficiency and speed question!" created="2011-10-04 11:04:48" updated="2011-10-04 11:04:48">
<type id="115">
perlquestion</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
Hi Monks!&lt;br&gt;

I am having speed issues with this code. I put together this sample code so I can get 
some suggestions. The block of commented out code is the explicit way of the code right 
after it. I am suspecting that the line &lt;br&gt;
&lt;code&gt;my $control_num = join ( ',', ('?') x @control );&lt;/code&gt; &lt;br&gt;
where I am joining all the control numbers from the array "@control" is
slowing down the query results. I wonder if anyone here would have a suggestion on how
I could improve( speed up) this code or query if someone has done something similar.&lt;br&gt;
Thanks for looking!!!

&lt;code&gt;
#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

$| = 1; # Disable buffering

my $q = new CGI;

my $db = 'TESTDB';
my $dbh = test::test-&gt;connect( $db );

print "\nTesting\n\n";

=code
my $sql = $dbh-&gt;exec_select("
SELECT acc, name,date
FROM mytable
WHERE mame&lt;&gt;''
AND ((date + 1 YEAR) &gt;= (CURRENT_DATE))
AND ((date + 1 YEAR) &lt; (CURRENT_DATE + 240 DAYS))
AND controlnum IN ('12345','8877788','23334455','223445988','222339000','2222333','223475699','22339668','2223999','009999','11220000','77766','88776'
,'9988888','11223756','22388577','9998888','998888','77665555','9999888','88877777','666555555','88777766666','112223333','9998877','9988877'
,'888777666666','00988777','887777654')
and date between '2010-11-11' and '2011-11-11' order by date desc");
=cut

#=code
my @control = qw(12345 8877788 23334455 223445988 222339000 2222333 223475699 22339668 2223999 009999 11220000 77766 88776 
                 9988888 11223756 22388577 9998888 998888 77665555 9999888 88877777 666555555 88777766666 112223333 9998877 9988877 888777666666 00988777 887777654);

my $control_num = join ( ',', ('?') x @control );

my $sql = $dbh-&gt;exec_select( "SELECT acc, name,date
                                          FROM mytable 
                              WHERE mame&lt;&gt;''
                              AND ((date + 1 YEAR) &gt;= (CURRENT_DATE))
                              AND ((date + 1 YEAR) &lt; (CURRENT_DATE + 240 DAYS))
                              AND controlnum  IN ($control_num) 
                              and date between '2010-11-11' and '2011-11-11' order by date desc
                              ", @control);
#=cut


if(@$sql)
  {
    for(my $i = 0; $i &lt; @$sql; $i++)
     {

       my $acc  = $sql-&gt;[$i]{'acc'} || '';
       my $name = $sql-&gt;[$i]{'name'} || '';
       my $date = $sql-&gt;[$i]{'date'} || '';
       
       print "\n$acc - $name - $date\n";

     }


}

&lt;/code&gt;

</field>
<field name="reputation">
3</field>
</data>
</node>
