简体   繁体   English

SQL 服务器——如何保证查询结果被缓存?

[英]SQL Server - how to ensure query results are cached?

I have an application whose first page after login presents the user with a lot of data.我有一个应用程序,其登录后的第一页向用户显示大量数据。 The query against the database takes a long time - let's say 10 seconds.针对数据库的查询需要很长时间——比如说 10 秒。 The database is in SQL Server 2008, the query run is constructed as a prepared statement in Java.数据库位于 SQL Server 2008 中,查询运行构造为 Java 中的准备好的语句。 The data that needs to be displayed though will generally only change once per day.但是,需要显示的数据通常每天只会更改一次。 So - if I could have this result cached, and display a lot quicker that would be ideal.所以 - 如果我可以缓存这个结果,并且显示得更快,那将是理想的。

However, I'm not really sure what I need to do to bring down this 10 second wait - every time I load the page the same delay happens.但是,我不确定我需要做些什么来缩短这 10 秒的等待时间——每次加载页面时都会发生相同的延迟。 Is there something I need to do to make sure SQL Server caches the results to the query?我需要做些什么来确保 SQL 服务器将结果缓存到查询中吗? It certainly doesn't seem to be any quicker on a second load at the minute - where should I start exploring?现在第二次加载似乎并没有更快 - 我应该从哪里开始探索?

All help is much appreciated!非常感谢所有帮助!

You should look into indexed views.您应该查看索引视图。

They let you store in an index the results of a query (in the form of the view) that the optimizer can access indirectly or directly (ie it "knows" it has the data and will pull it for queries besides those against that view).它们允许您在索引中存储优化器可以间接或直接访问的查询结果(以视图的形式)(即,它“知道”它拥有数据,并将拉取它以用于除针对该视图的查询之外的查询) .

There are a number of constraints as outlined in the link.链接中概述了许多限制。

They are the only really viable way (besides creating and managing results tables) to cache complicated query results like aggregates.它们是缓存复杂查询结果(如聚合)的唯一真正可行的方法(除了创建和管理结果表)。

I'm guessing your query is complicated and/or contains aggregated fields and grouping, which will ALWAYS entail processing overhead even if the data pages are already cached in memory.我猜您的查询很复杂和/或包含聚合字段和分组,即使数据页已经缓存在 memory 中,这也总是需要处理开销。

If the data is only updated once a day, consider writing the results to a regular table and updating the table periodically (assuming you have the access necessary for that).如果数据每天只更新一次,请考虑将结果写入常规表并定期更新表(假设您有必要的访问权限)。

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

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