2

I was wondering if I would have problems if I base64(or other) encode an entire sql query, for example:

SELECT t.* FROM (SELECT chat.id as tempId, UuidFromBin(uuid) as id,userFrom,userTo,objDateTime,msg,msgType,idExternal,readed FROM chat WHERE talk=UuidToBin(xxxx-xxxxxxx-xxxxxxx-xxxxxxx) and 650 IN (userTo,userFrom) ORDER BY tempId DESC LIMIT 50) t ORDER BY t.tempId ASC

Base64 Encoded:

U0VMRUNUIHQuKiBGUk9NIChTRUxFQ1QgY2hhdC5pZCBhcyB0ZW1wSWQsIFV1aWRGcm9tQmluKHV1aWQpIGFzIGlkLHVzZXJGcm9tLHVzZXJUbyxvYmpEYXRlVGltZSxtc2csbXNnVHlwZSxpZEV4dGVybmFsLHJlYWRlZCBGUk9NIGNoYXQgV0hFUkUgdGFsaz1VdWlkVG9CaW4oeHh4eC14eHh4eHh4LXh4eHh4eHgteHh4eHh4eCkgYW5kIDY1MCBJTiAodXNlclRvLHVzZXJGcm9tKSBPUkRFUiBCWSB0ZW1wSWQgREVTQyBMSU1JVCA1MCkgdCBPUkRFUiBCWSB0LnRlbXBJZCBBU0M=

I noticed that comparing the length of the original string with the base64 encoded string, the last one was considerably increased, but some articles on internet says that redis keys can't have spaces, so encoding it would be a workaround.

Continuing... What do you guys thinks about using this string as a Redis key and as its value, I would store the entire query response as a simple json string.

This way, before hitting database, I would always check if that same query was recently executed. What do you guys think about performance in this kind of approach? Will I have problems or is this a good way of giving some breath to my database servers?

Éder Rocha
  • 1,538
  • 11
  • 29

1 Answers1

0

but some articles on internet says that redis keys can't have spaces

Wrong. Redis keys are binary safe, and it can contains any character.

Also, it's better have shorter keys for performance. You can use some hash function, e.g. md5, murmur hash, to make your original sql query shorter.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • I thought about using md5 but I was afraid that it wouldn't be sufficient to safely represent bigger query strings. – Éder Rocha Apr 28 '22 at 02:04