简体   繁体   English

SQL查询每天开始缓慢

[英]SQL Query slow beginning of each day

I have a basic query below. 我在下面有一个基本查询。

Select [ID] 
FROM MyTable 
Where [TextBlobField] like '%test%'

This query takes a long time when I first run it each day. 我每天第一次运行此查询时,需要花费很长时间。 After that it only takes about 5 seconds. 之后,仅需5秒钟。 Next morning back to long query time. 第二天早上又回到了漫长的查询时间。 Is this because SQL is caching a index temporary? 这是因为SQL缓存索引是临时的吗?

How can I make this fast all the time? 我如何一直保持这种速度? NOTE: I do not have permission to create my own index on the table. 注意:我没有权限在表上创建自己的索引。 I can create a Stored Procedure, but don't know if that help? 我可以创建一个存储过程,但是不知道有什么帮助吗? Should I create a view? 我应该创建一个视图吗?

it is highly unlikely that the reason is an index cache. 原因极不可能是索引缓存。 The reason: your query cannot make use of an index for the where clause. 原因:查询无法为where子句使用索引。 Although like can use an index, it cannot when the pattern starts with a wildcard. 尽管like可以使用索引,但是当模式以通配符开头时它不能。

However, you are on the right track. 但是,您的方向正确。 The reason is probably because the table itself needs to be read and cached in memory. 原因可能是因为表本身需要读取并缓存在内存中。

Although like doesn't use an index, you might be able to modify the query to use contains , taking advantage of full text indexing. 尽管like不使用索引,但您可以利用全文索引将查询修改为使用contains You can learn more about this here . 您可以在此处了解更多信息。

That's usually because of caching. 这通常是因为缓存。

One solution is to call that query once every morning automatically before your system is used by end users - that triggers the caching. 一种解决方案是每天早上自动在最终用户使用您的系统之前调用一次查询,这会触发缓存。

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

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