выберите из более чем 10 таблиц (MySQL)

Пожалуйста, помогите мне в моем вопросе — у меня есть запрос с select более 10 столов, но я жду более 180 секунд. Это очень очень плохой результат.
Как я могу оптимизировать свой запрос?

SELECT main_order.id,main_order.datetime_add,main_order.datetime_update, main_order.id_user_add, main_order.id_user_update,main_order.date_start,main_order.time_start,main_order.date_finish, main_order.time_finish,main_order.order_cashless,main_order.client_paid, main_order.`commentary`, order_addres.`addres`,order_addres.`build` , order_addres.office_or_flat,pack.name, client.name,client.company,client.phone
FROM main_order,order_addres,pack,client,workers_salary,cash_money,loaders,auto_order,sms
WHERE 1=1
AND date_finish<=:date_finish
OR date_finish IS NULL
GROUP BY main_order.id
ORDER BY date_start DESC, time_start DESC

EXPLAIN

0

Решение

В моем коде я добавил по значениям к моему $ where и после всей операции вставки в запрос. Как я могу обновить его по LEFT JOIN?

<?$params = array();
$where = " WHERE 1=1";

if(!empty($_GET["id"]) &&  isset($_GET["id"]))
{
$params["id"]=$_GET["id"];
$where .= " AND main_order.id=:id";
}

if(!empty($_GET["shop_id"]) &&  isset($_GET["shop_id"]))
{
$params["number_order"]=$_GET["shop_id"];
$where .= " AND sms.number_order=:number_order AND sms.id_order=main_order.id ";
}if(!empty($_GET["date_start"]) &&  isset($_GET["date_start"]))
{
$params["date_start"]=date("Y-m-d",strtotime($_GET["date_start"]));
$where .= " AND date_start>=:date_start OR date_start IS NULL";
}

if(!empty($_GET["time_start"]) &&  isset($_GET["time_start"]))
{
$params["time_start"]=date("H:i",strtotime($_GET["time_start"]));
$where .= " AND time_start >=:time_start  OR time_start IS NULL";
}

if(!empty($_GET["date_finish"]) &&  isset($_GET["date_finish"]))
{
$params["date_finish"]=date("Y-m-d",strtotime($_GET["date_finish"]));
$where .= " AND date_finish<=:date_finish OR date_finish IS NULL";
}

if(!empty($_GET["time_finish"]) &&  isset($_GET["time_finish"]))
{
$params["time_finish"]=date("H:i",strtotime($_GET["time_finish"]));
$where .= " AND time_finish <=:time_finish OR time_finish IS NULL";
}

//MANY MANY FILTER HERE I JUST CUT IT ...........................

if(!empty($_GET["cashier"]) &&  isset($_GET["cashier"]))
{
$i = 1;
$where.=" AND (";
foreach ($_GET["cashier"] as $key )
{
$params["cashier$i"]=$key;
$where.= " cash_money.cashier=:cashier$i OR";
$i++;
}
$where =  substr($where, 0, -3).")";
}

if(!empty($_GET["status"]) &&  isset($_GET["status"]))
{
$i = 1;
$where.=" AND (";
foreach ($_GET["status"] as $key )
{
$params["id_status$i"]=$key;
$where.= " main_order.id_status=:id_status$i OR";
$i++;
}
$where =  substr($where, 0, -3).")";
}@$num = (int)$_GET['row'];
if ($num == 0)
$num = 25;
if (!empty($_GET['page']))
$page = (int)$_GET['page'];
else
$page = 1;

$filter_sql = "SELECT main_order.id,main_order.datetime_add,main_order.datetime_update,
main_order.id_user_add,
main_order.id_user_update,main_order.date_start,main_order.time_start,main_order.date_finish,
main_order.time_finish,main_order.order_cashless,main_order.client_paid, main_order.`commentary`,
order_addres.`addres`,order_addres.`build` , order_addres.office_or_flat,pack.name,
client.name,client.company,client.phone
FROM main_order,order_addres,pack,client,workers_salary,cash_money,loaders,auto_order,sms

$where
GROUP BY main_order.id
ORDER BY date_start DESC, time_start DESC
";exit($filter_sql);

$res = $pdo->prepare($filter_sql);
if(!empty($params))
$res->execute($params);
else  $res->execute();

$posts = $res->rowCount();
$total = intval(($posts - 1) / $num) + 1;
$page = intval($page);
if (empty($page) or $page < 0)
$page = 1;
if ($page > $total)
$page = $total;
$start = $page * $num - $num;

$res = $pdo->prepare("SELECT main_order.id,main_order.datetime_add,main_order.datetime_update,
main_order.id_user_add,
main_order.id_user_update,main_order.date_start,main_order.time_start,main_order.date_finish,
main_order.time_finish,main_order.order_cashless,main_order.client_paid, main_order.`commentary`,
order_addres.`addres`,order_addres.`build` , order_addres.office_or_flat,pack.name,
client.name,client.company,client.phone
FROM main_order,order_addres,pack,client,workers_salary,cash_money,loaders,auto_order,sms

$where
GROUP BY main_order.id
ORDER BY date_start DESC, time_start DESC LIMIT $start,$num
");if(!empty($params))
$res->execute($params);
else  $res->execute();

?>

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]