简体   繁体   English

从 Azure Analytics 日志错误消息中查找 SQL 查询

[英]Find SQL query from Azure Analytics Log error message

We have Azure SQL Analytics enabled against a Managed Instance Database.我们针对托管实例数据库启用了 Azure SQL Analytics。

The "Errors/Blocking/Timeouts/Deadlocks" chart presents a view of Counts by Time, and then a listing of issues underneath eg “错误/阻塞/超时/死锁”图表显示了按时间计数的视图,然后是下面的问题列表,例如

按时间排列的错误计数图表

If you click on an entry, your are presented with a detail view eg如果你点击一个条目,你会看到一个详细的视图,例如

错误详情

The message provides no other context eg the table being queried.该消息不提供其他上下文,例如正在查询的表。 I'm unable to trace this error back to the actual query which raised this error.我无法将此错误追溯到引发此错误的实际查询。 There's no query hash given to allow it to be found in the query plan cache.没有给出查询哈希以允许在查询计划缓存中找到它。 Since it is such a generic error, it's of very little value when trying to find the errant code.由于这是一个通用错误,因此在尝试查找错误代码时它的价值很小。

Does anyone have the Kusto-fu to provide a query that I can run against the analytics logs to get more context, or a T-SQL query to find queries run at a given date and time?有没有人有 Kusto-fu 来提供我可以针对分析日志运行的查询以获取更多上下文,或者使用 T-SQL 查询来查找在给定日期和时间运行的查询?

EDIT : I've found the raw log entry, but all query hash values are -1编辑:我找到了原始日志条目,但所有查询哈希值都是 -1

在此处输入图片说明

There is a few ways that you can possibly track this down.有几种方法可以跟踪此情况。 First i would start with this query running in the master database for the server:首先,我将从在服务器的 master 数据库中运行的这个查询开始:

SELECT * FROM sys.event_log WHERE severity=2

This will show you errors, and hopefully yours shows up there.这将向您显示错误,并希望您的错误显示在那里。 After getting an idea of what database is having this error, assuming you didnt already know, you can then go to that database's query store and search for statements run at the same time as your error.在了解出现此错误的数据库后,假设您还不知道,您可以转到该数据库的查询存储并搜索与您的错误同时运行的语句。 Query store can be found in the object explorer in SSMS but that only gives you the out of the box options.查询存储可以在 SSMS 的对象资源管理器中找到,但这只能为您提供开箱即用的选项。 Here is a query you can play around with to assist in your rearch - note you will need to change the where clause in the bottom portion.这是一个您可以使用的查询来帮助您进行搜索 - 请注意,您需要更改底部的 where 子句。

IF OBJECT_ID('tempdb..#compiledValue') IS NOT NULL
    DROP TABLE #compiledValue;

SELECT q.object_id
    ,pp.name
    ,p.query_plan AS QryPlan
    ,CAST(p.query_plan AS XML) query_plan
    ,q.last_execution_time
    ,t.query_sql_text
    ,TRY_CONVERT(XML, SUBSTRING(p.query_plan, CHARINDEX('<ParameterList>', p.query_plan), CHARINDEX('</ParameterList>', p.query_plan) + LEN('</ParameterList>') - CHARINDEX('<ParameterList>', p.query_plan))) AS Parameters
INTO #compiledvalue
FROM sys.query_store_plan AS p
INNER JOIN sys.query_store_query AS q ON p.query_id = q.query_id
INNER JOIN sys.query_store_query_text AS t ON q.last_compile_batch_sql_handle = t.statement_sql_handle
INNER JOIN sys.procedures pp ON q.object_id = pp.object_id;

SELECT object_id
    ,name
    ,query_plan
    ,last_execution_time
    ,query_sql_text
    ,pc.compiled.value('@Column', 'nvarchar(128)') AS Parameterlist
    ,pc.compiled.value('@ParameterCompiledValue', 'nvarchar(128)') AS [compiled Value]
FROM #compiledValue cvalue
OUTER APPLY cvalue.parameters.nodes('//ParameterList/ColumnReference') AS pc(compiled)
WHERE pc.compiled.value('@Column', 'nvarchar(128)') IS NOT NULL
    AND pc.compiled.value('@ParameterCompiledValue', 'nvarchar(128)') <> 'NULL'
    --QryPlan LIKE '%Table Scan%'

This isnt meant to be a full answer, but too long to be a comment.这并不意味着是一个完整的答案,但作为评论太长了。

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

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