How can I tell if a Azure SQL Database has QUERY_STORE turned on?
You enable it with this command:
ALTER DATABASE <database_name> SET QUERY_STORE = ON;
I figure it should be simple to check the database for this, but I have not found the trick.
FYI, I tried this command on a database that had it enabled, but the command just returned null:
SELECT DATABASEPROPERTYEX ('<database_name>', 'QUERY_STORE')
This DMV sys.database_query_store_options should allow you to determine if QUERY_STORE is enabled:
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 ;
Description of Actual_state_Desc states :
OFF (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
ERROR (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's answer shows if the Query store is enabled for the current database - so you would need to loop through them.
This query shows if the Query store is enabled for all databases (but doesn't show any details).
SELECT
d.name,
d.is_query_store_on
FROM sys.databases AS d
Related
Is there a way to monitor specific SQL queries in SQL Server?
For example I would like to get notified somehow when somebody run a specific query against the database. For eg.:
Select *
from table
where id = 1
Thank You!
There are a couple of ways you can do this. I'd probably start with an audit. Create a server audit.
USE [master]
GO
USE [master]
GO
CREATE SERVER AUDIT [audit_stackoverflow_question]
TO APPLICATION_LOG
WITH (QUEUE_DELAY = 1000, ON_FAILURE = FAIL_OPERATION)
ALTER SERVER AUDIT [stack] WITH (STATE = OFF)
GO
Create a database audit specification.
USE [<your_database>]
GO
CREATE DATABASE AUDIT SPECIFICATION [audit_monitor_dbo.table]
FOR SERVER AUDIT [audit_stackoverflow_question]
ADD (SELECT ON OBJECT::[dbo].[table] BY [public])
WITH (STATE = OFF)
GO
Enable the audit and database audit specification when you're ready. After that point, activity will be logged (in this case to the application event log):
You can push activity to a file. Whatever floats your boat. And, after that, you can Powershell out whatever specific activity you want to see.
You can use extended events or traces for this too, but I think an audit is the way to go.
If you're looking after the fact, check the cache tables:
SELECT t.[text]
, *
FROM sys.dm_exec_cached_plans AS p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'Select%from%table%where%id%'
OPTION (recompile);
While creating the database by using the create database statement in informix, I couldn't enable is_nls parameter in sysdatabases table in sysmaster database.
How to enable it?
The is_nls column on the sysdatabases view over the sysmaster is a flag that tells whether GLS is enabled (1) or not (0).
You should not try to change it over this view nor the sysdbspartn table.
If what you're trying to do is change the code set used for a database that it's not possible.
To specify a code set on the creation you must set environment variable DB_LOCALE for the one you want.
You can check the locale in which the database was created by querying the sysmaster view sysdbslocale.
I'm trying to rename a table using the following syntax
sp_rename [oldname],[newname]
but any time I run this, I get the following [using Aqua Datastudio]:
Command was executed successfully
Warnings: --->
W (1): The SQL Server is terminating this process.
<---
[Executed: 16/08/10 11:11:10 AM] [Execution: 359ms]
Then the connection is dropped (can't do anything else in the current query analyser (unique spid for each window))
Do I need to be using master when I run these commands, or am I doing something else wrong?
You shouldn't be getting the behaviour you're seeing.
It should either raise an error (e.g. If you don't have permission) or work successfully.
I suspect something is going wrong under the covers.
Have you checked the errorlog for the ASE server? Typically these sorts of problems (connections being forcibly closed) will be accompanied by an entry in the errorlog with a little bit more information.
The error log will be on the host that runs the ASE server, and will probably be in the same location that ASE is installed into. Something like
/opt/sybase/ASE-12_5/install/errorlog_MYSERVER
try to avoid using "sp_rename". Because some references in system tables remain like old name. Someday this may cause some faulties if you forget this change.
I suggest;
select * into table_backup from [tableRecent]
go
select * into [tableNew] from table_backup
go
drop table [tableRecent] -- in case of backup you may not drop that table
go
drop table table_backup -- in case of backup you may not drop that table
go
to achieve that; your database has an option "select into/bulkcopy/pllsort"
if your ata is huge, check your free space on that database.
and enjoy :)
I need to know how to interrogate a Microsoft SQL Server, to see if a given database has been set to Read-Only or not.
Is that possible, using T-SQL?
The information is stored in sys.databases.
SELECT name, is_read_only
FROM sys.databases
WHERE name = 'MyDBNAme'
GO
--returns 1 in is_read_only when database is set to read-only mode.
Querying sys.databases for checking a DB's Read-Only property will only give the right information if the database has been explicitly set to Read-Only mode.
For databases that are in the passive servers (e.g. in AlwaysOn technology Secondary Servers), even though the databases cannot be written into, their Read-Only mode in sys.databases would still be set as False(0).
Hence, it is advisable to check the Read-Only mode of databases using the statement:
SELECT DATABASEPROPERTYEX('MyDBNAme', 'Updateability');
I was trying to use the p.campbell's answer to check if my Azure SQL DB is the primary one or the read only replica - it didn't work. Both the primary DB and the replica returned had 0 on the is_read_only field.
Here's what worked for me:
SELECT DATABASEPROPERTYEX('MyDBNAme', 'Updateability');
the above select statement returns string 'READ_ONLY' or 'READ_WRITE'.
Here is a command to display or set this property.
EXEC sp_dboption "AdventureWorks", "read only"
Sample output
OptionName CurrentSetting
read only OFF
If DB is part of your Always On and the secondary node is designed in Read_Only then
"sys.databases --> Is_Read_Only" column wont show the correct result ! its a bug that Microsoft needs to address it during the next versions.
If you would like to check all DB statuses in your server, use this:
SELECT name, user_access_desc, is_read_only, state_desc, recovery_model_desc
FROM sys.databases;
You can quickly determine your next steps.
SQL Server 2000 here.
I'm trying to be an interim DBA, but don't know much about the mechanics of a database server, so I'm getting a little stuck. There's a client process that hits three views simultaneously. These three views query a remote server to pull back data.
What it looks like is that one of these queries will work, but the other two fail (client process says it times out, so I'm guessing a lock can do that). The querying process has a lock that sticks around until the SQL process is restarted (I got gutsy and tried to kill the spid once, but it wouldn't let go). Any queries to this database after the lock hang, and blame the first process for blocking it.
The process reports these locks... (apologies for the formatting, the preview functionality shows it as fully lined up).
spid dbid ObjId IndId Type Resource Mode Status
53 17 0 0 DB S GRANT
53 17 1445580188 0 TAB Sch-S GRANT
53 17 1445580188 0 TAB [COMPILE] X GRANT
I can't analyze that too well. Object 1445580188 is sp_bindefault, a system stored procedure in master. What's it hanging on to an exclusive lock for?
View code, to protect the proprietary...I only changed the names (they stayed consistent with aliases and whatnot) and tried to keep everything else exactly the same.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER view [dbo].[theView]
as
select
a.[column1] column_1
,b.[column2] column_2
,[column3]
,[column4]
,[column5]
,[column6]
,[column7]
,[column8]
,[column9]
,[column10]
,p.[column11]
,p.[column12]
FROM
[remoteServer].db1.dbo.[tableP] p
join [remoteServer].db2.dbo.tableA a on p.id2 = a.id
join [remoteServer].db2.dbo.tableB b on p.id = b.p_id
WHERE
isnumeric(b.code) = 1
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
Take a look at this link. Are you sure it's views that are blocking and not stored procedures? To find out, run this query below with the ObjId from your table above. There are things that you can do to mitigate stored procedure re-compiles. The biggest thing is to avoid naming your stored procedures with an "sp_" prefix, see this article on page 10. Also avoid using if/else branches in the code, use where clauses with case statements instead. I hope this helps.
[Edit]:
I believe sp_binddefault/rule is used in conjunction with user defined types (UDT). Does your view make reference to any UDT's?
SELECT * FROM sys.Objects where object_id = 1445580188
Object 1445580188 is sp_bindefault in the master database, no? Also, it shows resource = "TAB" = table.
USE master
SELECT OBJECT_NAME(1445580188), OBJECT_ID('sp_bindefault')
USE mydb
SELECT OBJECT_NAME(1445580188)
If the 2nd query returns NULL, then the object is a work table.
I'm guessing it's a work table being generated to deal with the results locally.
The JOIN will happen locally and all data must be pulled across.
Now, I can't shed light on the compile lock: the view should be compiled already. This is complicated by the remote server access and my experience of compile locks is all related to stored procs.