Monday, October 24, 2022
HomeWordPress Developmentphp - Very heavy queries

php – Very heavy queries


I’ve a web page the place I have to fetch some information from the database. What I at present is doing is a lot of capabilities every working a question with a number of phrases – in different phrases the queries is runned a number of occasions…
That is fairly server-heavy, and I’ve allotted 512M reminiscence to the positioning in query. A web site that with out that web page can run on 64M…

Can anybody assist me “trim” my scripts?

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

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

        wp_reset_postdata();

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

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

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return 0;
    }
}
operate antall_ta5_dager($avdeling, $dager="0")
{
$d = new DateTime(strtotime( "Y-m-d" ));
$d->modify( $dager .' day in the past' );
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'subject' => 'slug',
                'phrases' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'yr'    => $d->format( 'Y' ),
                'month'   => $d->format( 'm' ),
                'day'     => $d->format( 'd' ),
            ),
        ) ,
    );

    $posts = get_posts($posts);
    $telte = rely($posts);
    return $telte;
}
operate antall_deltakere_dager($avdeling, $dager="0")
{
$d = new DateTime(strtotime( "Y-m-d" ));
$d->modify( $dager .' day in the past' );
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'subject' => 'slug',
                'phrases' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'yr'    => $d->format( 'Y' ),
                'month'   => $d->format( 'm' ),
                'day'     => $d->format( 'd' ),
            ),
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $phrases = get_posts($posts);
    if ($phrases)
    {
        foreach ($phrases as $phrase)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $rely = rely($meta);
            $totalcount = $rely;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

        return array_sum($array);
    }
    else
    {
        return $totalcount = 0;
    }
}
operate antall_ta5_ar($avdeling, $korr="0")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'subject' => 'slug',
                'phrases' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'yr' => date('Y') - $korr,
            ) ,
        ) ,
    );
    $posts = get_posts($posts);
    return rely($posts);
}

operate antall_deltakere_ar($avdeling = ALL, $korr="0")
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'subject' => 'slug',
                'phrases' => $avdeling
            ) ,
        ) ,
        'date_query' => array(
            array(
                'yr' => date('Y') - $korr,
            ) ,
        ) ,
        'meta_query' => array(
            array(
                'key' => 'meta_deltakere',
            ) ,
        ) ,
    );
    $phrases = get_posts($posts);
    if ($phrases)
    {
        foreach ($phrases as $phrase)
        {
            $meta = strip_tags($word->meta_deltakere);
            if(!empty($meta)) {
            $meta = explode(",", $meta);
            $rely = rely($meta);
            $totalcount = $rely;
            } else {
            $totalcount="0";
            }
            $array[] = $totalcount;
        }

        wp_reset_postdata();

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

operate antall_ta5_tot($avdeling)
{
    $posts = array(
        'post_type' => 'ta5',
        'numberposts' => - 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'ta5_avdeling',
                'subject' => 'slug',
                'phrases' => $avdeling
            ) ,
        ) ,
    );
    $posts = get_posts($posts);
    return rely($posts);
}

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

        wp_reset_postdata();

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

What I am utilizing 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>";
?>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments