【问题标题】:Need to optimize this query需要优化这个查询
【发布时间】:2011-04-25 07:44:05
【问题描述】:

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

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
        )
    );

【问题讨论】:

  • 你是如何缩进这个查询的?
  • 除了正确的索引之外,没有什么可以优化的。有关这方面的建议,您应该发布执行计划。
  • 使用制表符和空格?如果您不认为这是一种改进,您可以随时回滚到以前的版本。
  • IDFMA - 请发布以下信息或至少发布此查询的解释计划pastie.org/1220430

标签: mysql join query-optimization


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 2014-11-04
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2014-03-05
    相关资源
    最近更新 更多