使用 MongoDB API 和 Oracle 自治 JSON 数据库调试缓慢的操作

问题描述 投票:0回答:1

我将 Oracle API for MongoDB 与自治数据库结合使用。 一些查询/聚合管道的性能没有达到我的预期。 如何提高性能?

json database mongodb oracle oracle-autonomous-db
1个回答
0
投票

这种情况下的性能问题通常是由于缺少索引造成的。 诊断这些问题的最佳方法是使用 Oracle SQL 监控。 在幕后,Oracle API for MongoDB 将所有 MongoDB 命令转换为 SQL 操作。 您可以指定MongoDB命令的提示来生成底层SQL的监控报告。 此示例假设您使用 mongosh 连接到数据库。

确保您拥有必要的角色。

来自 SQL 管理用户:

grant select on "V$SQL" to your_user;
grant select on "V$SQL_MONITOR" to your_user;
grant advisor to your_user; 
grant execute on dbms_sql_monitor to your_user;

添加MONITOR提示

mongosh> db.movie.aggregate([{$match : {year:2000}}], {hint :{$native : "MONITOR"}});

mongosh> db.movie.find({$match : {year : 2000}}).hint({$native : "MONITOR"});

确定 SQL_ID

以下查询可用于确定您刚刚执行的命令的 SQL_ID。

mongosh> db.aggregate([{$sql:`
       select sql_fulltext, sql_id 
       from v$sql 
       where sql_text like '%MONITOR%' and
             sql_text not like '%v$sql%'
       order by last_active_time desc
       fetch first 1 rows only
   `}]);
 [
   {
     SQL_FULLTEXT: '...',
     SQL_ID: '6xy9y446n1ha6'
   }
 ]

生成报告

mongosh> var html = 
   db.aggregate([{$sql:`
     select dbms_sql_monitor.report_sql_monitor(
       sql_id => '6xy9y446n1ha6',
       report_level => 'ALL',
       type => 'ACTIVE'
     ) as "html"
     from dual
   `}]).toArray()[0].html;

mongosh> require('fs').writeFileSync('/Users/jspiegel/out.html', html);

打开报告

在网络浏览器中打开

/Users/jspiegel/out.html

报告显示详细信息包括:

  • 为命令生成的 SQL
  • 运行时计划(例如索引扫描与全表扫描)
  • 计划的每个部分花费了多少时间
  • 计划的每个部分返回了多少文档/行
© www.soinside.com 2019 - 2024. All rights reserved.