需要优化此查询

时间:2010-10-14 09:51:38

标签: mysql join query-optimization

我有一个优化此查询的任务,任何想法将不胜感激。

SELECT DISTINCT `br`.`id` AS `branch_id`
       , `br`.`store_id`
       , `br`.`longitude`
       , `br`.`latitude`
       , `br`.`town_id`
       , `br`.`post_region`
       , `cb`.`cat0_id`
       , `cb`.`cat1_id`
       , `cb`.`cat2_id`
       , `plan`.`listing_plan_id`
       , `cat0`.`cat_topid`
       , `cg`.`tarrif` 
FROM   `mvcl_local_new`.`branch` AS `br`
       INNER JOIN `mvcl_local_new`.`voucher_branch` AS `vb` ON br.id = vb.branch_id
       INNER JOIN `mvcl_local_new`.`voucher` AS `v` ON v.id = vb.voucher_id
       INNER JOIN `category_voucher` AS `cb` ON cb.voucher_id = v.id
       INNER JOIN `category_voucher_listing_plans` AS `plan` ON plan.voucher_id = v.id
       INNER JOIN `category_level0` AS `cat0` ON cb.cat0_id = cat0.id
       LEFT JOIN `category_tariffs` AS `cg` ON cg.voucher_id = v.id 
WHERE  (
        br.latitude != 0 
        and br.longitude != 0 
        and br.post_region <> ''
        and valid_from < 1286876423 
        and expires > 1286876423
        and 
        (
            (
                plan.listing_plan_id IN (1,2,3) 
                and cg.town_id = br.town_id 
                and cg.is_active = 1 
                and cg.status_code='Active' 
                and cg.tarrif_id=0
            )
            or plan.listing_plan_id=4
        )
    );

1 个答案:

答案 0 :(得分:2)

对于优化查询,在dbForge Studio For MySQL中使用MySQL Profiler非常方便。