PostgreSQL在何处处理 sql查询之二十七

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:

接前面,仔细看这个 :这 add_base_rels_to_query 是个递归调用嘛。

想像一下: select  * from tst01  where id in (select sid from tst02)  or id in (select sid from tst03) 之类的,此函数将层层深入,构造一个二叉树式样的语法树。

复制代码
void
add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
{
    if (jtnode == NULL)
        return;
    if (IsA(jtnode, RangeTblRef))
    {
        int    varno = ((RangeTblRef *) jtnode)->rtindex;
        (void) build_simple_rel(root, varno, RELOPT_BASEREL);
    }
    else if (IsA(jtnode, FromExpr))
    {
        FromExpr   *f = (FromExpr *) jtnode;
        ListCell   *l;

        foreach(l, f->fromlist)
            add_base_rels_to_query(root, lfirst(l));
    }
    else if (IsA(jtnode, JoinExpr))
    {
        JoinExpr   *j = (JoinExpr *) jtnode;

        add_base_rels_to_query(root, j->larg);
        add_base_rels_to_query(root, j->rarg);
    }
    else
        elog(ERROR, "unrecognized node type: %d",
             (int) nodeTag(jtnode));
}
复制代码

 这个时候,我想到一个问题,在PostgreSQL中,有没有并行查询的可能呢?

那么,再上溯到更高的层面,再次梳理一下:

exec_simple_query中调用 了 plan_tree_list,从语法分析树得到了计划树...执行 PortalRun。

而上面的 add_base_rels_to_query 是plan_tree_list 里的一部分。

调用关系如下:

plan_tree_list -->pg_plan_queries-->pg_plan_query-->planner

planner-->subquery_planner-->groupingplanner

groupingplanner-->query_planner-->add_base_rels_to_query

复制代码
/*
  * exec_simple_query
  *
  * Execute a "simple Query" protocol message.
  */
 static void
 exec_simple_query(const char *query_string)
 {
     ...
     isTopLevel = (list_length(parsetree_list) == 1);
     ...
     foreach(parsetree_item, parsetree_list)
     {
         ...
         querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
                                                 NULL, 0);
 
         plantree_list = pg_plan_queries(querytree_list, 0, NULL);
         ...
 
         /*
          * Create unnamed portal to run the query or queries in. If there
          * already is one, silently drop it.
          */
         portal = CreatePortal("", true, true);
         /* Don't display the portal in pg_cursors */
         portal->visible = false;
 
         /*
          * We don't have to copy anything into the portal, because everything
          * we are passing here is in MessageContext, which will outlive the
          * portal anyway.
          */
         PortalDefineQuery(portal,
                           NULL,
                           query_string,
                           commandTag,
                           plantree_list,
                           NULL);
 
         /*
          * Start the portal.
          *
          * If we took a snapshot for parsing/planning, the portal may be able
          * to reuse it for the execution phase.  Currently, this will only
          * happen in PORTAL_ONE_SELECT mode.  But even if PortalStart doesn't
          * end up being able to do this, keeping the parse/plan snapshot
          * around until after we start the portal doesn't cost much.
          */
         PortalStart(portal, NULL, 0, snapshot_set);
         ...
 
         /*
          * Run the portal to completion, and then drop it (and the receiver).
          */
         (void) PortalRun(portal,
                          FETCH_ALL,
                          isTopLevel,
                          receiver,
                          receiver,
                          completionTag);
 
         (*receiver->rDestroy) (receiver);
 
         PortalDrop(portal, false);
 
         ...
     }                            /* end loop over parsetrees */
 
     /*
      * Close down transaction statement, if one is open.
      */
     finish_xact_command();
     ...
 }
复制代码

 加入调试信息,看看一个带子查询的SQL,能否有并行查询的可能。

复制代码
[postgres@lex pgsql]$ ./bin/psql
psql (9.2.1)
Type "help" for help.

postgres=# select * from tst01 where id IN (select sid from tst02) or id IN (select sid from tst03);
 id  
-----
 100
 200
(2 rows)

postgres=# 
复制代码

看后台的信息,知道planner调用了三次,PortalRun只调用了一次。

