我的一个朋友说他的超市管理层正在考虑去一个 分布式缓存系统 以提高其电子商务应用程序的性能。 但他不确定这个分布式缓存是否会为他们提供以下能力: 使用 SQL 搜索数据. 原因是他们的大部分在线商店功能都依赖于搜索数据,他们无力重写他们的应用程序。 而且,如果他们所有的 SQL 查询最终仍然进入数据库,那么它就违背了使用分布式缓存的目的。
如果您遇到类似情况并需要快速可靠地解决所有性能问题,那么 NCache 正是您所需要的。 请继续阅读以了解更多信息。
缓存为数据存储
现在,如果我们将数据存储在缓存中以使其可以更快地使用,我朋友解释的问题仍然存在; 客户想要查询库存以搜索所需的产品。 好吧,我有好消息要告诉你(和他); NCache 完全配备了 SQL,它允许您 查询缓存中的数据 与您在数据库中的方式相同。
图1: NCache 处理 SQL 查询负载
等等……等等! 这不是突然解决了我们所有的问题吗? 如图 1 所示,整个产品目录存储在缓存中,我们可以对其执行 SQL 查询并将结果直接返回给应用程序——这显着减少了对数据表的总访问次数,因为 NCache 可扩展且可靠,一次可以处理数十万客户!
中的 SQL 查询 NCache – 快速示例
假设一个客户有他经常消费的产品的产品 ID,请注意下面的查询,这将在缓存中执行(而不是在数据库中)并将结果发送到应用程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Pre-condition: Cache is already connected // Items are already present in the cache. // Provide Fully Qualified Name (FQN) of your custom class string query = "SELECT ProductName FROM FQN.Product WHERE ProductID > ?"; /// Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("ProductID",50000); // Executing QueryCommand through ICacheReader ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if the result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { string result = reader.GetValue("ProductName"); // Perform operations } } |
问题是,缓存中的这些 SQL 查询非常有用,但它们毕竟是在缓存中工作的; 所以…
在缓存中搜索什么类型的数据?
NCache 有两大类存储数据。 这些是交易数据和参考数据。
交易数据
交易数据是为了满足请求而被视为单个单元的数据。 这些作为查询结果从数据库中收集并存储在缓存中。 例如,给定数据库中的表,客户要求所有价格低于 5 美元的产品,结果存储在缓存中的输出是交易数据。
图 2:数据库中的 Products 表
1 |
SELECT * FROM Products WHERE UnitPrice <= 5.00 |
图 3:缓存中存储的查询结果
事务数据通常临时存储以供处理。 它经常更新,旧的和不必要的数据被丢弃。 单个对象不需要 SQL,而是对对象数据使用关键字搜索。 事务数据已经是查询结果数据; 因此,无法对其执行 SQL 查询。
参考资料
NCache 允许您将大量数据在缓存中存储更长的时间。 这种数据称为参考数据。 参考数据以结构化和关系形式存储,并定期更新并与数据库同步。 由于这些原因,可以在分布式缓存中对这些数据执行 SQL 查询。
随着执行 NCache,客户所做的大部分查询都在缓存中执行并立即返回结果。 这极大地减少了对缓存的访问次数,从而消除了对数据库的过多访问,从而消除了所有性能瓶颈。
NCache 更多信息 中的 SQL 语法 NCache NCache SQL 文档
中的 SQL 功能 NCache
NCache 提供广泛的 SQL函数 以及允许您在缓存中搜索和删除数据的运算符。 请注意,由提供的 SQL 扩展 NCache 不支持插入和更新命令。 这些命令直接在数据库中实现。
让我们来看看一些独特的内存中 SQL 特性 NCache.
在中使用 SQL 通配符 NCache
NCache 在 SQL 函数中使用两个不同的通配符提供灵活的搜索 样. 这些是 '?' 和 '*'
“?” 用于前面有一个字符需要搜索的地方。 例如,“shar?”。 这将搜索缓存并可能给出共享、清晰或任何其他类似结果。
“*”用于前面搜索的字符数可能为零到任意数量的情况。 例如,“cha*”可以给出 char、character、chair 或任何其他类似的结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Pre-condition: Cache is already connected // Items are already present in the cache // Use the Fully Qualified Name (FQN) of your own custom class string query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("ProductName", "Cha?"); queryCommand.Parameters.Add("Category", "Edib*"); // Executing QueryCommand through ICacheReader ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { string result = reader.GetValue("ProductID"); // Perform operations } } |
在中使用 SQL 聚合函数 NCache
汇总功能 在 SQL 中用于对多个值执行算术运算并返回一个奇异值。 NCache 使用这些聚合函数对缓存的数据项执行操作。
支持的一些聚合函数 NCache 是:
总和、计数、平均值、最小值、最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Pre-condition: Cache is already connected // Items are already present in the cache // Use the Fully Qualified Name (FQN) of your own custom class string query = "SELECT COUNT(*) FROM FQN.Products WHERE UnitPrice > 5"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set int result = reader.GetValue(1); } } |
在中使用 SQL GROUP BY NCache
NCache 使您能够根据您提供的标准使用 通过...分组 功能。 但是有一个问题,您只能在包含上述任何聚合函数的查询中使用 group-by。 例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Pre-condition: Cache is already connected // Items are already present in the cache // Use the Fully Qualified Name (FQN) of your own custom class string query = "SELECT Category, COUNT(*) FROM FQN.Products WHERE UnitPrice > 5 Group By Category"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set int result = reader.GetValue(1); string category = reader.GetValue(0); } } |
原因 NCache 是 .NET 应用程序的好选择
让我们回到我们的超市问题。 随着实施 NCache 为了存储产品目录并在其上实现 SQL 查询,现在应用程序必须对数据库进行查询和检索数据的昂贵访问次数显着减少。
以下是其他一些好处 NCache 随之而来的是:
- 没有额外的代码: 由于大多数查询都是在缓存中进行的,因此每次都无需编写额外的代码来访问数据库。
- 100% 原生 .NET 和 .NET core: NCache 是 100% 原生的 .NET 和 .NET core 与任何其他分布式缓存和数据存储不同,分布式缓存允许您以可靠的形式存储管理和查询数据。
- 降低带宽成本: 对缓存的数据进行查询并返回给应用程序,节省了大量的时间和带宽成本。
- 极其快速和可靠: NCache 是一个非常快速且可扩展的分布式缓存,专为您的 .NET 和 .NET core 应用程序。 查看 NCache 基准 每秒两百万次交易。
所以,如您所见,这是一个完全双赢的局面!
前往 NCache 了解更多关于如何在分布式缓存数据存储中使用 SQL 的文档。