gpt4 book ai didi

sql-server - 为什么我的 Sql 查询在第二次运行时更快?

转载 作者:行者123 更新时间:2023-12-05 03:04:25 24 4
gpt4 key购买 nike

每次我在执行 SQL 查询时获得相同的执行时间。如果 SQL 查询多次运行,是否有机会始终获得相同的执行时间?

最佳答案

当您第一次运行查询并且数据不在缓存中时,服务器会从磁盘读取数据。这很耗时。第二次执行相同的查询时,数据已在缓存中,因此需要的时间更少。

is there any chance to get same execution time for all the time if SQL query runs multiple times ?

如果你想用冷缓存测试你的查询(每次都没有缓存数据)你可以使用DBCC DROPCLEANBUFFERS在执行查询之前:

Use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache without shutting down and restarting the server. To drop clean buffers from the buffer pool and columnstore objects from the columnstore object pool, first use CHECKPOINT to produce a cold buffer cache. This forces all dirty pages for the current database to be written to disk and cleans the buffers. After you do this, you can issue DBCC DROPCLEANBUFFERS command to remove all buffers from the buffer pool.

当然这不是要在生产环境中使用。

如果相反,您希望数据始终在缓存中,您应该增加服务器的 RAM,并且尽可能不要重新启动它。

关于sql-server - 为什么我的 Sql 查询在第二次运行时更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53040095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com