mongoDB's Query Optimizer [ not CBO ]

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介:
   mongodb 的优化器为每个客户端提交的查询产生所有的执行计划后并行执行所有的执行计划,最先执行完毕的执行计划将被选出并缓存起来,没有执行完的执行计划将丢弃。下次执行时mongoDB将匹配缓存中的执行计划。
   在mongoDB中,被选出的较优的执行计划,在后期的执行中如果不优了(如因为数据的变更或者查询传入参数的改变,没有达到预期的执行效率)怎么办?mongoDB自动感知到一个执行计划已经不优了的时候,将重新选择并缓存更优的执行计划。
   mongoDB的这种不是基于cost和统计信息的优化选择器有一定的好处,但是也给数据库带来一定的负担。哪个好哪个不好确实不好定论。
   使用CBO的数据库如PostgreSQL,在对数据表进行大批量操作后通常建议重新analyze该表(即收集统计信息)。Oracle也是如此,如果统计信息不正确将导致数据库选择较劣的执行计划给数据库带来严重负担。
    而mongoDB没有统计信息当然也谈不上analyze,如果大批量更新数据导致执行计划不对的话按照mongoDB的说法应该下一次执行就会正常了。但是对于传入参数不一样导致的执行计划不对是不是会出现执行计划紊乱呢?解决办法除了重业务层隔离数据以外,使用非绑定变量也行。不过会给CPU带来严重负担。

【原文】

The MongoDB query optimizer generates query plans for each query submitted by a client.  These plans are executed to return results. Thus, MongoDB supports ad hoc queries much like say, MySQL.

The database uses an interesting approach to query optimization though. Traditional approaches (which tend to be cost-based and statistical) are not used, as these approaches have a couple of problems.

First, the optimizer might consistently pick a bad query plan.  For example, there might be correlations in the data of which the optimizer is unaware.  In a situation like this, the developer might use a query hint.

Also with the traditional approach, query plans can change in production with negative results.  No one thinks rolling out new code without testing is a good idea.  Yet often in a production system a query plan can change as the statistics in the database change on the underlying data.  The query plan in effect may be a plan that never was invoked in QA.  If it is slower than it should be, the application could experience an outage.

The Mongo query optimizer is different.  It is not cost based -- it does not model the cost of various queries.  Instead, the optimizer simply tries different query plans and learn which ones work well.  Of course, when the system tries a really bad plan, it may take an extremely long time to run.  To solve this, when testing new plans, MongoDB executes multiple query plans in parallel.  As soon as one finishes, it terminates the other executions, and the system has learned which plan is good. This works particularly well given the system is non-relational, which makes the space of possible query plans much smaller (as there are no joins).

Sometimes a plan which was working well can work poorly -- for example if the data in the database has changed, or if the parameter values to the query are different.  In this case, if the query seems to be taking longer than usual, the database will once again run the query in parallel to try different plans.

This approach adds a little overhead, but has the advantage of being much better at worst-case performance.

目录
相关文章
|
计算机视觉
Mac-Mini 外接三个显示器教程
Mac-mini 官方只支持连接2个显示器,ctrl cv 复制粘贴代码和写博客的时候十分不方便,这时候需要外接第三个显示器,下面看看如何操作。
1583 0
Mac-Mini 外接三个显示器教程
【全是精华】Token的获取和使用-FastApi版
【全是精华】Token的获取和使用-FastApi版
1443 0
|
9月前
|
边缘计算 网络安全 数据安全/隐私保护
从当前创新看HTTP代理的未来发展格局
在数字化时代,网络安全与隐私保护成为核心需求。HTTP代理凭借智能化、安全性、边缘计算等创新技术,支持HTTP/2、HTTP/3等新兴协议,优化用户体验,提升数据传输安全性和效率,满足现代网络需求。
141 13
|
人工智能 前端开发 搜索推荐
前端技术发展趋势分析与展望
【2月更文挑战第9天】 随着移动互联网的快速发展,前端技术在不断演进,从最初的HTML、CSS到如今的React、Vue等前端框架,技术发展日新月异。本文将从移动端、跨平台、人工智能等多个角度分析前端技术的发展趋势,并展望未来的发展方向。
|
12月前
|
小程序 开发者 UED
支付宝小程序UI/UX设计原则与最佳实践
支付宝小程序UI/UX设计原则与最佳实践
475 6
什么是能源转换技术?
什么是能源转换技术?
409 3
|
传感器 Linux API
远程处理器消息框架(RPMSG) 【ChatGPT】
远程处理器消息框架(RPMSG) 【ChatGPT】
|
安全 iOS开发 MacOS
Dolphin指纹浏览器+IPXProxy代理IP:配置与使用全流程
通过代理IP,用户可以轻松绕过地域限制,访问全球范围内的网站和服务。不过想要同时使用多个代理IP的话,就需要借助指纹浏览器。Dolphin指纹浏览器和IPXProxy代理IP是大家常用的组合,这个组合为用户打造了一个既安全又高效的在线环境。下面是Dolphin指纹浏览器配置IPXProxy代理IP使用步骤,帮助大家更好了解这个组合。
468 3
|
程序员 Python
利用Python实现科学式占卜
一直以来,中式占卜都是基于算命先生手工实现,程序繁琐(往往需要沐浴、计算天时、静心等等流程)。准备工作复杂(通常需要铜钱等道具),计算方法复杂,需要纯手工计算二进制并转换为最终的卦象,为了解决这个问题,笔者基于python实现了一套科学算命工具,用于快速进行占卜。 本文的算命方式采用八卦 + 周易+ 梅花易数实现,脚本基于python3.9.0开发。本人对于周易五行研究较浅,如有疏漏请见谅。 最终效果如图,在运行程序之后,会根据当前的运势自动获取你心中所想之事的卦象(本卦、互卦、变卦) 前置知识 基础原理 首先我们需要了解一些最基本的占卜知识,目前我国几种比较主流的占卜方式基本都是基
206 0
|
DataWorks 关系型数据库 数据库
DataWorks操作报错合集之运行报错"system internal error",该如何排查
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。