在react-native中提高领域查询的速度吗? [英] Improve the speed of a realm query in react-native?

查看:78
本文介绍了在react-native中提高领域查询的速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的react本机应用程序中,我有类似以下代码的内容来为性能测试设置模拟/测试数据.

I have something like the following code in my react native app to set up mock/test data for performance tests.

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'},
    }
};

这意味着产品"对象具有120条记录,而兼容性"对象具有170万条记录.

This means the Products object has 120 records and the Compatibility object has 1.7 million records.

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

When I run the query realm.objects('Compatibility').filtered(products.productId = 3 AND products.productId = 25 AND products.productId = 97), it takes about 15 seconds to run on my old HTC Desire 510 and my Huawei Nova Plus. This is too slow.

是否有提高查询速度的方法?例如,您可以索引列或其他内容吗?

Is there a way to improve the speed of the query? For example, can you index the columns or something?

推荐答案

尝试将主键设置为已索引.

Try to set your primary keys as indexed.

顺便说一句,我从未使用Realm遇到过性能问题. 如今,我在方案中使用Realm来管理我的通知.我有时会运行很多查询,但这从不影响性能.

Btw I've never had problems with performance using Realm. Nowadays I'm using Realm in a scenario to manage my notifications. I have a lot of queries running at some time and this never hurt the performance.

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'},
    }
};

这篇关于在react-native中提高领域查询的速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