Помогите мне оптимизировать этот запрос. пожалуйста - PullRequest
0 голосов
/ 07 апреля 2011
$query_index_neighborhood1 = 
"SELECT areas_db.areas_name, areas_db.areas_id, neighborhoods_db.neighborhoods_id,
neighborhoods_db.neighborhoods_name, neighborhoods_db.neighborhoods_area_id, 
areas_db.areas_state_id 
FROM (
       (
         (
            restaurants_db 
            INNER JOIN neighborhoods_db ON neighborhoods_db.neighborhoods_id=restaurants_db.restaurants_neighborhood
         ) 
         INNER JOIN areas_db ON areas_db.areas_id=neighborhoods_db.neighborhoods_area_id
       )
       INNER JOIN areas_db AS areas_db1 on areas_db1.areas_id=restaurants_db.restaurants_area
    )
WHERE areas_db.areas_state_id=$mxstateid
GROUP BY neighborhoods_db.neighborhoods_id
ORDER BY areas_db.areas_id, neighborhoods_db.neighborhoods_name ASC";

1 Ответ

0 голосов
/ 30 апреля 2011

В качестве интересного мыслительного упражнения я придумал следующее:

SELECT a.areas_name,
    a.areas_id,
    n.neighborhoods_id,
    n.neighborhoods_name,
    n.neighborhoods_area_id, 
    a.areas_state_id 
FROM neighborhoods_db AS n
INNER JOIN areas_db AS a ON a.areas_id = n.neighborhoods_area_id
WHERE a.areas_state_id = $mxstateid
    AND n.neighborhoods_id in (SELECT restaurants_neighborhood FROM restaurants_db)
ORDER BY a.areas_id, n.neighborhoods_name ASC

Кроме того, псевдонимы таблиц - ваш друг.

...