GreptimeTeam / greptimedb

An open-source, cloud-native, unified time series database for metrics, logs and events with SQL/PromQL supported. Available on GreptimeCloud.
https://greptime.com/
Apache License 2.0
4.39k stars 317 forks source link

LRU cache(with TTL) for prepared statements #1790

Open killme2008 opened 1 year ago

killme2008 commented 1 year ago

What type of enhancement is this?

Performance, Tech debt reduction

What does the enhancement do?

GreptimeDB caches the query or logical plan for prepared statements, It's a HashMap with RwLock in

https://github.com/GreptimeTeam/greptimedb/blob/128c6ec98c57996de08a12e958feb9695afa2204/src/servers/src/mysql/handler.rs#L54

We can use something like LRU cache which supports TTL to replace the naive implement.

Implementation challenges

No response

ccjeff commented 1 year ago

I would like to have a try if you don't mind :). Would also like to know a bit more about the requirement for the LRU cache to be implemented. I'm seeing in the current implementation a stmt_id is used to get/execute the sql plan, and it is passed a parameter for the on_execute function. Does the LRU cache still need this or it returns a sql plan according to the replacement policy? Thank you!!

evenyag commented 1 year ago

@ccjeff Seems we could simply replace the HashMap with some cache implementation like moka::sync::Cache.

TTL support: https://docs.rs/moka/latest/moka/sync/struct.Cache.html#example-time-based-expirations