in reply to Perl DBI
This way you should only need to fetch n rows from the database -- where n is the amount of rows you have in KPI_Master -- with only a single round-trip. This should also simplify your code to something like this:SELECT m.*, coalesce(sb.somecol, sd.somecol, tb.somecol, td.somecol) AS someco +l FROM KPI_Master m LEFT JOIN SDCCHBLOCKING sb ON m.NE = sb.NE AND m.KPI = 'SDCCHBLOCKING' LEFT JOIN SDCCHDROP sd ON m.NE = sd.NE AND m.KPI = 'SDCCHDROP' LEFT JOIN TCHBLOCKING tb ON m.NE = tb.NE AND m.KPI = 'TCHBLOCKING' LEFT JOIN TCHDROP td ON m.NE = td.NE AND m.KPI = 'TCHDROP'
my $dbh = DBI->connect('dbi:ODBC:driver=Microsoft Access Driver (*.mdb +, *.accdb);dbq=D:\Project\Project\BSC_KPIMonitoring\CHN\E_BSC_KPIMoni +torDB.mdb', undef, undef, { RaiseError => 1}); my $query = <<EOF; SELECT m.BSC, m.KPI, m.THRESH, coalesce(sb.somecol, sd.somecol, tb.somecol, td.somecol) AS someco +l FROM KPI_Master m LEFT JOIN SDCCHBLOCKING sb ON m.NE = sb.NE AND m.KPI = 'SDCCHBLOCKING' LEFT JOIN SDCCHDROP sd ON m.NE = sd.NE AND m.KPI = 'SDCCHDROP' LEFT JOIN TCHBLOCKING tb ON m.NE = tb.NE AND m.KPI = 'TCHBLOCKING' LEFT JOIN TCHDROP td ON m.NE = td.NE AND m.KPI = 'TCHDROP' EOF my %kpis = (SDCCHBLOCKING => 'SB', SDCCHDROP => 'SD', TCHBLOCKING => 'TD', TCHDROP => 'TD'); my $sth = $dbh->prepare($query); $sth->execute; while (my @row = $sth->fetchrow_array()) { my ($bsc, $kpi, $thresh, $somecol) = @$row; if ($somecol > $thresh) { print join(",", $kpis{$kpi}, $bsc, $thresh, "/$somecol"); } }
Other points of interest for performance might be reducing the amount of columns you get: don't SELECT *, but SELECT theOneColumnYouNeed
|
|---|