Is there any command in Redis to get multiple key's values in a single query?
Actually, My Keys are all SETS so I want to get all of their value but as MEMBERS only take one KEY in argument, is this possible in a single query.
You cannot get the value of the multiple sets in one query. You have to query the database multiple times.
However, you can do operation which involves multiple sets using single query. The commands for this type of operations are:
SDIFF- Returns the members of the set resulting from the difference between the first set and all the successive sets.
SINTER- Returns the members of the set resulting from the intersection of all the given sets.
SUNION- Returns the members of the set resulting from the union of all the given sets.
I think following docs may help you: http://redis.io/commands/sunion
I faced such problem and found this ability of Redis.
If you need only values and don't need to know the key for value it is what you need.
I checked perfomance for more than 667 keys. And there are results below:
Method 1 is 667 sequential requests
Method 2 is 667 concurrent requests
Method 3 is using sunion
Related
I need to compare two columns declared as string in 2 different databases, all values of the columns, like 10000 rows at once.
One database is Firebird, and one is SQLite. I want to create some sort of checksum of two columns to see if there are exactly the same values in the two columns.
For integer, I can make a sum(column) and if the sum match from the tables, I can assume that it has the same values. This is not bulletproof, but if there are many records, the accuracy increases.
For numerical the same can be used. However, I cannot know how to make similar things to a string column.
I see several possibilities.
1. Per row comparison
The easiest is to check the values one row per one row from Delphi code. It has the huge benefit of finding immediately which data row is not synchronized.
But if has the drawback of reading the whole data from the DB.
2. Hash in SQL SELECT
If you want to do the check in-place, with no data retrieval, then you need to compute the hash of the columns.
Firebird 4 has cryptographic hash functions like MD5 or SHA1, and you may be able to find a suitable UDF library for older Firebird revisions. They are easy to implement with SQLite3, as a custom aggregate function.
Ensure you hash the UTF-8 text version of the columns, because binary/raw storage content is not compatible with the two DBs.
Then you could compute the hash of a column in two SELECT SQL statements.
3. Iterative approach
If the data is INSERTed into the DB, not UPDATEd, then you could add a "hash" field in each row. When a row is INSERTed, you retrieve the last hash (which may be cached in memory for efficiency), then you hash the new appended values.
Then you can easily compare the resulting hash of the last row, and check if data are the same on both DB.
An alternative may be to compute this hash in memory, not as field in the DB. It will only retrieve the data once at startup, then refresh it when something is INSERTed. It may be enough to check if two DBs are in synch.
I am looking to have a set which will store elements and that I can get the cardinality after. I noticed I could use the commands SADD or PFADD then use SCARD or PFCOUNT. What is the difference between these two? What are the advantages/disadvantages?
When using SADD, you store data in a SET.
When using PFADD, you store data in an Hyperloglog, which is a different kind of data structure.
A SET is used to store unique values, when you have to access again these values.
An HyperLogLog allows to get an approximate count of the number of unique values in the data added using PFADD. It is useful when you have a great number of distinct values and don't need to get them back. It may be used by example to get the number of unique visitors for a given day for a given page on a high traffic web site (you just add the unique visitor IDs to the HLL).
SADD and SCARD are for "Set".
PFADD and PFCOUNT are for "HyperLogLog".
Advantage of "HyperLogLog":
"HyperLogLog" takes much less memory than "Set".
This video below explains about "HyperLogLog" precisely in about 5 minutes.
https://youtu.be/UAL2dxl1fsE
I have a large map, which maps skuIds (strings, e.g.: AB-1 to "hola") to names; the skuIds are unique, but the names are not.
There are about 1 million skuIds mapped to about 1000 unique names. Now I need to get the unique name list for any subset of the skuId set.
I tried hashmap's hmget, but retrieving millions of records and looping through is not effiecient; then I tried using the Sorted sets, (kept the same score for same name), but then I needed the set of scores for a sorted set, which is not provided directly by redis.
We can do this by using a Lua script (taking about 10-15 seconds), but I am not sure about disadvantages of having Lua scripts in the code.
Use HSCAN for that. Also look at this answer about HSCAN usage.
I know barely anything about redis, except it is in-memory and fast.
But I have a case I consider using it.
I have a system, that may have a huge number of users (500k+ may up to a few million) and I want to do a unique check for email adresses across all users. I consider using redis to maintain a set of all email adresses to do the uniquness check. So I asked my self, is it possible to do something like
if(!set.contains(email)) add email
as an atomic operation and then get a simple result I can handle, just like failure or success.
This code/command should be callable from concurrent code.
If there is a different tool that would fit my needs better, I am open to suggestions.
Use SET datatype for that:
Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).
Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element. Practically speaking this means that adding a member does not require a check if exists then add operation.
So just use
SADD emails my#email.com
We have questions based where in home page we were showing 2 list
Questions by date modified
Question have bigger views and ans count. And in this both listing if question have same views or ans count then sorting is based on date.
Previously i am directly quiring to MySQL database and fetching the values so it's easy.
But each page request hitting to MySQL it's bit expensive then start doing caching.
I started using Redis. Following is the cases when i use redis cache
Issues is On second listing i have to display questions by votes and not answered combine.
How can i stored this type of data in redis to load faster with sorting based by 2 conditions votes with time and ans count with time?
You can use sorted sets in redis. Your view or answer count can be the score. create a key based on timestamp. Sorted set method zrevrangebyscore will give you the correct order.
you can set your member of sorted set as:
'YEAR_MONTH_DATE_HOUR_MINUTE_SECONDS:question_id'
This way if you sort, questions with same score, will be returned in lexicographical order. That way question which came later will be placed higher if you use zrevrangebyscore.
You can create a hash map to map timestamp and question_id. for faster lookup
I asked a similar question, where I also purposed a solution. I want something different but it will do exactly what you want.
Redis zrevrangebyscore, sorting other than lexicographical order