复制代码
[postgres@lex pgsql]$ ./bin/pg_ctl -D ./data start
server starting
[postgres@lex pgsql]$ LOG:  database system was shut down at 2013-05-29 10:12:02 CST
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
In query_planner.... by process 11796
In get_relation_info ...oid of table is: 16387 ...by process 11796
In query_planner.... by process 11796
In query_planner.... by process 11796
In get_relation_info ...oid of table is: 16390 ...by process 11796
In query_planner.... by process 11796
In query_planner.... by process 11796
In get_relation_info ...oid of table is: 16384 ...by process 11796
In query_planner.... by process 11796
In PortalRun ... by 11796 
复制代码

也就是说,PostgreSQL 没有子查询的并行查询能力了?!

至少目前,它还非常地不完整:

http://postgresql.1045698.n5.nabble.com/Parallel-query-execution-td5740431.html

http://wiki.postgresql.org/wiki/Parallel_Query_Execution

各种集群方案也作不到这点:

http://www.postgresql.org/docs/current/static/different-replication-solutions.html

Many of the above solutions allow multiple servers to handle multiple queries, but none allow a single query to use multiple servers to complete faster.

对比下Oracle的:

http://miracle.blog.51cto.com/255044/147058/





本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2013/05/29/3105388.html,如需转载请自行联系原作者

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
1天前
|
SQL 安全 数据库
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
如何在Django中正确使用参数化查询或ORM来避免SQL注入漏洞?
27 20
|
20天前
|
SQL NoSQL Java
Java使用sql查询mongodb
通过使用 MongoDB Connector for BI 和 JDBC,开发者可以在 Java 中使用 SQL 语法查询 MongoDB 数据库。这种方法对于熟悉 SQL 的团队非常有帮助,能够快速实现对 MongoDB 数据的操作。同时,也需要注意到这种方法的性能和功能限制,根据具体应用场景进行选择和优化。
64 9
|
1月前
|
SQL 存储 人工智能
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
Vanna 是一个开源的 Python RAG(Retrieval-Augmented Generation)框架,能够基于大型语言模型(LLMs)为数据库生成精确的 SQL 查询。Vanna 支持多种 LLMs、向量数据库和 SQL 数据库,提供高准确性查询,同时确保数据库内容安全私密,不外泄。
150 7
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
|
2月前
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
43 8
|
2月前
|
SQL 安全 PHP
PHP开发中防止SQL注入的方法,包括使用参数化查询、对用户输入进行过滤和验证、使用安全的框架和库等,旨在帮助开发者有效应对SQL注入这一常见安全威胁,保障应用安全
本文深入探讨了PHP开发中防止SQL注入的方法,包括使用参数化查询、对用户输入进行过滤和验证、使用安全的框架和库等,旨在帮助开发者有效应对SQL注入这一常见安全威胁,保障应用安全。
73 4
|
2月前
|
SQL 监控 关系型数据库
SQL语句当前及历史信息查询-performance schema的使用
本文介绍了如何使用MySQL的Performance Schema来获取SQL语句的当前和历史执行信息。Performance Schema默认在MySQL 8.0中启用,可以通过查询相关表来获取详细的SQL执行信息,包括当前执行的SQL、历史执行记录和统计汇总信息,从而快速定位和解决性能瓶颈。
|
2月前
|
SQL 存储 缓存
如何优化SQL查询性能?
【10月更文挑战第28天】如何优化SQL查询性能?
198 10
|
2月前
|
SQL 关系型数据库 MySQL
|
2月前
|
SQL 关系型数据库 数据库
PostgreSQL性能飙升的秘密:这几个调优技巧让你的数据库查询速度翻倍!
【10月更文挑战第25天】本文介绍了几种有效提升 PostgreSQL 数据库查询效率的方法,包括索引优化、查询优化、配置优化和硬件优化。通过合理设计索引、编写高效 SQL 查询、调整配置参数和选择合适硬件,可以显著提高数据库性能。
536 1
|
3月前
|
SQL 数据库 开发者
功能发布-自定义SQL查询
本期主要为大家介绍ClkLog九月上线的新功能-自定义SQL查询。