最近在用servicestack.redis,需要从IRedisTypedClient
查询.我知道所有数据都在内存中,但仍然想知道 GetAll().Where()
之间的速度是否不同?和 GetByIds()
?
GetAll()
和 GetByIds()
是servicestack.redis提供的两个方法。
使用 GetAll()
可以继续在结果中搜索(通过 lambda),这意味着我可以使用一些自定义条件,但我不知道,这是否会从 redis 内存中加载所有数据,然后在 IEnumable<T>
中搜索, 搜索速度会不会比GetByIds()
慢.
最佳答案
我刚刚做了一个实验,我存储了100万个对象(ps:servicestack有一个bug,一次只能存储大约50万个对象)。
用这两种方法查询。
DateTime beginDate = DateTime.Now;
Debug.WriteLine("查询1开始");`
Website site = WebsiteRedis.GetByCondition(w => w.Name == "网址2336677").First();
double time = (DateTime.Now - beginDate).TotalMilliseconds;
Debug.WriteLine("耗时:" + time + "ms");
DateTime beginDate2 = DateTime.Now;
Debug.WriteLine("查询2开始");
Website site2 = WebsiteRedis.GetByID(new Guid("29284415-5de0-4781-bea4-5e01332814b2"));
double time2 = (DateTime.Now - beginDate2).TotalMilliseconds;
Debug.WriteLine("耗时:" + time2 + "ms");
结果是
GetAll().Where()
- 需要 19 秒,
GetById()
- 需要 190 毫秒。
我猜是因为 servicestack
使用对象 id 作为 redis
的键,所以永远不要使用 GetAll().Where()
作为查询,每个对象应与 id 相关并使用 GetById()
作为查询。 GetAll()
应在记录较少的对象上使用。
关于c# - servicestack.redis GetAll().where() 和 GetByIds() 之间有速度差异吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38970305/