简体   繁体   English

内存缓存中的SQL Server

[英]SQL Server in memory cache

I want to implement a simple in memory cache for an sql server 2008 R2 Express Edition. 我想为SQL Server 2008 R2 Express Edition实现一个简单的内存缓存。

Requirements: 要求:

  1. The cache must be visible for all users 缓存必须对所有用户可见
  2. The cache is synchronized with a persistent table. 缓存与持久表同步。 Over this persistent table might occur heavy CRUD operations. 在此持久表上可能会发生繁重的CRUD操作。

My solution: Create a bucket of CLR procedures that stands into a .NET assembly. 我的解决方案:创建一个存储在.NET程序集中的CLR过程。 These procedures will be named: 这些过程将被命名为:

  • Add (param1, ... paramN); 添加(param1,... paramN);
  • Remove (param1 ... paramN); 删除(param1 ... paramN);
  • GetAll (); 得到所有 ();

where param1... paramN is my persistent table data specific that i want to cache. 其中param1 ... paramN是我要缓存的特定于我的持久表数据。

Behind the scenes I will organize in memory data maybe into a HashSet for O (1) add / remove. 在幕后,我将在内存中组织数据,或者将数据整理到用于O(1)的HashSet中添加/删除。 Then, I will create a trigger for Insert / Delete operations (only inserts and deletes can occur on the persistent table) that would call these methods (Add / Remove). 然后,我将为插入/删除操作创建触发器(只能在持久性表上进行插入和删除),该触发器将调用这些方法(添加/删除)。 The big issue is that the transaction can be rolled back. 最大的问题是交易可以回滚。 How can I find out that the transaction with a specific ID was rolled back? 我如何才能发现具有特定ID的交易已回滚? There is a server level event that I can listen? 我可以听服务器级别的事件吗?

Please give me whatever alternatives you know. 请给我您知道的其他选择。 I know that SQL Server 2014 supports memory optimized OLTP tables but ... this feature is active only for SQL Server enterprise edition :D. 我知道SQL Server 2014支持内存优化的OLTP表,但是...此功能仅对SQL Server企业版:D有效。

Another alternative would be to create a temporary table with ## (visible on all connections) BUT: 另一种选择是使用##创建一个临时表(在所有连接上可见)但:

  1. The table will be stored in TEMPDB. 该表将存储在TEMPDB中。 If the tempDB is stored in RAM, there might be a performance boost but then it comes the second issue: 如果tempDB存储在RAM中,则可能会提高性能,但这是第二个问题:

  2. Searching in it (for a specific item) in this temporary table will cost me O ( Log ( N )) because behind the scenes if I create an index, there will be a B-tree. 在此临时表中搜索(查找特定项目)将使我付出O(Log(N))的代价,因为如果在后台创建索引,则会有B树。 My In memory alternative will ensure that lookups will cost O (1). 我的内存中替代方法将确保查找将花费O(1)。

If you have enough RAM, you're really not reading the data from disc, all your often accessed data will be in memory. 如果您有足够的RAM,您实际上并没有从磁盘读取数据,那么所有经常访问的数据都将存储在内存中。 I don't really know the limits of express edition in detail, so those could be causing issues you're facing. 我真的不太了解Express Edition的限制,因此这些可能会导致您面临问题。 I think you should at least consider upgrading to standard version, especially if you very big number of records, since you'll most likely run out of space too. 我认为您至少应该考虑升级到标准版本,特别是如果您有大量记录时,因为您也很可能会用完空间。

Temp DB is no exception, it will be either in disc or RAM, depending on how much memory you have and what is being accessed the most. 临时数据库也不例外,它将存储在磁盘或RAM中,具体取决于您拥有的内存量和访问量最大的内容。 Just creating cache 1:1 for data from other tables into tempdb is not useful, you'll just waste your memory for storing the same data twice. 只是为从其他表到tempdb的数据创建高速缓存1:1是没有用的,您只会浪费内存来两次存储相同的数据。

The in memory OLTP of SQL Server 2012 wouldn't probably help you at all. SQL Server 2012的内存OLTP可能根本无法帮助您。 The point of it is not that you have the data in memory, but to reduce the overhead of locking & latching etc. 关键不是要在内存中存储数据,而是要减少锁定和闩锁等开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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