I want to get the query_id for the running query in Snowflake programatically. It seems like account_usage.query_history doesn't have running query ids stored. Is there are any other way to get the query_id of running query?
If I'm not mistaken, you will need two sessions on your client application to get the query ID. You would only want to do this for long-running queries (for example to allow checking on status or aborting it after so many seconds), because you can get the query ID of a short query with last_query_id().
-- In first session, get the session ID before executing the long-running query.
select current_session() as SESSION_ID;
-- In second session, get the query ID of the running query in the first session
select QUERY_ID
from table(information_schema.query_history())
where execution_status ='RUNNING'
and session_id = 172747880058954;
You can use information schema.
select *
from table(information_schema.query_history()) where execution_status ='RUNNING';
You can do either which return the ID for the most recently executed query
select last_query_id();
Or select last_query_id(1);
which Return the ID for the first query executed in the session
Read more here
There are other options provided but if you want to check for queries running for specific time (long running) you can use this query.
SET long_running_threshold_seconds = 10;
SELECT * FROM TABLE(information_schema.query_history()) WHERE (execution_status = 'RUNNING'
AND datediff('seconds',start_time,current_timestamp()) > $long_running_threshold_seconds )
Related
Have abcd database. one user has executed query on abcd database. Another user has executed another query on abcd database. Like this whenever different user has executed different user has executed query on abcd database. Need to capture user execution time and who has executed query..etc
This information is gathered from https://docs.snowflake.com/en/user-guide/access-history.html which is basically the access history details. Also, the same detail can be checked from Snowflake - Query History - Filters where you can use username or any other parameter to look up the details.
You can use Query_history view available in Snowflake.ACCOUNT_USAGE schema to get the complete information of the query within the last 365 days including user_name, execution time, etc
Please note Latency for the view may be up to 45 minutes.
https://docs.snowflake.com/en/sql-reference/account-usage/query_history.html#query-history-view
Also, you can use QUERY_HISTORY function available in information_schema to retrieve query information within the last 7 days and with no latency.
Please review the below documentation for more information.
https://docs.snowflake.com/en/sql-reference/functions/query_history.html#query-history-query-history-by
https://docs.snowflake.com/en/sql-reference/account-usage.html#differences-between-account-usage-and-information-schema
You can use below query(you can add the columns as you need):
select distinct QH.query_id, QH.USER_NAME,qh.database_name,Qh.start_time, qh.EXECUTION_TIME from
"SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY" QH
where
-- QH.query_id='' --If you know the query id,use it here
-- QH.user_name='USERNAME' -- You can filter by user id
QH.database_name='DBNAME' --you can filter by databasename
and qh.start_time > '2022-06-29 12:45:36.291'-- you can filter by date
;
If you want to track the IP address and application from where query was run, you can use below query as well:
select distinct QH.query_id,LH.client_ip, QH.USER_NAME,s.client_application_id,qh.database_name,Qh.start_time, qh.EXECUTION_TIME from snowflake.account_usage.login_history LH
inner join "SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY" QH
on QH.USER_NAME=LH.user_name
inner join "SNOWFLAKE"."ACCOUNT_USAGE"."SESSIONS" S on S.session_id=QH.session_id
and s.LOGIN_EVENT_ID=lh.EVENT_ID
where
-- QH.query_id='' --If you know the query id,use it here
-- QH.user_name='USERNAME' --If you know the user id,use it here
QH.database_name='DBNAME' --If you know the DB id,use it here
and qh.start_time > '2022-06-29 12:45:36.291'-- filter by date as required
;
I am trying to find the original code behind the creation of a table in Snowflake.
I am using this query:
SELECT GET_DDL('table', 'table1');
This is only giving me the original DDL behind the table. I would need the full code (as in the original SQL SELECT statement).
Anyone know what query could get me that?
You can query QUERY_HISTORY and get the SQL statement (and other data) using the following:
// Be sure to use a role with permission to perform the following
SELECT
*
FROM
SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE
QUERY_TEXT ILIKE '%create%table%table1%'
ORDER BY END_TIME DESC
LIMIT 20;
I found the following queries on the internet. The first query is for receiving 100 queries executed on the server,
SELECT TOP 100
deqs.last_execution_time AS [Time],
dest.text AS [Query] , *
FROM
sys.dm_exec_query_stats AS deqs
CROSS apply
sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY
deqs.last_execution_time DESC
and the second query displays the users registered in SQL Server.
SELECT * FROM sys.sql_logins
My question is how can I connect these two queries in such a way that it shows the users who have run the queries in the list. Thanks ...
As Charlieface says in the comments, you should set up Auditing to track who is executing what query & when. You can find information about SQL Server Auditing, including how to set it up, on Microsoft Docs: https://learn.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-database-engine
Once you have auditing set up as you want it, you can then execute the following to find the details of the most recent 100 queries:
SELECT TOP 100 *
FROM sys.fn_get_audit_file ('\\serverName\foldername\filename.sqlaudit',default,default)
ORDER BY event_time DESC;
Is it possible to do the same thing using the SnowSql command line interface (CLI)? I'd like to view the SQL code for a particular query, as specified by its query ID, using the CLI.
When using the web console, one may go on the History tab and filter by "Query ID" e.g. "xxx-xxxxx" to view the SQL code and error messages (if any) for that particular query.
You can use LAST_QUERY_ID to retrieve the query IDs for queries in your session.
select last_query_id(); Gets the most Recent Query ID
select last_query_id(1); Gets the first Query ID of the session
select last_query_id(-2); Gets the Query ID from two queries ago.
etc.
Then you can use a query like this to get your actual Query Text if you need it.
SELECT QUERY_TEXT
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY_BY_SESSION(RESULT_LIMIT => 10))
WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346'
;
If you need to retrieve query information outside of your Session, I believe you can use ACCOUNT_USAGE if that works for you.
SELECT * FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY WHERE QUERY_ID = '018cde0c-0077-09ee-0000-001812d26346';
Please try this using QUERY_HISTORY* Tables:
SELECT query_id, query_text,user_name,session_id,role_name,warehouse_name,
execution_status,error_code,error_message,start_time,end_time
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(DATEADD('HOURS',-1,CURRENT_TIMESTAMP()),CURRENT_TIMESTAMP()))
ORDER BY start_time;
Reference : https://docs.snowflake.net/manuals/sql-reference/functions/query_history.html
I need to diagnose some issues in production but I cannot query the event_log, query times out.
I was trying to executing the following query on Master database in my Azure database,
select * from sys.event_log where start_time>='2016-02-20:12:00:00' and end_time<='2016-02-20 12:00:00'
Query starts executing, and runs over more than 8 mins and Cancels query execution. I am pretty sure that the eventlog must be a very large one in this database server. How to overcome this situation and query the sys.event_log table?
Even the top 10 query times out. Need some help!
Query I ran was, this might also get a time out, just keep trying (worked for me in the 3rd time)
SELECT *
,CAST(event_data AS XML).value('(/event/#timestamp)[1]', 'datetime2') AS TIMESTAMP
,CAST(event_data AS XML).value('(/event/data[#name="error"]/value)[1]', 'INT') AS error
,CAST(event_data AS XML).value('(/event/data[#name="state"]/value)[1]', 'INT') AS STATE
,CAST(event_data AS XML).value('(/event/data[#name="is_success"]/value)[1]', 'bit') AS is_success
,CAST(event_data AS XML).value('(/event/data[#name="database_name"]/value)[1]', 'sysname') AS database_name
FROM sys.fn_xe_telemetry_blob_target_read_file('el', NULL, NULL, NULL)
WHERE object_name = 'database_xml_deadlock_report'
This gives very useful details in the xml data field.
Use an XML viewer to view details. I used XMLGrid.
It will show what are the two processes (deadlock victim and winner) and the good news is that it gives you the SQL statements those processes were trying to execute.
In my case two processes were trying to update one data table, but two different rows. Winner process was using a SQL "Merge" which creates a table lock for the row update. Solution was I changed that Merge query to use SQL UPDATE.