Как можно оптимизировать sql запросы?

149
05 апреля 2018, 12:18

Дело в том что при выборе department_id пустым то есть всех , то код срабатывает а при том что если имеется department_id то загрузка идет и выдает ошибку 502 , поменял хостинг на VPS изменил php.ini данные но не помогло . Раньше при малых данных работала .

 if(isset($_POST["action"]) and $_POST["action"] == "search" )
    {
        $department_id = $_POST["department"];
        $report_for = $_POST["report_for"];
        $start_date = $_POST["start_date"];
        $end_date = $_POST["end_date"];
        $i = 1;
    }else{
        $start_date=date("Y-m-d");
        $end_date=date("Y-m-d");
        $department_id=($_SESSION['user_data']['department_type']==1)?"a":$_SESSION['user_data']['department_id'];
        $report_for=1;
        $i = 0;
    }
        if($report_for==1)
        {
            $where1=$where2="";
            if(strchr($department_id , "a")=="")
            {
                $where1="and department_id in(".$department_id.")";
                $where2="and from_department_id in(".$department_id.")";
            }
            $sql = " SELECT cl.id,concat(cl.name,' ',cl.surname) name,ld.name department_name,
                    (IFNULL((select sum(price) from game_sales where game_status_id=3 and client_id=cl.id and date(date)<'".$start_date."' and stadium_id in(SELECT id from stadium where data_status=1 ".$where1." )),0)  +
                    IFNULL((select sum(amount) from sales_movements where data_status=1 and to_user_id=cl.id and date(sale_date)<'".$start_date."' ".$where2."),0) +
                    IFNULL((select sum(penalty) from game_sales where client_id=cl.id and date(date)<'".$start_date."' and stadium_id in(SELECT id from stadium where data_status=1 ".$where1." )),0)+
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=cl.id and date(date)<'".$start_date."' and debt_direction=1 ),0) -
                    IFNULL((select sum(amount) from client_payments where client_id=cl.id and date(date)<'".$start_date."' and cashbox_id in(SELECT id from cashbox where data_status=1 ".$where1.")),0)) debt,
                    (IFNULL((select sum(price) from game_sales where game_status_id=3 and client_id=cl.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and stadium_id in(SELECT id from stadium where data_status=1 ".$where1." )),0)  +
                    IFNULL((select sum(amount) from sales_movements where data_status=1 and to_user_id=cl.id and date(sale_date)>='".$start_date."' and sale_date<='".$end_date."' ".$where2."),0) +
                    IFNULL((select sum(penalty) from game_sales where client_id=cl.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and stadium_id in(SELECT id from stadium where data_status=1 ".$where1." )),0)+
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=cl.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and debt_direction=1  and amount>0),0)) artim,
                    ((IFNULL((select (sum(amount)) from debt_correction where debt_users_id=cl.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and debt_direction=1  and amount<0),0))*(-1) +
                    IFNULL((select sum(amount) from client_payments where client_id=cl.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and cashbox_id in(SELECT id from cashbox where data_status=1 ".$where1.")),0)) azalma
                    FROM clients cl,local_department ld where cl.user_department_id=ld.id  HAVING debt>0 or artim>0 or azalma>0";
        }
        elseif($report_for==2)
        {
            $where1="";
            if(strchr($department_id , "a")=="")
            {
                $where1="and e.department_id in(".$department_id.")";
            }
            $sql = "SELECT e.id,concat(e.name,' ',e.surname) name, ld.name department_name,
                    (IFNULL((select sum(salary)+sum(bonus)-sum(penalty) from salary_calculation where employee_id=e.id and salary_date<'".$start_date."'),0)  +
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=e.id and debt_direction=2 and date(date)<'".$start_date."'),0) -
                    IFNULL((select sum(amount) from salary_payments where salary_employee_id=e.id and date<'".$start_date."'),0)) debt,
                    (IFNULL((select sum(salary)+sum(bonus)-sum(penalty) from salary_calculation where employee_id=e.id and salary_date>='".$start_date."' and salary_date<='".$end_date."'),0)  +
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=e.id and debt_direction=2 and date(date)>='".$start_date."' and date(date)<='".$end_date."' and amount>0),0) ) artim,        
                    (IFNULL((select sum(amount) from debt_correction where debt_users_id=e.id and debt_direction=2 and date(date)>='".$start_date."' and date(date)<='".$end_date."' and amount<0),0)*(-1)+
                    IFNULL((select sum(amount) from salary_payments where salary_employee_id=e.id and date(date)>'".$start_date."' and date(date)<='".$end_date."'),0)) azalma        
                    FROM employee e,local_department ld where e.department_id=ld.id ".$where1." HAVING debt>0 or artim>0 or azalma>0";
        }
        elseif($report_for==3)
        {
            $where1=$where2="";
            if(strchr($department_id , "a")=="")
            {
                $where1="and department_id in(".$department_id.")";
                $where2="and to_department_id in(".$department_id.")";
            }
            $sql = "SELECT sup.id,concat(sup.name,' ',sup.surname) name,ld.name department_name,
                    (IFNULL((select sum(total_price) from purchases_movements where from_user_id=sup.id ".$where2." and purchase_date<'".$start_date."'),0)  +
                    IFNULL((select sum(amount) from expenses_services where suplier_id=sup.id ".$where1." and payment_date<'".$start_date."'),0) +
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=sup.id and debt_direction=3  and date(date)<'".$start_date."'),0) -
                    IFNULL((select sum(amount) from suplier_payment where data_status=1 and  supplier_id=sup.id and date(date)<'".$start_date."' and cashbox_id in(SELECT id from cashbox where data_status=1 ".$where1.")),0)) debt,
                    (IFNULL((select sum(total_price) from purchases_movements where from_user_id=sup.id ".$where2." and purchase_date>='".$start_date."' and purchase_date<='".$end_date."'),0)  +
                    IFNULL((select sum(amount) from expenses_services where suplier_id=sup.id ".$where1." and payment_date>='".$start_date."' and payment_date<='".$end_date."' ),0) +
                    IFNULL((select sum(amount) from debt_correction where debt_users_id=sup.id and debt_direction=3  and date(date)>='".$start_date."' and date(date)<='".$end_date."' and amount>0),0)) artim,
                    (IFNULL((select sum(amount) from debt_correction where debt_users_id=sup.id and debt_direction=3 ".$where1." and date(date)>='".$start_date."' and date(date)<='".$end_date."' and amount<0),0)*(-1)+
                    IFNULL((select sum(amount) from suplier_payment where data_status=1 and  supplier_id=sup.id and date(date)>='".$start_date."' and date(date)<='".$end_date."' and cashbox_id in(SELECT id from cashbox where data_status=1 ".$where1.")),0)) azalma
                    FROM supplier sup,local_department ld where sup.user_department_id=ld.id HAVING debt>0 or artim>0 or azalma>0";
        }
READ ALSO
Чтение массива php

Чтение массива php

Есть такой алгоритм:

141
перевести с mysqli в PDO

перевести с mysqli в PDO

доброго времени суток, постал вопрос перехода на PDO, есть такая функция как mysqli_fetch_all, каков будет аналог для PDO?

139
Где реализовать очистку полей?

Где реализовать очистку полей?

Как правильно очистить поля(TextBox) формеПробую использовать паттерн MVP есть форма фильтра и в ней много полей

175