php – Very heavy queries

Question

I have a page where I need to fetch some data from the database. What I currently is doing is a number of functions each running a query with multiple terms – in other words the queries is runned lots of times…
This is quite server-heavy, and I’ve allocated 512M memory to the site in question. A site that without that page can run on 64M…

Can anyone help me “trim” my scripts?

<?php
function antall_ta5_mnd($avdeling = ALL, $korr="0", $mnd = 'm')
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') ,
                'month' => date($mnd) - $korr,
            ) ,
        ) ,
    );
    $faktisk_mnd = date('m');
    $posts = get_posts($posts);
    $telte = count($posts);
    if (($telte < 1) && ($mnd > $faktisk_mnd))
    {
        return NULL;
    }
    else
    {
        return $telte;
    }
}

function antall_deltakere_mnd($avdeling = ALL, $korr="0", $mnd = 'm')
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') ,
                'month' => date($mnd) - $korr,
            ) ,
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $words = get_posts($posts);
    if ($words)
    {
        foreach ($words as $word)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $count = count($meta);
            $totalcount = $count;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return $totalcount = 0;
    }
}

function antall_ta5_uke($avdeling, $korr="0", $uke="W")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') ,
                'week' => date($uke) - $korr,
            ) ,
        ) ,
    );
    $faktisk_uke = date('W');
    $posts = get_posts($posts);
    $telte = count($posts);
    if (($telte < 1) && ($uke > $faktisk_uke))
    {
        return NULL;
    }
    else
    {
        return $telte;
    }
}
function antall_deltakere_uke($avdeling = ALL, $korr="0", $uke="W")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') ,
                'week' => date($uke) - $korr,
            ) ,
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $words = get_posts($posts);
    if ($words)
    {
        foreach ($words as $word)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $count = count($meta);
            $totalcount = $count;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return 0;
    }
}
function antall_ta5_dager($avdeling, $dager="0")
{
$d = new DateTime(strtotime( "Y-m-d" ));
$d->modify( $dager .' day ago' );
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year'    => $d->format( 'Y' ),
                'month'   => $d->format( 'm' ),
                'day'     => $d->format( 'd' ),
            ),
        ) ,
    );

    $posts = get_posts($posts);
    $telte = count($posts);
    return $telte;
}
function antall_deltakere_dager($avdeling, $dager="0")
{
$d = new DateTime(strtotime( "Y-m-d" ));
$d->modify( $dager .' day ago' );
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year'    => $d->format( 'Y' ),
                'month'   => $d->format( 'm' ),
                'day'     => $d->format( 'd' ),
            ),
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $words = get_posts($posts);
    if ($words)
    {
        foreach ($words as $word)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $count = count($meta);
            $totalcount = $count;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return $totalcount = 0;
    }
}
function antall_ta5_ar($avdeling, $korr="0")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') - $korr,
            ) ,
        ) ,
    );
    $posts = get_posts($posts);
    return count($posts);
}

function antall_deltakere_ar($avdeling = ALL, $korr="0")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'year' => date('Y') - $korr,
            ) ,
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $words = get_posts($posts);
    if ($words)
    {
        foreach ($words as $word)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $count = count($meta);
            $totalcount = $count;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return $totalcount = 0;
    }
}

function antall_ta5_tot($avdeling)
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
    );
    $posts = get_posts($posts);
    return count($posts);
}

function antall_deltakere_tot($avdeling = ALL)
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'field' => 'slug',
                'terms' => $avdeling
            ) ,
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $words = get_posts($posts);
    if ($words)
    {
        foreach ($words as $word)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $count = count($meta);
            $totalcount = $count;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return $totalcount = 0;
    }
}
?>

What I’m using for output:

<?php
$tot_denne_uken = antall_ta5_uke($avdeling) + antall_deltakere_uke($avdeling);
$tot_forrige_uke = antall_ta5_uke($avdeling,'1') + antall_deltakere_uke($avdeling,'1');
$tot_denne_mnd = antall_ta5_mnd($avdeling) + antall_deltakere_mnd($avdeling);
$tot_forrige_mnd = antall_ta5_mnd($avdeling,'1') + antall_deltakere_mnd($avdeling,'1');
$tot_dette_ar = antall_ta5_ar($avdeling) + antall_deltakere_ar($avdeling);
$tot_forrige_ar = antall_ta5_ar($avdeling,'1') + antall_deltakere_ar($avdeling,'1');
$tot = antall_ta5_tot($avdeling) + antall_deltakere_tot($avdeling);

    echo "<p>Hittil denne uken: ". antall_ta5_uke($avdeling) ." (". $tot_denne_uken .")</p>";
    echo "<p>Forrige uke: ". antall_ta5_uke($avdeling,'1') ." (". $tot_forrige_uke .")</p>";
    echo "<p>Hittil denne måned: ". antall_ta5_mnd($avdeling) ." (". $tot_denne_mnd .")</p>";
    echo "<p>Forrige måned: ". antall_ta5_mnd($avdeling,'1') ." (". $tot_forrige_mnd .")</p>";
    echo "<p>Hittil i år: ". antall_ta5_ar($avdeling) ." (". $tot_dette_ar .")</p>";
    echo "<p>I fjor: ". antall_ta5_ar($avdeling,'1') ." (". $tot_forrige_ar .")</p>";
    echo "<p>Totalt: ". antall_ta5_tot($avdeling) ." (". $tot .")</p>";
?>

0
TerjeB 1 month 2022-10-24T13:35:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse