如何判断 SQL 数据库是否启用了 QUERY_STORE?

How to tell if a SQL Database has QUERY_STORE enabled?

如何判断 Azure SQL 数据库是否已启用 QUERY_STORE

您可以使用以下命令启用它:

ALTER DATABASE <database_name> SET QUERY_STORE = ON;

我想查一下数据库应该很简单,但我还没有找到窍门。

仅供参考,我在启用了该命令的数据库上尝试了此命令,但该命令仅返回空值:

SELECT DATABASEPROPERTYEX ('<database_name>', 'QUERY_STORE')

此 DMV sys.database_query_store_options 应该允许您确定 QUERY_STORE 是否已启用:

SELECT  desired_state_desc ,
        actual_state_desc ,
        readonly_reason, 
        current_storage_size_mb , 
        max_storage_size_mb ,
        max_plans_per_query 
FROM    sys.database_query_store_options ;

Actual_state_Desc 个州的描述:

关闭 (0)

-Not Enabled

READ_ONLY (1)

Query Store may operate in read-only mode even if read-write was specified by the user. For example, that might happen if the database is in read-only mode or if Query Store size exceeded the quota

READ_WRITE (2)

Query store is on and it is capturing all queries

错误 (3)

Extremely rarely, Query Store can end up in ERROR state because of internal errors. In case of memory corruption, Query Store can be recovered by requesting READ_WRITE mode explicitly, using the ALTER DATABASE SET QUERY_STORE statement. In case of corruption on the disk, data must be cleared before READ_WRITE mode is requested explicitly.

David 的回答显示了当前数据库是否启用了查询存储 - 因此您需要遍历它们。

此查询显示是否为所有数据库启用了查询存储(但不显示任何详细信息)。

SELECT 
    d.name,
    d.is_query_store_on
FROM sys.databases AS d