postgresql——底层对查询类语句的处理(优化),以及执行计划生成策略

一次简单的postgreSQL的SQL语句优化实际案例 简单的SQL语句优化,最后有些疑问 阅读详情

理论先行

  1. postgresql执行SQL语句时,首先通过语法分析模块(词法分析、语法分析(语法树)、语义分析(查询树)),然后通过查询优化器进行逻辑优化和物理优化,生成计划树,就是执行计划。
  2. 逻辑优化: 是基于规则的优化,对SQL进行重写。比如谓词下推,连接顺序交换等。
  3. 物理优化:是基于代价的优化,数据库建立了各种代价模型,对各种物理路径进行代价的评估,选择一条代价相对较低的执行。物理路径包括扫描路径、连接路径等。
  4. 代价计算: 代价的计算需要用到统计信息和选择率。统计信息记录了表内数据的抽样信息,定期进行统计。根据统计信息,数据库可以计算出一个约束条件可以过滤掉多少数据,计算出选择率。根据选择率可以得到各种物理路径的代价。
  5. 连接路径: 嵌套循环连接、哈希连接、归并连接。

查询类语句的处理

postgresql对查询类语句的优化过程,除了语法分析过程和词法分析过程以外还需进行:

  1. 将原始语法树转换为查询语法树;

  2. 以查询语法树为基础对其进行逻辑优化;

  3. 对查询语句进行物理优化;

  4. 查询计划创建等过程。

具体流程介绍

根据本人知识沉淀以及相关资料的查阅,以下提供各位本人对查询类语句优化过程的理解

(1)将原始语法树转换为查询语法树

经过词法分析和语法分析后,postgresql需要将原始语法树转换为查询语法树并在转换过程中进行语义方面合法性检查。例如基表的有效性检查和目标列的有效性检查及展开等。transformStmt函数依据查询语句的类型进行相应语法树到查询书的转换工作。

(2)基于规则的查询改写

经过转换操作后,查询引擎获得Query类型的查询树,然后依据系统中定义的规则对查询树进行依据规则的改写操作。例如视图的改写等。

(3)

PostgreSQL读写分离架构设计与实践指南 PostgreSQL读写分离架构通过主从节点分工显著提升数据库性能,将70-90%的读请求分流到只读副本。核心采用WAL日志传输机制实现数据同步,提供异步/同步两种复制模式。主流实现方案包括客户端路由、中间件代理及云托管服务,各具特点。最佳实践需结合业务场景,金融系统适合半同步复制,电商平台可采用多数据源配置。关键优化策略涵盖延迟监控、智能负载均衡和安全控制,并需建立完善的监控告警体系。云原生趋势下,KubernetesOperator部署模式正成为新方向。 阅读详情

相关推荐

AI Agent技能执行安全实战:基于openclaw-agentlock的授权中间件设计与实现

在软件架构中,访问控制是保障系统安全的核心机制,其原理是通过定义和执行策略来管理主体对资源的操作权限。随着AI Agent技术的发展,智能体能够自主调用外部工具(技能)执行任务,这带来了全新的安全挑战:如何防止越权或危险操作?传统的基于角色的访问控制(RBAC)在动态、上下文丰富的Agent场景中显得力不从心,而基于属性的访问控制(ABAC)模型则能提供更精细的管控。其技术价值在于将安全逻辑与业务逻辑解耦,通过一个统一的、策略驱动的授权层,实现对AI Agent技能调用的实时拦截、鉴权与审计。这在智能客服、

weixin_33881140的博客 344

慢SQL优化In PostgreSQL

1、Create Index Directly 2、Change Conditions to Use Index 3、尽量避免在where子句中对字段进行运算,导致查询规划器放弃使用index 4、尽量避免在where子句中对字段类型进行强制转换,导致查询规划器放弃使用index 5、少用outer join,减少不必要的sub-query层级数【在不影响得到正确结果的前提下】 6、坚决避免select * 和 redundant columns【多余字段】 7、Index on Expressions 8、Partial Indexes 9、Decompose DDL【分解DDL】 10、Comprehensive optimization【综合优化】 11、索引的创建 12、查找需要删除的索引 13、查找重复的索引 14、查找需要维护的索引,并自定创建索引维护SQL 15、一个index size影响query plan的例子

