爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      mysql/stonedb-查询优化器逻辑记录及优化器魔改

      首页 知识中心 数据库 文章详情页

      mysql/stonedb-查询优化器逻辑记录及优化器魔改

      2024-09-24 06:29:56 阅读次数:487

      mysql,stonedb,数据库

      摘要:

      记录STONEDB对于mysql的查询优化器的使用

      逻辑关系:

      调用堆栈:

      (gdb) bt
      #0 JOIN::optimize (this=0x7fb23804e600, part=1 '\001') at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:296
      #1 0x0000000002c70a23 in Tianmu::core::optimize_select (thd=0x7fb238002c10, select_options=2147748608, result=0x7fb238011940, select_lex=0x7fb238007da0,
      optimize_after_tianmu=@0x7fb468da9d0c: 1, free_join=@0x7fb468da9d10: 1) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/core/engine_execute.cpp:352
      #2 0x0000000002c702d6 in Tianmu::core::Engine::HandleSelect (this=0x5df8fd0, thd=0x7fb238002c10, lex=0x7fb238004f38, result=@0x7fb468da9d18: 0x7fb238011940, setup_tables_done_option=0,
      res=@0x7fb468da9d14: 0, optimize_after_tianmu=@0x7fb468da9d0c: 1, tianmu_free_join=@0x7fb468da9d10: 1, with_insert=0)
      at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/core/engine_execute.cpp:225
      #3 0x0000000002d58d6d in Tianmu::dbhandler::TIANMU_HandleSelect (thd=0x7fb238002c10, lex=0x7fb238004f38, result=@0x7fb468da9d18: 0x7fb238011940, setup_tables_done_option=0,
      res=@0x7fb468da9d14: 0, optimize_after_tianmu=@0x7fb468da9d0c: 1, tianmu_free_join=@0x7fb468da9d10: 1, with_insert=0)
      at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/tianmu/handler/ha_rcengine.cpp:82
      #4 0x000000000239b0ca in execute_sqlcom_select (thd=0x7fb238002c10, all_tables=0x7fb23804b670) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:5182
      #5 0x000000000239444e in mysql_execute_command (thd=0x7fb238002c10, first_level=true) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:2831
      #6 0x000000000239c093 in mysql_parse (thd=0x7fb238002c10, parser_state=0x7fb468daaeb0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:5621
      #7 0x000000000239132b in dispatch_command (thd=0x7fb238002c10, com_data=0x7fb468dab650, command=COM_QUERY)
      at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:1495
      #8 0x0000000002390257 in do_command (thd=0x7fb238002c10) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/sql_:1034
      #9 0x00000000024c2e57 in handle_connection (arg=0x8bf3c30) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/sql/conn_handler/connection_handler_per_:313
      #10 0x0000000002ba6a88 in pfs_spawn_thread (arg=0x8c2c1e0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-75-131-20220805/storage/perfschema/:2197
      #11 0x00007fb4731beea5 in start_thread () from /lib64/libpthread.so.0
      #12 0x00007fb4706e2b0d in clone () from /lib64/libc.so.6

      核心函数:

      Engine::optimize_select

      /*
      Prepares and optimizes a single select for Tianmu engine
      */
      int optimize_select(THD *thd, ulong select_options, Query_result *result,
      SELECT_LEX *select_lex, int &optimize_after_tianmu, int &free_join)
      {
      // copied from sql_select.cpp from the beginning of mysql_select(...)
      int err = 0;
      free_join = 1;
      select_lex->context.resolve_in_select_list = TRUE;
      JOIN *join;
      if (select_lex->join != 0) {
      join = select_lex->join;
      // here is EXPLAIN of subselect or derived table
      if (select_lex->linkage != DERIVED_TABLE_TYPE || (select_options & (1ULL << 2))) {

      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE) {

      if (result->prepare(select_lex->join->fields_list, select_lex->master_unit()) || result->prepare2())
      {
      return TRUE;

      }
      } else {
      if ((err = select_lex->prepare(thd)))
      {
      return err;
      }
      }
      }
      free_join = 0;
      join->select_options = select_options;
      }
      else
      {
      thd_proc_info(thd, "init");

      if ((err = select_lex->prepare(thd)))
      {
      return err;
      }
      if (result->prepare(select_lex->fields_list, select_lex->master_unit()) || result->prepare2()) {
      return TRUE;
      }
      if (!(join = new JOIN(thd, select_lex)))
      return TRUE; /* purecov: inspected */
      select_lex->set_join(join);

      }
      join->best_rowcount = 2;
      optimize_after_tianmu = TRUE;
      if ((err = join->optimize(1)))
      return err;
      return FALSE;
      }
      if ((err = join->optimize(1)))
      return err;

      JOIN::optimize

      /**
      Optimizes one query block into a query execution plan (QEP.)

      This is the entry point to the query optimization phase. This phase
      applies both logical (equivalent) query rewrites, cost-based join
      optimization, and rule-based access path selection. Once an optimal
      plan is found, the member function creates/initializes all
      structures needed for query execution. The main optimization phases
      are outlined below:

      -# Logical transformations:
      - Outer to inner joins transformation.
      - Equality/constant propagation.
      - Partition pruning.
      - COUNT(*), MIN(), MAX() constant substitution in case of
      implicit grouping.
      - ORDER BY optimization.
      -# Perform cost-based optimization of table order and access path
      selection. See JOIN::make_join_plan()
      -# Post-join order optimization:
      - Create optimal table conditions from the where clause and the
      join conditions.
      - Inject outer-join guarding conditions.
      - Adjust data access methods after determining table condition
      (several times.)
      - Optimize ORDER BY/DISTINCT.
      -# Code generation
      - Set data access functions.
      - Try to optimize away sorting/distinct.
      - Setup temporary table usage for grouping and/or sorting.

      @retval 0 Success.
      @retval 1 Error, error code saved in member JOIN::error.
      */
      int
      JOIN::optimize(unsigned char part) //TIANMU UPGRADE
      {
      uint no_jbuf_after= UINT_MAX;

      DBUG_ENTER("JOIN::optimize");
      assert(select_lex->leaf_table_count == 0 ||
      thd->lex->is_query_tables_locked() ||
      select_lex == unit->fake_select_lex);
      //assert(tables == 0 &&
      // primary_tables == 0 &&
      // tables_list == (TABLE_LIST*)1);
      //TIANMU UPGRADE BEGIN
      /*
      Two more values of part were introduced part=3 and part=4. The main reason is to break optimization in sense of part=2 in point
      where all transformations of LOJ conditions are finished. The optimization is continued in case we switch to MySQL.
      The case was wrong result in "select * from t1 left join t2 on a1=b1 and b1=3 where a1=1;". That was due to optimization
      of ON condition "a1=b1 and b1=3" into "a1=b1 and b1=1" which after part=2 would be transformed to FALSE. Part=3 does this transformation.
      However, it has to be stopped at some point (compared to part=2) to avoid rest of optimizations, e.g., creation of temporary tables. (P.S.)
      */
      //const bool first_optimization= select_lex->first_cond_optimization;
      Opt_trace_context * const trace= &thd->opt_trace;
      Opt_trace_object trace_wrapper(trace);
      Opt_trace_object trace_optimize(trace, "join_optimization");
      trace_optimize.add_select_number(select_lex->select_number);
      Opt_trace_array trace_steps(trace, "steps");
      if(part != 4) {
      if (part==0 || part==1)
      {
      //END
      // to prevent double initialization on EXPLAIN
      if (optimized)
      DBUG_RETURN(0);

      Prepare_error_tracker tracker(thd);

      DEBUG_SYNC(thd, "before_join_optimize");

      THD_STAGE_INFO(thd, stage_optimizing);

      if (select_lex->first_execution)
      {
      /**
      @todo
      This query block didn't transform itself in SELECT_LEX::prepare(), so
      belongs to a parent query block. That parent, or its parents, had to
      transform us - it has not; maybe it is itself in prepare() and
      evaluating the present query block as an Item_subselect. Such evaluation
      in prepare() is expected to be a rare case to be eliminated in the
      future ("SET x=(subq)" is one such case; because it locks tables before
      prepare()).
      */
      if (select_lex->apply_local_transforms(thd, false))
      DBUG_RETURN(error= 1);
      }
      //TIANMU UPGRADE
      /*
      Opt_trace_context * const trace= &thd->opt_trace;
      Opt_trace_object trace_wrapper(trace);
      Opt_trace_object trace_optimize(trace, "join_optimization");
      trace_optimize.add_select_number(select_lex->select_number);
      Opt_trace_array trace_steps(trace, "steps");
      */
      //END
      count_field_types(select_lex, &tmp_table_param, all_fields, false, false);

      assert(tmp_table_param.sum_func_count == 0 ||
      group_list || implicit_grouping);

      if (select_lex->olap == ROLLUP_TYPE && optimize_rollup())
      DBUG_RETURN(true); /* purecov: inspected */

      if (alloc_func_list())
      DBUG_RETURN(1); /* purecov: inspected */

      if (select_lex->get_optimizable_conditions(thd, &where_cond, &having_cond))
      DBUG_RETURN(1);

      set_optimized();

      tables_list= select_lex->get_table_list();

      /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
      /*
      Run optimize phase for all derived tables/views used in this SELECT,
      including those in semi-joins.
      */
      if (select_lex->materialized_derived_table_count)
      {
      for (TABLE_LIST *tl= select_lex->leaf_tables; tl; tl= tl->next_leaf)
      {
      if (tl->is_view_or_derived() && tl->optimize_derived(thd))
      DBUG_RETURN(1);
      }
      }

      /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */

      row_limit= ((select_distinct || order || group_list) ?
      HA_POS_ERROR : unit->select_limit_cnt);
      // m_select_limit is used to decide if we are likely to scan the whole table.
      m_select_limit= unit->select_limit_cnt;

      if (unit->first_select()->active_options() & OPTION_FOUND_ROWS)
      {
      /*
      Calculate found rows if
      - LIMIT is set, and
      - Query block is not equipped with "braces". In this case, each
      query block must be calculated fully and the limit is applied on
      the final UNION evaluation.
      */
      calc_found_rows= m_select_limit != HA_POS_ERROR && !select_lex->braces;
      }
      if (having_cond || calc_found_rows)
      m_select_limit= HA_POS_ERROR;

      if (unit->select_limit_cnt == 0 && !calc_found_rows)
      {
      zero_result_cause= "Zero limit";
      best_rowcount= 0;
      goto setup_subq_exit;
      }
      }//TIANMU UPGRADE END part=0||part=1
      if (where_cond || select_lex->outer_join)
      {
      if (optimize_cond(thd, &where_cond, &cond_equal,
      &select_lex->top_join_list, &select_lex->cond_value,part))//TIANMU UPGRADE
      {
      error= 1;
      DBUG_PRINT("error",("Error from optimize_cond"));
      DBUG_RETURN(1);
      }
      if (select_lex->cond_value == Item::COND_FALSE)
      {
      zero_result_cause= "Impossible WHERE";
      best_rowcount= 0;
      goto setup_subq_exit;
      }
      }
      if (having_cond)
      {
      if (optimize_cond(thd, &having_cond, &cond_equal, NULL,
      &select_lex->having_value, part))//TIANMU UPGRADE
      {
      error= 1;
      DBUG_PRINT("error",("Error from optimize_cond"));
      DBUG_RETURN(1);
      }
      if (select_lex->having_value == Item::COND_FALSE)
      {
      zero_result_cause= "Impossible HAVING";
      best_rowcount= 0;
      goto setup_subq_exit;
      }
      }
      //TIANMU UPGRADE
      if (part == 1)
      {
      error= 0;
      DBUG_RETURN(0);
      }
      //END
      if (select_lex->partitioned_table_count && prune_table_partitions())
      {
      error= 1;
      DBUG_PRINT("error", ("Error from prune_partitions"));
      DBUG_RETURN(1);
      }

      /*
      Try to optimize count(*), min() and max() to const fields if
      there is implicit grouping (aggregate functions but no
      group_list). In this case, the result set shall only contain one
      row.
      */
      if (tables_list && implicit_grouping)
      {
      int res;
      /*
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
      the WHERE condition,
      or 1 if all items were resolved (optimized away),
      or 0, or an error number HA_ERR_...

      If all items were resolved by opt_sum_query, there is no need to
      open any tables.
      */
      if ((res= opt_sum_query(thd, select_lex->leaf_tables, all_fields,
      where_cond)))
      {
      best_rowcount= 0;
      if (res == HA_ERR_KEY_NOT_FOUND)
      {
      DBUG_PRINT("info",("No matching min/max row"));
      zero_result_cause= "No matching min/max row";
      goto setup_subq_exit;
      }
      if (res > 1)
      {
      error= res;
      DBUG_PRINT("error",("Error from opt_sum_query"));
      DBUG_RETURN(1);
      }
      if (res < 0)
      {
      DBUG_PRINT("info",("No matching min/max row"));
      zero_result_cause= "No matching min/max row";
      goto setup_subq_exit;
      }
      DBUG_PRINT("info",("Select tables optimized away"));
      zero_result_cause= "Select tables optimized away";
      tables_list= 0; // All tables resolved
      best_rowcount= 1;
      const_tables= tables= primary_tables= select_lex->leaf_table_count;
      /*
      Extract all table-independent conditions and replace the WHERE
      clause with them. All other conditions were computed by opt_sum_query
      and the MIN/MAX/COUNT function(s) have been replaced by constants,
      so there is no need to compute the whole WHERE clause again.
      Notice that make_cond_for_table() will always succeed to remove all
      computed conditions, because opt_sum_query() is applicable only to
      conjunctions.
      Preserve conditions for EXPLAIN.
      */
      if (where_cond && !thd->lex->describe)
      {
      Item *table_independent_conds=
      make_cond_for_table(where_cond, PSEUDO_TABLE_BITS, 0, 0);
      DBUG_EXECUTE("where",
      print_where(table_independent_conds,
      "where after opt_sum_query()",
      QT_ORDINARY););
      where_cond= table_independent_conds;
      }
      goto setup_subq_exit;
      }
      }
      if (!tables_list)
      {
      DBUG_PRINT("info",("No tables"));
      best_rowcount= 1;
      error= 0;
      if (make_tmp_tables_info())
      DBUG_RETURN(1);
      count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
      // Make plan visible for EXPLAIN
      set_plan_state(NO_TABLES);
      DBUG_RETURN(0);
      }
      error= -1; // Error is sent to client
      sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);

      if ((where_cond || group_list || order) &&
      substitute_gc(thd, select_lex, where_cond, group_list, order))
      {
      // We added hidden fields to the all_fields list, count them.
      count_field_types(select_lex, &tmp_table_param, select_lex->all_fields,
      false, false);
      }

      // Set up join order and initial access paths
      THD_STAGE_INFO(thd, stage_statistics);
      if (make_join_plan())
      {
      if (thd->killed)
      thd->send_kill_message();
      DBUG_PRINT("error",("Error: JOIN::make_join_plan() failed"));
      DBUG_RETURN(1);
      }

      // At this stage, join_tab==NULL, JOIN_TABs are listed in order by best_ref.
      ASSERT_BEST_REF_IN_JOIN_ORDER(this);

      if (zero_result_cause)
      goto setup_subq_exit;

      if (rollup.state != ROLLUP::STATE_NONE)
      {
      if (rollup_process_const_fields())
      {
      DBUG_PRINT("error", ("Error: rollup_process_fields() failed"));
      DBUG_RETURN(1);
      }
      /*
      Fields may have been replaced by Item_func_rollup_const, so
      recalculate the number of fields and functions for this query block.
      */

      // JOIN::optimize_rollup() may set quick_group=0, and we must not undo that.
      const uint save_quick_group= tmp_table_param.quick_group;

      count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
      tmp_table_param.quick_group= save_quick_group;
      }
      else
      {
      /* Remove distinct if only const tables */
      select_distinct&= !plan_is_const();
      }

      if (const_tables && !thd->locked_tables_mode &&
      !(select_lex->active_options() & SELECT_NO_UNLOCK )&& part!=3) //TIANMU UPGRADE
      {
      TABLE *ct[MAX_TABLES];
      for (uint i= 0; i < const_tables; i++)
      ct[i]= best_ref[i]->table();
      mysql_unlock_some_tables(thd, ct, const_tables);
      }
      if (!where_cond && select_lex->outer_join)
      {
      /* Handle the case where we have an OUTER JOIN without a WHERE */
      where_cond=new Item_int((longlong) 1,1); // Always true
      }

      error= 0;
      /*
      Among the equal fields belonging to the same multiple equality
      choose the one that is to be retrieved first and substitute
      all references to these in where condition for a reference for
      the selected field.
      */
      if (where_cond)
      {
      where_cond= substitute_for_best_equal_field(where_cond, cond_equal,
      map2table);
      if (thd->is_error())
      {
      error= 1;
      DBUG_PRINT("error",("Error from substitute_for_best_equal"));
      DBUG_RETURN(1);
      }
      where_cond->update_used_tables();
      DBUG_EXECUTE("where",
      print_where(where_cond,
      "after substitute_best_equal",
      QT_ORDINARY););
      }

      /*
      Perform the same optimization on field evaluation for all join conditions.
      */
      for (uint i= const_tables; i < tables ; ++i)
      {
      JOIN_TAB *const tab= best_ref[i];
      if (tab->position() && tab->join_cond())
      {
      tab->set_join_cond(substitute_for_best_equal_field(tab->join_cond(),
      tab->cond_equal,
      map2table));
      if (thd->is_error())
      {
      error= 1;
      DBUG_PRINT("error",("Error from substitute_for_best_equal"));
      DBUG_RETURN(1);
      }
      tab->join_cond()->update_used_tables();
      }
      }
      //TIANMU UPGRADE BEGIN
      // this is end of part=3 and beginning of part=4
      if(part == 3) {
      DBUG_RETURN(0); // error == 0
      }
      //END
      }// end of if(part!=4)
      if (init_ref_access())
      {
      error= 1;
      DBUG_PRINT("error",("Error from init_ref_access"));
      DBUG_RETURN(1);
      }

      // Update table dependencies after assigning ref access fields
      update_depend_map();

      THD_STAGE_INFO(thd, stage_preparing);

      if (make_join_select(this, where_cond))
      {
      if (thd->is_error())
      DBUG_RETURN(1);

      zero_result_cause=
      "Impossible WHERE noticed after reading const tables";
      goto setup_subq_exit;
      }

      if (select_lex->query_result()->initialize_tables(this))
      {
      DBUG_PRINT("error",("Error: initialize_tables() failed"));
      DBUG_RETURN(1); // error == -1
      }

      error= -1; /* if goto err */

      if (optimize_distinct_group_order())
      DBUG_RETURN(true);

      if ((select_lex->active_options() & SELECT_NO_JOIN_CACHE) ||
      select_lex->ftfunc_list->elements)
      no_jbuf_after= 0;

      /* Perform FULLTEXT search before all regular searches */
      if (select_lex->has_ft_funcs() && optimize_fts_query())
      DBUG_RETURN(1);

      /*
      By setting child_subquery_can_materialize so late we gain the following:
      JOIN::compare_costs_of_subquery_strategies() can test this variable to
      know if we are have finished evaluating constant conditions, which itself
      helps determining fanouts.
      */
      child_subquery_can_materialize= true;

      /*
      It's necessary to check const part of HAVING cond as
      there is a chance that some cond parts may become
      const items after make_join_statisctics(for example
      when Item is a reference to const table field from
      outer join).
      This check is performed only for those conditions
      which do not use aggregate functions. In such case
      temporary table may not be used and const condition
      elements may be lost during further having
      condition transformation in JOIN::exec.
      */
      if (having_cond && const_table_map && !having_cond->with_sum_func)
      {
      having_cond->update_used_tables();
      if (remove_eq_conds(thd, having_cond, &having_cond,
      &select_lex->having_value, part))
      {
      error= 1;
      DBUG_PRINT("error",("Error from remove_eq_conds"));
      DBUG_RETURN(1);
      }
      if (select_lex->having_value == Item::COND_FALSE)
      {
      having_cond= new Item_int((longlong) 0,1);
      zero_result_cause= "Impossible HAVING noticed after reading const tables";
      goto setup_subq_exit;
      }
      }

      /* Cache constant expressions in WHERE, HAVING, ON clauses. */
      if (!plan_is_const() && cache_const_exprs())
      DBUG_RETURN(1);

      // See if this subquery can be evaluated with subselect_indexsubquery_engine
      if (const int ret= replace_index_subquery())
      {
      set_plan_state(PLAN_READY);
      /*
      We leave optimize() because the rest of it is only about order/group
      which those subqueries don't have and about setting up plan which
      we're not going to use due to different execution method.
      */
      DBUG_RETURN(ret < 0);
      }

      {
      /*
      If the hint FORCE INDEX FOR ORDER BY/GROUP BY is used for the first
      table (it does not make sense for other tables) then we cannot do join
      buffering.
      */
      if (!plan_is_const())
      {
      const TABLE * const first= best_ref[const_tables]->table();
      if ((first->force_index_order && order) ||
      (first->force_index_group && group_list))
      no_jbuf_after= 0;
      }

      bool simple_sort= true;
      // Check whether join cache could be used
      for (uint i= const_tables; i < tables; i++)
      {
      JOIN_TAB *const tab= best_ref[i];
      if (!tab->position())
      continue;
      if (setup_join_buffering(tab, this, no_jbuf_after))
      DBUG_RETURN(true);
      if (tab->use_join_cache() != JOIN_CACHE::ALG_NONE)
      simple_sort= false;
      assert(tab->type() != JT_FT ||
      tab->use_join_cache() == JOIN_CACHE::ALG_NONE);
      }
      if (!simple_sort)
      {
      /*
      A join buffer is used for this table. We here inform the optimizer
      that it should not rely on rows of the first non-const table being in
      order thanks to an index scan; indeed join buffering of the present
      table subsequently changes the order of rows.
      */
      simple_order= simple_group= false;
      }
      }

      if (!plan_is_const() && order)
      {
      /*
      Force using of tmp table if sorting by a SP or UDF function due to
      their expensive and probably non-deterministic nature.
      */
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
      {
      Item *item= *tmp_order->item;
      if (item->is_expensive())
      {
      /* Force tmp table without sort */
      simple_order= simple_group= false;
      break;
      }
      }
      }

      /*
      Check if we need to create a temporary table.
      This has to be done if all tables are not already read (const tables)
      and one of the following conditions holds:
      - We are using DISTINCT (simple distinct's have already been optimized away)
      - We are using an ORDER BY or GROUP BY on fields not in the first table
      - We are using different ORDER BY and GROUP BY orders
      - The user wants us to buffer the result.
      When the WITH ROLLUP modifier is present, we cannot skip temporary table
      creation for the DISTINCT clause just because there are only const tables.
      */
      need_tmp= ((!plan_is_const() &&
      ((select_distinct || (order && !simple_order) ||
      (group_list && !simple_group)) ||
      (group_list && order) ||
      (select_lex->active_options() & OPTION_BUFFER_RESULT))) ||
      (rollup.state != ROLLUP::STATE_NONE && select_distinct));

      DBUG_EXECUTE("info", TEST_join(this););

      if (!plan_is_const())
      {
      JOIN_TAB *tab= best_ref[const_tables];
      /*
      Because filesort always does a full table scan or a quick range scan
      we must add the removed reference to the select for the table.
      We only need to do this when we have a simple_order or simple_group
      as in other cases the join is done before the sort.
      */
      if ((order || group_list) &&
      tab->type() != JT_ALL &&
      tab->type() != JT_FT &&
      tab->type() != JT_REF_OR_NULL &&
      ((order && simple_order) || (group_list && simple_group)))
      {
      if (add_ref_to_table_cond(thd,tab)) {
      DBUG_RETURN(1);
      }
      }
      // Test if we can use an index instead of sorting
      test_skip_sort();
      }

      if (alloc_qep(tables))
      DBUG_RETURN(error= 1); /* purecov: inspected */

      if (make_join_readinfo(this, no_jbuf_after))
      DBUG_RETURN(1); /* purecov: inspected */

      if (make_tmp_tables_info())
      DBUG_RETURN(1);

      // At this stage, we have fully set QEP_TABs; JOIN_TABs are unaccessible,
      // pushed joins(see below) are still allowed to change the QEP_TABs

      /*
      Push joins to handlerton(s)

      The handlerton(s) will inspect the QEP through the
      AQP (Abstract Query Plan) and extract from it whatever
      it might implement of pushed execution.

      It is the responsibility of the handler:
      - to store any information it need for later
      execution of pushed queries.
      - to call appropriate AQP functions which modifies the
      QEP to use the special 'linked' read functions
      for those parts of the join which have been pushed.

      Currently pushed joins are only implemented by NDB.

      It only make sense to try pushing if > 1 non-const tables.
      */
      if (!plan_is_single_table() && !plan_is_const())
      {
      const AQP::Join_plan plan(this);
      if (ha_make_pushed_joins(thd, &plan))
      DBUG_RETURN(1);
      }

      // Update m_current_query_cost to reflect actual need of filesort.
      if (sort_cost > 0.0 && !explain_flags.any(ESP_USING_FILESORT))
      {
      best_read-= sort_cost;
      sort_cost= 0.0;
      if (thd->lex->is_single_level_stmt())
      thd->m_current_query_cost= best_read;
      }

      count_field_types(select_lex, &tmp_table_param, all_fields, false, false);
      // Make plan visible for EXPLAIN
      set_plan_state(PLAN_READY);

      DEBUG_SYNC(thd, "after_join_optimize");

      error= 0;
      DBUG_RETURN(0);

      setup_subq_exit:

      assert(zero_result_cause != NULL);
      /*
      Even with zero matching rows, subqueries in the HAVING clause may
      need to be evaluated if there are aggregate functions in the
      query. If this JOIN is part of an outer query, subqueries in HAVING may
      be evaluated several times in total; so subquery materialization makes
      sense.
      */
      child_subquery_can_materialize= true;
      trace_steps.end(); // because all steps are done
      Opt_trace_object(trace, "empty_result")
      .add_alnum("cause", zero_result_cause);

      having_for_explain= having_cond;
      error= 0;

      if (!qep_tab && best_ref)
      {
      /*
      After creation of JOIN_TABs in make_join_plan(), we have shortcut due to
      some zero_result_cause. For simplification, if we have JOIN_TABs we
      want QEP_TABs too.
      */
      if (alloc_qep(tables))
      DBUG_RETURN(1); /* purecov: inspected */
      unplug_join_tabs();
      }

      set_plan_state(ZERO_RESULT);
      DBUG_RETURN(0);
      }
      //TIANMU UPGRADE 
      if (part == 1)
      {
      error= 0;
      DBUG_RETURN(0);
      }

      遇到的不解的地方记录:

      一. STONEDB引擎调用SQL层的JOIN::optimize, 为什么将参数part设置为1 

      二. 为何要魔改SQL层的JOIN::optimize, part为1的情况下, 不继续执行后续的查询优化

      2.1 TIANMU UPGRADE的注释是什么意思? 为什么不继续执行后续逻辑

      2.2 part的语义, 是否还和mysql原始代码中的语义相同?

      2022-09-07 mysql/stonedb-查询优化器逻辑记录及优化器魔改

      2022-09-07 mysql/stonedb-查询优化器逻辑记录及优化器魔改

      2.3 make_join_plan的逻辑在TIANMU UPGRADE的逻辑之后, 无法执行相关join优化

      1. JOIN::decide_subquery_strategy 通过代价计算判定是将IN转换为EXISTS还是使用物化
      2. Item_in_subselect::finalize_exists_transform 完成将IN转换为EXISTS
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/adofsauron/5668071,作者:帝尊悟世,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:oracle字符串清洗、拆分案例

      下一篇: mysql/stonedb-Q16-并行执行分析

      相关文章

      2025-05-19 09:05:01

      项目更新到公网服务器的操作步骤

      项目更新到公网服务器的操作步骤

      2025-05-19 09:05:01
      公网 , 数据库 , 文件 , 更新 , 服务器
      2025-05-19 09:04:53

      Django rest froamwork-ModelSerializer

      Django rest froamwork-ModelSerializer

      2025-05-19 09:04:53
      django , sqlite , 数据库
      2025-05-19 09:04:38

      mysql只有在任务处于完成状态才能运行

      mysql只有在任务处于完成状态才能运行

      2025-05-19 09:04:38
      MySQL , 任务 , 数据库 , 查询 , 状态
      2025-05-19 09:04:30

      设置28401事件后启动数据库时报错ORA-49100

      设置28401事件后启动数据库时报错ORA-49100

      2025-05-19 09:04:30
      ORA , 数据库 , 时报
      2025-05-14 10:03:13

      MySQL 索引优化以及慢查询优化

      MySQL 是一种广泛使用的关系型数据库管理系统,因其性能优异和使用便捷而备受欢迎。然而,随着数据量的增长和查询复杂度的增加,性能瓶颈也变得越来越明显。

      2025-05-14 10:03:13
      MySQL , 优化 , 使用 , 性能 , 数据库 , 查询 , 索引
      2025-05-14 10:03:05

      Oracle数据库用户权限分析

      Oracle数据库用户权限分析

      2025-05-14 10:03:05
      Oracle , 分析 , 数据库 , 权限 , 用户
      2025-05-14 10:02:48

      互斥锁解决redis缓存击穿

      在高并发系统中,Redis 缓存是一种常见的性能优化方式。然而,缓存击穿问题也伴随着高并发访问而来。

      2025-05-14 10:02:48
      Redis , 互斥 , 数据库 , 线程 , 缓存 , 请求
      2025-05-14 10:02:48

      SQL Server 账号管理1

      SQL Server 账号管理主要包含登录名、用户、架构、角色等管理。通过对账号的管理可以有效的提高数据库系统的安全性,规范运维及使用。

      2025-05-14 10:02:48
      Server , SQL , 对象 , 数据库 , 权限 , 用户
      2025-05-14 10:02:48

      SQL Server 事务日志体系结构1--基本术语

      事务包括对数据库的一次更改或一系列更改。它有一个明确开始和明确结束。开始时使用BEGIN TRANSACTION语句,或者SQL Server会自动为您开始一个事务。

      2025-05-14 10:02:48
      Server , SQL , 事务 , 数据库 , 日志 , 磁盘
      2025-05-13 09:51:17

      dblogin登陆数据库时报错ORA-04060

      dblogin登陆数据库时报错ORA-04060

      2025-05-13 09:51:17
      ORA , 数据库 , 时报
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5232038

      查看更多

      最新文章

      Django rest froamwork-ModelSerializer

      2025-05-19 09:04:53

      mysql只有在任务处于完成状态才能运行

      2025-05-19 09:04:38

      设置28401事件后启动数据库时报错ORA-49100

      2025-05-19 09:04:30

      MySQL 索引优化以及慢查询优化

      2025-05-14 10:03:13

      Oracle数据库用户权限分析

      2025-05-14 10:03:05

      SQL Server 账号管理1

      2025-05-14 10:02:48

      查看更多

      热门文章

      Windows下使用批处理实现启动关闭mysql

      2023-04-24 11:27:05

      cdh安装到scm-server的mysql报错处理

      2023-04-28 02:38:44

      Nacos数据持久化到MySQL

      2023-05-12 07:20:56

      python学习——使用MySQL

      2023-04-27 07:57:16

      MySQL的间隙锁

      2023-05-12 07:20:56

      正确理解Mysql的列索引和多列索引

      2023-05-12 07:20:42

      查看更多

      热门标签

      数据库 mysql 字符串 数据结构 MySQL 算法 redis oracle java sql python 数据 索引 SQL 查询
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      深入解析MySQL分区(Partition)功能

      Sybase数据库故障的修复方法

      mysql无法使用mysqldump命令

      Django+celery+eventlet+flower+redis异步任务创建及查询实现

      如何在Ubuntu14.04中安装mysql

      《MySQL高级篇》十一、事务基础知识

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号