开发者

versioning an sql query

开发者 https://www.devze.com 2023-01-31 16:38 出处:网络
I have a farm of servers, each server is regularly making an identical query to the database开发者_JS百科.

I have a farm of servers, each server is regularly making an identical query to the database开发者_JS百科. These query results are cached in a shared cache, accessible to all servers.

How can I ensure that a newer query does not get overwritten by an older query? Is there a way of versioning the queries somehow, by time for example, so that this doesn't happen? How to deal with concurrent queries?

Thanks!

Edit: db is SQL Server. Query is a select statement. And, caching mechanism is very simple: simple write, with no locking. And that is because there is currently no way of telling the order of the select queries.


One approach is to have a global update counter in the database, either for updates or for reads (updates is more efficient, but also harder to get right).

So on each update, also increment the global counter. On each read, read the global counter, and put the data into the cache along with the counter value. Only overwrite the cache contents if the counter value is larger.

Because of database isolation, transactions should appear as if they happened in a serial manner (assuming you have chosen the SERIALIZABLE isolation level). That, in turn, will mean that strictly higher counter numbers relate to more recent data.


Where is the cache? Is it a disk file? Or a table in the database?

And what does it mean when you say an older query might overwrite a newer one? Isn't it true that the most-recently-completed query (or maybe the most-recently-started) is the newest one?

You should lock the cache container before performing each query, whether that's a file or a table. Each server should only perform the query if it can obtain the lock, otherwise it should wait for the locked resource. That way the cache will contain only the most recent results.

Is that along the lines of what you are asking?

0

精彩评论

暂无评论...
验证码 换一张
取 消