探索niri:让你的Linux桌面布局无拘无束

niri是一款基于Wayland的滚动平铺合成器,采用创新的窗口管理方式。它将窗口排列为向右无限延伸的条目,支持独立显示器布局和动态垂直工作区,确保多任务操作流畅性。核心功能包括概要视图、截图界面、屏幕投射、手势操作、窗口分组等,并支持自定义布局和动画效果。niri具备良好的稳定性,兼容xwayland-satellite工具,灵感来自PaperWM但突破了GNOME限制。该项目为追求高效桌面管理的用户提供了全新解决方案,欢迎加入社区交流体验。

weixin_44626085的博客 1815

二维码扫描与页面跳转的完整流程解析

本文详细解析了二维码扫描与页面跳转的完整流程,从核心原理到前后端实现,涵盖了二维码生成、扫码事件处理、后端验证机制及跳转逻辑等关键环节。特别介绍了如何使用qrcode.js生成二维码、WebSocket处理扫码状态以及JWT令牌验证设计,帮助开发者构建安全高效的扫码跳转系统。

cicd6pipeline的博客 1088

深入解析PCIe Retimer:工作原理与典型应用场景

本文深入解析了PCIe Retimer的工作原理与核心价值。作为高速信号的中继再生器,Retimer通过时钟数据恢复和信号再生技术,有效解决长距离或复杂通道中的信号衰减与完整性问题,从而极大地扩展了PCIe链路的有效传输距离。文章详细阐述了其内部架构、自动与手动两种工作模式,并重点介绍了在长距离板间互连、复杂背板设计等典型应用场景中的关键作用。

weixin_29195615的博客 498

PTPX功耗分析实战——从脚本配置到报告解读

本文详细介绍了PTPX功耗分析的全流程,从脚本配置到报告解读,帮助工程师快速掌握芯片设计中的功耗估算技术。文章涵盖了环境搭建、波形文件处理、功耗报告解析等关键环节,并提供了实战经验和优化建议,特别适合需要提升PTPX功耗分析效率的开发者。

weixin_29248313的博客 279

CTP开发实战:如何高效处理预埋单与委托单的完整流程(附代码示例)

本文深入探讨了CTP开发中预埋单与委托单的完整处理流程,从订单生命周期理解、核心代码实现到异步事件处理与系统健壮性构建。通过详细的代码示例和状态机分析,为开发者提供了构建高可靠订单处理系统的实战指南,重点解决了状态同步、异常恢复等关键工程挑战。

weixin_29234239的博客 116

实验十一 ——————二层交换基础

实验内容实验网络拓扑图如上所示SW1以及SW2都创建VLAN10和VLAN20,按照上图所示,将接口添加到特定的VLAN;测试PC之间的连通性,要求相同VLAN内的PC能够相互通信要求设置两台交换机名称以及进行特权模式的密码,并允许通过Telnet方式管理交换机实验目的了解二层交换机工作原理掌握交换机基本配置命令理解VLAN的概念,掌握VLAN的配置理解trunk的概念,掌握trunk的配置实验步...

Fly_鹏程万里 1489

UG二次开发GRIP创建drill工序

UG二次开发GRIP创建drill工序

qq285503851的博客 1694

YOLO26涨点改进 | 小目标损失函数独家改进篇 | TGRS 2025 | 引入AdaFocalLoss损失函数,改进小目标检测,自适应聚焦小目标与样本分布不平衡,提升检测精度与训练稳定性

本文提出将AdaFocalLoss损失函数应用于YOLO26模型优化,有效提升小目标检测性能。AdaFocalLoss通过目标尺寸感知机制和IoU引导的动态调节,自动调整训练权重和聚焦程度,解决了样本不平衡问题。相比传统损失函数,它无需手动调参,能增强训练稳定性,在多尺度、复杂场景下表现优异。文章详细介绍了AdaFocalLoss的原理、优势及实现方法,并提供了代码集成指南。实验表明该方法能显著提升检测精度和鲁棒性,特别适用于小目标检测任务。

Ai缝合怪、助力小伙伴高效发论文 334

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: postgresql——执行计划查看方式与计划分析
下一篇: python——如何写一个python程序,如何执行(入门)
Walter Sun
博客等级 码龄9年 1726粉丝 288原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Walter Sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值