JDBC Timeout with Oracle DB - database

Is it possible to find out which query was executing/waiting in Oracle db when JDBC timeout was issued for a session?
I checked the sessions and I can see P2TEXT = 'interrupt' and P3TEXT = 'timeout' and wait_class = 'System I/O' but sql_id is blank and p2 is 0.
Thanks in advance.

Use the Active Session History (ASH) data to find out what was running and when.
ASH data is based on sampling. Don't expect to find the exact query at the exact millisecond. But if there is a performance problem it should stick out in a query like this:
select username, sample_time, sql_id
from gv$active_session_history
join dba_users
on gv$active_session_history.user_id = dba_users.user_id
where sample_time between timestamp '2017-05-20 09:00:00' and timestamp '2017-05-21 09:05:00'
order by sample_time desc;
That view generally only contains data for the past day, depending on how busy the system is. If you need to go back further in time you may be able to use DBA_HIST_ACTIVE_SESS_HISTORY instead.

Related

Microsoft SQL Server Agent Job: Get Schedule that triggered the Job

I have SQL Server Agent Job on my System that copies data into at table for later evaluation purposes. The Job runs on two types of schedules every Friday every week and last day of the month. The target data records should also contain a column indicating the schedule that originally triggered the job. But I found no way so far to receive this data as parameter or so. I'm using a Microsoft SQL Server 2017.
I did a web search but maybe searched for the wrong keywords. I also thought about comparing current time to expected runtime per schedule but that seemed to be not a fault tolerant option to me.
I like to fill a column "schedule" with values like "End of week", "End of month"
sys tables are your friend here. Documentation
sysjobs has your job information.
sysjobschedules links your job to its schedule.
sysschedules has your schedule info.
SELECT j.*
, s.*
FROM sysjobs j
JOIN sysjobschedules js ON j.id = js.job_id
JOIN sysschedules s ON js.schedule_id = s.schedule_id
WHERE j.name = 'your job name here'
After long search and analyzing I finally found a solution that at least fit my needs:
The undocumented and unsupport stored procedures provides the schedule that triggered a job ind Column Request Source ID:
EXEC master.dbo.xp_sqlagent_enum_jobs 1, garbage
see also: https://am2.co/2016/02/xp_sqlagent_enum_jobs_alt/

How to find out Account usage by user > particular date and how much time he used particular query

I want to query from snow flake db as part of monitoring process, How much time a user using snowflakedb to execute his queries after particular date. The purpose of this is, to prevent users to running long queries.
Account usage history is some thing I wanted to know. I'm very new to snowflakedb.
Is there any way to query from the metadata ?
You can use Query history view for this requirement
There are many columns in this view you can see and use appropriately as per your requirement.
Example Query :
SELECT query_id,
query_text,
query_type,
session_id,
user_name,
warehouse_name,
start_time,
end_time,
total_elapsed_time,
compilation_time,
execution_time
FROM snowflake.account_usage.query_history
WHERE user_name = 'anyusername'
AND Cast (start_time AS DATE) >= 'yourdate in yyyy-mm-dd format'
AND total_elapsed_time > 600000 -- around 10 minutes in milliseconds or you can specify any number here
AND warehouse_name = 'your datawarehouse name'
ORDER BY execution_time DESC;
There is also a parameter called STATEMENT_TIMEOUT_IN_SECONDS to control long running queries. Set to the amount of time, in seconds, after which a running SQL statement (query, DDL, DML, etc.) is canceled by the system. Can be set for Account » User » Session; can also be set for individual warehouses. The default setting is 172800 (2 days).

Snowflake query_history gets reset after warehouse suspension

