简体   繁体   English

Optic api, existsJoin by two or more columns 导致性能下降

[英]Optic api, existsJoin by two or more columns causes a decrease in performance

For such input data, I want to obtain a result in which I will have only those values that for the same id1 and id2 have the value of Reported and Primary.对于这样的输入数据,我想获得一个结果,在该结果中我将只有那些对于相同的 id1 和 id2 具有 Reported 和 Primary 值的值。

var items = [
    {
      "id1": 241,
      "id2": 716,
      "type": "Primary"
    },
    {
      "id1": 241,
      "id2": 716,
      "type": "Reported"
    },
    {
      "id1": 477,
      "id2": 850,
      "type": "Reported"
    },
    {
      "id1": 563,
      "id2": 340,
      "type": "Primary"
    },
    {
      "id1": 649,
      "id2": 322,
      "type": "Reported"
    }];

I try to use exists join like below:我尝试使用 exists join 如下所示:

const op = require('/MarkLogic/optic');

var reportedItems = op.fromLiterals(items)
    .where(op.in(op.col('type'), 'Reported'))
    .select(['id1', 'id2'], 'reported');

var primaryItems = op.fromLiterals(items)
    .where(op.in(op.col('type'), 'Primary'))
    .select(['id1', 'id2'], 'primary')

primaryItems
    .existsJoin(reportedItems, [
        op.on(op.viewCol('primary', 'id1'),
            op.viewCol('reported', 'id1')),
        op.on(op.viewCol('primary', 'id2'),
            op.viewCol('reported', 'id2'))
    ])
    .select("id1")
    .result()
    .toArray()

However, when the number of items in the input data exceeds 5000, the execution time exceeds 2 seconds.但是,当输入数据的项目数超过5000时,执行时间超过2秒。 If, however, I perform a join only on one property (eg id1), the result will almost immediately appear (for 5k objects 0.3s).但是,如果我只对一个属性(例如 id1)执行连接,结果几乎会立即出现(对于 5k 个对象 0.3s)。

Why is it so slow, can I improve it in some way or use another method to obtain the same result?为什么这么慢,我可以通过某种方式改进它或者使用其他方法来获得相同的结果吗?

I expect this should give the same result, and in my testing it seems to be much faster:我希望这会给出相同的结果,而且在我的测试中它似乎要快得多:

'use strict';

const op = require('/MarkLogic/optic');

var reportedItems = op.fromView('test', 'exists')
    .where(op.in(op.col('type'), 'Reported'))
    .select(['id1', 'id2'], 'reported')
    .whereDistinct();

var primaryItems = op.fromView('test', 'exists')
    .where(op.in(op.col('type'), 'Primary'))
    .select(['id1', 'id2'], 'primary')
    .whereDistinct()

primaryItems
  .joinInner(reportedItems, [
    op.on(op.viewCol('primary', 'id1'),
      op.viewCol('reported', 'id1'))
  ],
    op.eq(op.viewCol('primary', 'id2'),
      op.viewCol('reported', 'id2'))
  )
  .select(op.viewCol('primary', 'id1'), '')
  .result()

What I think is happening in the original query is that the existsJoin is performing a cross product join of primary and reported , and using that result to filter primary .我认为在原始查询中发生的是existsJoin正在执行primaryreported的交叉产品连接,并使用该结果来过滤primary Because the cross product is so large, the filter takes a long time to execute.因为叉积太大,过滤器需要很长时间才能执行。

It might be possible to omit whereDistinct if you know you don't have any duplicate rows - my data was randomly generated for testing so I had to use it.如果您知道自己没有任何重复行,则可以省略whereDistinct - 我的数据是随机生成的用于测试,因此我不得不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM