--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.
in order to make things more clear I want to show Marcos what is the
obvious technique to use with sorted sets in order to get things in a
given range.
For instance I've two ranges:
A_start 10, A_end 20
B_start 30, B_end 40
I can add those ranges to single sorted set:
redis 127.0.0.1:6379> zadd ranges 10 A_start
(integer) 1
redis 127.0.0.1:6379> zadd ranges 20 A_end
(integer) 1
redis 127.0.0.1:6379> zadd ranges 30 B_start
(integer) 1
redis 127.0.0.1:6379> zadd ranges 40 B_end
(integer) 1
I've the value of 15, and want to check in what range it is:
redis 127.0.0.1:6379> zrangebyscore ranges (15 +inf LIMIT 0 1
1) "A_end"
As you can see I queried for elements that have a score between 15 and
+inf (infinite). I prefixed 15 with "(" in order to tell Redis I want
elements > 15, and not >= 15.
This way if I get as a result an "*_end" value I know I have a match.
If instead I get no elements or a *_start element I know I've no
match.
For instance 25 is not in the right interval, so if I query Redis I obtain:
redis 127.0.0.1:6379> zrangebyscore ranges (25 +inf LIMIT 0 1
1) "B_start"
I obtained a _start, so I'm not inside a range.
I hope this makes it clear how to do this kind of range queries with Redis.
About performances, you'll be surprised about the speed Redis can
serve this queries, in the order of tens of thousands per seconds in
an entry level virtual machine, even if you have millions of ranges.
Cheers,
Salvatore
--
Salvatore 'antirez' Sanfilippo
open source developer - VMware
http://invece.org
"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." -- Aristotele
Marcos
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.
Hello Dvir, your trick is absolutely more memory efficient, thanks!
> BTW, I used a similar trick to resolve lat,lon pairs - I converted them into
> geohash - a 64 bit (or a bit less) number, that allows me to use a single
> index to find adjacent coordinates.
> it's a really cool algorithm, you can read more about it here:
> http://en.wikipedia.org/wiki/Geohash
Cool, I hope to add those two patterns in the Redis book patterns section.
This problem is easily solvable in the pattern I showed you, but if
you use the variant proposed by Dvir I think it is even simpler. In
the example of three ranges: 10-20, 30-40, 22-22:
We populate the ranges with:
redis 127.0.0.1:6379> zadd ranges 20 10-20
(integer) 1
redis 127.0.0.1:6379> zadd ranges 40 30-40
(integer) 1
redis 127.0.0.1:6379> zadd ranges 22 22-22
(integer) 1
Let's check if 5 is inside some range:
redis 127.0.0.1:6379> zrangebyscore ranges 5 +inf LIMIT 0 1
1) "10-20"
5 is not included in the range Redis returned, so no match.
If we use 15 instead we get again "10-20" but this time we can verify
that our number is included.
Now let's try with 22:
redis 127.0.0.1:6379> zrangebyscore ranges 22 +inf LIMIT 0 1
1) "22-22"
That's obviously included.
As Dvir pointed out, this technique also uses less memory.
Cheers,
Salvatore
--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.