I am using the following query to retrieve query history from my Snowflake database.
SELECT *
FROM table(MY_DATABASE.information_schema.query_history(
end_time_range_start => dateadd(HOUR, -4, current_timestamp()),
current_timestamp()
));
Oddly, if the warehouse (size: XS) I am using gets suspended after a period of inactivity, the next time I attempt to retrieve query history- the history that was there prior to the warehouse's suspension is gone.
I could not find anything documented to explain this.
Anyone run into this issue or related documentation that could explain this?
Thank you!
I can't explain exactly the limitations of that information schema query you are running (some of them only return like 10,000 rows or like you said, once the warehouse turns off), but it's a limited view into the actual query history. You can use the snowflake database for all query history.
It's a massive table so make sure you put filters on it. Here's an example query to access it:
USE DATABASE snowflake;
USE SCHEMA account_usage;
SELECT *
FROM query_history
WHERE start_time BETWEEN '2020-01-01 00:00' AND '2020-01-03 00:00'
AND DATABASE_NAME = 'DATABASE_NAME'
AND USER_NAME = 'USERNAME'
ORDER BY START_TIME DESC;
1: Your question states that after a period of inactivity, does not specify what is the period of inactivity.
"after a period of inactivity, the next time I attempt to retrieve query history- the history that was there prior to the warehouse's suspension is gone."
If its beyond 7 days then the data can be found from account_usage table. Below is the link of difference between INFORMATION_SCHEMA and ACCOUNT_USAGE.
https://docs.snowflake.com/en/sql-reference/account-usage.html#differences-between-account-usage-and-information-schema
2: Your query does not specify USER_NAME or WAHREHOUSE_NAME in your query so it could be that before the output of your queries before suspension of warehouse may have moved beyond 4 hours period as in your predicate. If you can increase the time period and check if behaviour still exists.
3: In general its not advisable to query INFORMATION_SCHEMA to get query history unless your application requires data without any latency. If possible use ACCOUNT_USAGE table to get query history information.
Here is what I did.
1: Created an XS warehouse
2: Set auto_suspend to 5 minutes
3: Ran few queries
4: Ran your query (which does not specify user_name or warehouse_name) meaning you are searching for history from all users.
SELECT *
FROM table(MY_DATABASE.information_schema.query_history(
end_time_range_start => dateadd(HOUR, -4, current_timestamp()),
current_timestamp()
));
5: Returned output of few 100 records.
6: Used additional where clause to check for data of my user which ran few queries before auto_suspend of Warehouse and it returned few records.
SELECT *
FROM table(MY_DATABASE.information_schema.query_history(
end_time_range_start => dateadd(HOUR, -4, current_timestamp()),
current_timestamp()
))
WHERE USER_NAME = 'ADITYA';
7: Waited for 10 minutes so that my warehouse is auto_suspended.
8: Repeat point 5 and point 6 and again it returned records as expected.

How can I find out if someone modified a row in SQL server in a specific date?

I am just wondering, can I find out if somebody wrote a query and updated a row against specific table in some date?
I tried this :
SELECT id, name
FROM sys.sysobjects
WHERE NAME = ''
SELECT TOP 1 *
FROM ::fn_dblog(NULL,NULL)
WHERE [Lock Information] LIKE '%TheOutoput%'
It does not show me ?
Any suggestions.
No, row level history/change stamps is not built into SQL Server. You need to add that in the table design. If you want an automatic update date column it would typically be set by a trigger on the table.
There is however a way if you really need to find out what happened in a forensics scenario. But that is only available if you have the right backup plans. What you can do then is to use the DB transaction log to find when the modification was done. Note that this is not anything an application can or should do runtime.

Detect the cause of SQL Server update lock

Problem:
A .NET application during business transaction executes a query like
UPDATE Order
SET Description = 'some new description`
WHERE OrderId = #p1 AND RowVersion = #p2
This query hangs until timeout (several minutes) and then I get an exception:
SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
It is reproduced, when database is under heavy load (several times per day).
I need to detect the cause of the lock of the query.
What I've tried:
Exploring activity monitor - it shows that the query is hanging by lock. Filtering by headblocker does not give much, it is frequently changing.
Analyze SQL script, that gives similar to activity monitor data - almost same result as looking to activity monitor. Chasing blocking_session_id results in some session, that awaits for command or executing some SQL, I can't reason a relation to Order table. Executing the same script in a second gives other session. I also tried a some other queries/stored procedures from this atritcle with no result.
Building standard SQL Server report for locked/problem transactions results in errors like Max recursion exhausted or Local OutOfMemory Exception (I have 16 Gb RAM).
Database Details
Version: SQL Server 2016
Approximate number of concurrent queries per second by apps to database: 400
Database size: 1.5 Tb
Transaction isolation level: ReadUncommited for readonly transactions, Serializable for transactions with modifications
I'm absolutely new to this kind of problems, so I have missed a lot for sure.
Any help or direction would be great!
In case anyone interested, I have found this particular query espesially usefull:
SELECT tl.resource_type
,OBJECT_NAME(p.object_id) AS object_name
,tl.request_status
,tl.request_mode
,tl.request_session_id
,tl.resource_description
,(select text from sys.dm_exec_sql_text(r.sql_handle))
FROM sys.dm_tran_locks tl
INNER JOIN sys.dm_exec_requests r ON tl.request_session_id=r.session_id
LEFT JOIN sys.partitions p ON p.hobt_id = tl.resource_associated_entity_id
WHERE tl.resource_database_id = DB_ID()
AND OBJECT_NAME(p.object_id) = '<YourTableName>'
ORDER BY tl.request_session_id
It shows transactions, that have acquired locks on <YourTableName> and what query they are executing now.
Try to use sys.dm_exec_requests view, and filter by columns blocking_session_id, wait_time

Resources