【问题标题】:Improve the speed of a realm query in react-native?提高 react-native 中领域查询的速度?
【发布时间】:2017-09-24 21:08:41
【问题描述】:

我的 react 本机应用程序中有类似以下代码来设置模拟/测试数据以进行性能测试。

realm.write(() => { 
    const max = 120;
    for(let x=1; x<=max; x++)
    {
        realm.create('Product', {productId:x});
    }

    for(let x=1; x<=max; x++)
    {
        for(let y=x; y<=max; y++)
        {
            for(let z=y; z<=max; z++)
            {
                realm.create('Compatibility', {
                    result: 'Y '+x+' '+y+' '+z,
                    products: [
                    realm.objects('Product').filtered('productId = '+x)[0],
                    realm.objects('Product').filtered('productId = '+y)[0],
                    realm.objects('Product').filtered('productId = '+z)[0]
                    ]
                });
            }
        }
    }
});

class Product {}
Product.schema = {
    name: 'Product',
    primaryKey:'productId',
    properties: {
        productId:'int'
    }
};

class Compatibility {}
Compatibility.schema = {
    name: 'Compatibility',
    properties: {
        result: {type: 'string'},
        products: {type: 'list',objectType:'Product'},
    }
};

这意味着 Products 对象有 120 条记录,而 Compatibility 对象有 170 万条记录。

当我运行查询 realm.objects('Compatibility').filtered(products.productId = 3 AND products.productId = 25 AND products.productId = 97) 时,在我的旧 HTC Desire 510 和我的华为 Nova Plus 上运行大约需要 15 秒。这太慢了。

有没有办法提高查询速度?例如,你能索引列吗?

【问题讨论】:

    标签: react-native realm


    【解决方案1】:

    首先,领域中有索引,primaryKeys 已经被索引。因此,在这种情况下建立索引对您没有帮助。但我想我知道如何加快流程。

    在最后一个 for 循环中,您正在执行 3 个查询。我认为其中 2 个不必要地发生,因为 x 和 y 值对于 120 个 z 值将是相同的。如果你实现类似下面的代码,它可能对我认为的性能有所帮助。

    let productX;
    let productY;
    let productZ;
    
    for (let x = 1; x <= max; x++)
    {
        productX =  realm.objects('Product').filtered('productId = ' + x)[0];
        for (let y = x; y <= max; y++)
        {
            productY =  realm.objects('Product').filtered('productId = ' + y)[0];
            for (let z = y; z <= max; z++)
            {
                productZ =  realm.objects('Product').filtered('productId = ' + z)[0];
                realm.create('Compatibility',
                {
                    result: 'Y ' + x + ' ' + y + ' ' + z,
                    products: [ productX, productY, productZ]
                });
            }
        }
    }
    

    不过一秒钟;

    这可能是一个非常糟糕的主意,并且可能是一种糟糕的做法,但我将把它作为一种思想实践。

    如果您总是使用 3 个不同的 productIds 进行查询,您可以在单个属性中创建一个包含所有树的字符串并仅查询该属性。这样你就可以使用索引了。

    示例

    class Compatibility {}
    Compatibility.schema = {
        name: 'Compatibility',
        properties: {
            result: {type: 'string'},
            productQueryHelper: { type: 'string', indexed: true }
            products: {type: 'list',objectType:'Product'},
        }
    };
    
    realm.create('Compatibility',
    {
        result: 'Y ' + x + ' ' + y + ' ' + z,
        productQueryHelper: `${x}&${y}&${z}` // you can use any other separator that isn't possible to be in productId
        products: [
            realm.objects('Product').filtered('productId = ' + x)[0],
            realm.objects('Product').filtered('productId = ' + y)[0],
            realm.objects('Product').filtered('productId = ' + z)[0]
        ]
    });
    
    realm.objects('Compatibility').filtered('productQueryHelper = "3&25&97"')
    

    【讨论】:

    • 我更新了我的问题,明确表示我只对查询性能感兴趣。对于您的 productQueryHelper,我可以查询可变数量的 productId。有时我只查询 1 个 productId,其他时候我可能会查询 10 个或更多 productId。我猜 productQueryHelper 不灵活地支持可变数量的过滤参数?
    • @John 在那种情况下,我认为不是。很抱歉,它没有帮助。
    【解决方案2】:

    尝试将您的主键设置为索引。

    顺便说一句,我在使用 Realm 时从未遇到过性能问题。 现在我在一个场景中使用 Realm 来管理我的通知。我有很多查询在某个时间运行,这不会影响性能。

    class Product {}
    Product.schema = {
        name: 'Product',
        primaryKey:'productId',
        properties: {
            productId: { type: 'int', indexed: true }
        }
    };
    
    class Compatibility {}
    Compatibility.schema = {
        name: 'Compatibility',
        properties: {
            result: {type: 'string'},
            products: {type: 'list',objectType:'Product'},
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2019-09-27
      • 2012-03-31
      相关资源
      最近更新 更多