mongoDB's Query Optimizer [ not CBO ]

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
   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.

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
1月前
|
存储 NoSQL MongoDB
MongoDB如何创建数据库
MongoDB如何创建数据库
|
4天前
|
存储 NoSQL 关系型数据库
【MongoDB 专栏】MongoDB 与传统关系型数据库的比较
【5月更文挑战第10天】本文对比了MongoDB与传统关系型数据库在数据模型、存储结构、扩展性、性能、事务支持、数据一致性和适用场景等方面的差异。MongoDB以其灵活的文档模型、优秀的扩展性和高性能在处理非结构化数据和高并发场景中脱颖而出,而关系型数据库则在事务处理和强一致性上更具优势。两者各有适用场景,选择应根据实际需求来定。随着技术发展,两者正相互融合,共同构建更丰富的数据库生态。
【MongoDB 专栏】MongoDB 与传统关系型数据库的比较
|
8天前
|
存储 NoSQL 关系型数据库
MongoDB非关系型数据库实战
【5月更文挑战第6天】MongoDB,流行的NoSQL数据库,以其灵活的数据模型和高性能备受青睐。本文介绍了MongoDB的基础,包括文档型数据库特性、安装配置、数据操作。通过电商订单管理的实战案例,展示了MongoDB在处理复杂数据结构和大规模数据时的优势,适用于电商、游戏、视频直播等场景。MongoDB的索引、全文搜索和地理空间功能进一步增强了其实用性。注意性能优化和扩展性以确保系统稳定性和可靠性。
|
15天前
|
弹性计算 NoSQL Shell
一键安装 MongoDB 数据库脚本
【4月更文挑战第29天】
18 4
|
21天前
|
NoSQL MongoDB 数据库
MongoDB数据恢复—MongoDB数据库文件被破坏的数据恢复案例
服务器数据恢复环境: 一台Windows Server操作系统服务器,服务器上部署MongoDB数据库。 MongoDB数据库故障&检测: 工作人员在未关闭MongoDB数据库服务的情况下,将数据库文件拷贝到其他分区。拷贝完成后将原MongoDB数据库所在分区进行了格式化操作,然后将数据库文件拷回原分区,重新启动MongoDB服务,服务无法启动。
|
24天前
|
NoSQL MongoDB Redis
Python与NoSQL数据库(MongoDB、Redis等)面试问答
【4月更文挑战第16天】本文探讨了Python与NoSQL数据库(如MongoDB、Redis)在面试中的常见问题,包括连接与操作数据库、错误处理、高级特性和缓存策略。重点介绍了使用`pymongo`和`redis`库进行CRUD操作、异常捕获以及数据一致性管理。通过理解这些问题、易错点及避免策略,并结合代码示例,开发者能在面试中展现其技术实力和实践经验。
315 8
Python与NoSQL数据库(MongoDB、Redis等)面试问答
|
1月前
|
存储 SQL NoSQL
mongodb数据库使用
mongodb数据库使用
|
1月前
|
存储 SQL NoSQL
【MongoDB】如何区分MongoDB和关系型数据库?
【4月更文挑战第1天】【MongoDB】如何区分MongoDB和关系型数据库?
|
1月前
|
存储 NoSQL 物联网
【MongoDB】MongoDB 数据库概述
【4月更文挑战第1天】【MongoDB】MongoDB 数据库概述
【MongoDB】MongoDB 数据库概述
|
2月前
|
NoSQL 网络协议 MongoDB
Windows公网远程连接MongoDB数据库【无公网IP】
Windows公网远程连接MongoDB数据库【无公网IP】