简体   繁体   中英

Mapping primary keys in MySQL to Redis

I have a mysql db, with a table having attributes: p1,p2, p3, c1, c2, c3, where p1, p2, and p3 are the primary keys. Now the problem is, I want to map this to Redis in a "time-efficient" manner, and if possible, "space-effective" too. This is what thought:

Scenario 1: p1 -> HashMap (c1, c2, c3)
p2 -> p1
p3 -> p1

But here, I need two operations (66% probability) to fetch the required c1, c2, c3 values. Extra space taken by p1 (two times) is also an issue.

Scenario 2: p1:p2:p3 -> HashMap(c1, c2, c3)

But issue here is that I cannot fetch the hashmap without having known all the primary keys.

In short, my objective is to fetch c1, c2 and c3 in a time effective manner, using either of p1, p2 or p3(not all together).

Take the first approach. True, it will cost more in terms of memory and yes, you'll have to do two operations in 66% of the cases (assuming a uniform distribution of p1, p2, p3 values to fetch by) but the alternative, as you had pointed out, is having all p's in the key's name. The alternative, while technically feasible (you could, for example fetch all keys from the database [using SCAN of course, not KEYS] and filter them in the application to find the one you need according the p that you have), is much more expensive in terms of operations/performance.

Put differently, this is a classic case of the Space-Time tradeoff .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM