Microsoft SQL Server 2008 R2
I have a table that currently can not be queried on but seems to not have a lock against it.
Doesn't return:
SELECT * FROM myTable
Does return:
SELECT * FROM myTable with (nolock)
Inserts into the table also fail.
When I run sp_lock, I don't find any instances of locks on myTable. When I run the "Resource Locking Statistics by Object" report, I don't see any locks for myTable.
What other possibilities could there be that would keep a table from being acted upon?
Thanks.
Run sp_who2 and look at all the spids. See if you still have a connection to the database that is still thinking your performing an insert/update/delete to the table. If you find it, kill the spid and you should be able to query the table.
Related
I have Powershell script which runs the below SQL query. This script is scheduled to run every 5 minutes. Could you please tell me whether querying database every 5 minutes will cause database deadlock. Could someone please throw light on how deadlock works in SQL.
SELECT TOP 1000 * FROM TABLE1
I am not concerned about the data which I get from select query. I am pulling this data to check the response time. Does the parameter WITH(NOLOCK) prevent deadlock?
This script alone will never deadlock your table because it is just a SELECT.
Check your SQL Activity Monitor in SSMS to if any other processes are updating the table, because a deadlock happens when you select and update the record at the same time.
I would like to understand how postgreSQL is doing multi query execution for example i have a DB that there are lots of insert queries running for example 20-40 per minute and lots of select queries like 200-300 per minute (simple query by primary key selection).
This type of queries are run on the same table and i'm curious on how postgreSQL is handling these. Is it like when insert query is run table is lock and we have to wait for select queries or it is row locking so that while insert query is in progress select queries can continue and ignore locked rows?
In mysql database there is MyISAM engine that does table lock and innoDB that does row locking i guess...
Postgres implements multiv version concurrency control (MVCC) which means that readers never block writers and writers never block readers.
For normal DML statements Postgres never takes a table lock either so the SELECT queries are never blocked by any of the INSERT statements you are running concurrently.
The Postgres Wiki contains links to more detailed descriptions on how exactly MVCC is implemented and works in Postgres.
Essentially every modern DBMS uses some kind of MVCC these days. Oracle, Firebird and DB2 have "always" been using it. SQL Server introduced it with SQL Server 2005 (although it's still not the default behaviour) and in MySQL the InnoDB engine uses it.
Is it possible to somehow force Microsoft SQL Server to slow down certain or all queries so that we can test the client application on how it acts when the SQL queries run for extended time? For example, by seriously limiting its I/O or CPU to 1%.
Unfortunately it is not possible to modify the data in the database to add more rows.
I tried Forcing a query timeout in SQL Server but unfortunately the client application issues SELECT WITH (NOLOCK) so it ignores any locks we create.
To force query timeout error for SELECT ... FROM MySchema.MyTable WITH (NOLOCK) queries:
SELECT ... FROM MySchema.MyTable WITH (NOLOCK) still requires Sch-S lock. According to Lock Compatibility (Database Engine), Sch-S lock has a conflict with Sch-M lock.
1) So, create a new query in SSMS and execute the next script:
BEGIN TRAN
ALTER TABLE MySchema.MyTable
ADD DummyCol INT NULL DEFAULT 0 -- It requires Sch-M lock
--ROLLBACK
2) Execute the queries (from client app. or from another SSMS query) with NOLOCK table hint:
SELECT *
FROM MySchema.MyTable d WITH(NOLOCK) -- It requires Sch-S lock
3) Now, you have to wait.
Use WAITFOR DELAY to, well, make the code wait if you use stored procedures.
Controlling CPU requires Resource Governer, but I've not used it. IIRC, it requires Enterprise edition
Got an issue where i have a complicated sql query that occasionally hangs and doesn't execute on MS SQL. However, when i run update statistics on the tables involved in the query, the query executes normally.
Any idea or pointers on the cause?
Thanks!
SQL Server creates an "execution plan" that uses the statistics info to determine an optimal order to filter the data/reduce access to the database tables.
This execution plan is stored in the database cache and is re-used as long as the database is online; the statistics are not rebuild and the query is not modified.
When you update the indexes, the statistics are updated as well.
As a result, the stored execution plan for your query is no longer optimal and as a result will not be used any more.
I expect SQL Server also closes unused locks and transactions for the table before rebuilding the index. That is an undocumented feature.
I have SQL Server 2008. I run a query in a table on a database. The weirdest thing keeps happening. I run a simple select statement on the table. I know there are 62 rows in the table but it gets stuck at row 48 and goes on "querying...". Waited already for hours and it didn't move on from there. I only know of two programs, and one reporting service connecting to that particular table and one other user. Does anyone have any idea on what could be causing this and how I could trace the source of the lock on that table?
As a side note, I noted that the logs only had a notice that Autogrow failed the day before I checked. Could this have something to do with it?
What if you do a
SELECT * FROM YourTable WITH(NOLOCK)
Does it still hang?
Additionally when it does appear to be blocked you can try running
exec sp_who2
And looking in the BlkBy column to see what process is blocking you.
If that doesn't shed any light this article gives some info on some DMVs that might help get some insight into reasons for waits.