Given a single instance redis which supports lua scripts. Is there any performance difference between calling ‘mget’ once and calling ‘get’ multiple times to retrieve the value of multiple keys?
Given a single instance redis which supports lua scripts. Is there any performance difference between calling ‘mget’ once and calling ‘get’ multiple times to retrieve the value of multiple keys?
2
Answers
It will be different in some cases.
MGET key [key …] Time complexity: O(N) where N is the number of keys to retrieve.
GET key Time complexity: O(1)
According to time complexity, GET is more efficient.
However, there is a difference between passing a single command to redis and passing it multiple times.
If you don’t use pool, the difference is even greater.
Of course, what I would recommend is to use it depending on the type of data you want to deal with.
You decide whether it’s more efficient to manage your data in Maps.
Time-complexity-wise, both result in the same:
O(N) = N*O(1)
.But there is overhead associated with processing each command and parsing the result back to Lua. So
MGET
will give you better performance.You can measure this. The following scripts receive a list of keys, one calls
GET
multiple times, the other one callsMGET
.Calling
GET
multiple times:Calling
MGET
once:Calling
GET
multiple times took 51 microseconds, vsMGET
once 20 microseconds: