Tag Info

Hot answers tagged

19

For relatively small values of n (20 in this example), you can use a method that exploits the fact that the natural integers are combinations of bits. T-SQL Solution Sample data: DECLARE @Sample AS TABLE ( item_id tinyint IDENTITY(1,1) PRIMARY KEY NONCLUSTERED, item nvarchar(500) NOT NULL, bit_value AS CONVERT ...


19

Is a bad practice to create a transaction always? It depends on what context you are talking here. If it is an update, then I would highly recommend using TRANSACTIONS explicitly. If it is a SELECT then NO (explicitly). But wait there is more to understand first : Everything in sql server is contained in a transaction. When the session option ...


18

Summary If you have locking problems then you have a problem with your code: it isn't the database engine It isn't a magic bullet You may add more problems Load It will also increase load on your tempdb and CPU. Also see: "Performance Impact: The Potential Cost of Read_Committed_Snapshot" (Linchi Shea) Safety Most important, snapshot isolations ...


17

I've not tried fn_dblog on Express but if it is available the following will give you delete operations: SELECT * FROM fn_dblog(NULL, NULL) WHERE Operation = 'LOP_DELETE_ROWS' Take the transaction ID for transactions you're interested in and identify the SID that initiated the transaction with: SELECT [Transaction SID] FROM ...


14

Couple of additional points to add to the other answers: SET ALLOW_SNAPSHOT_ISOLATION ON only enables snapshot isolation in a database. To take advantage of it you have to recode and SET TRANSACTION ISOLATION LEVEL SNAPSHOT for the transactions you want it to apply to. The calling code will need to be changed to handle update conflict errors. After SET ...


14

An insert is always within a transaction. If you don't have an explicit BEGIN TRAN ... COMMIT or SET IMPLICIT_TRANSACTIONS ON then the statement runs as a self contained auto commit transaction. The trigger is always part of the transaction for the action that fires the trigger. If an error occurs in the trigger that causes transaction rollback then the ...


13

It will truncate automatically but that is very different to shrink. Truncation reclaims log space for re-use, shrinking physically reduces the file size to release space back to the OS. If your log has grown to its current size its likely that it will grow again if you shrink it. I'd suggest getting a handle on what typical and maximum log usage is for ...


13

There are a few different approaches depending on the details of your batch process and why you're trying to view the uncommitted changes. 1) Oracle Workspace Manager is a tool that was originally designed to allow people developing Spatial applications to have the equivalent of extremely long-running transactions (i.e. transactions that may require ...


13

I believe this will give us something closer to oracle where if one transaction is updating other transactions can still read the old data. Is this correct? Yes, this is correct. Well worth reading the links in gbn's answer and I believe the same applies to Oracle's default MVCC as to SQL Server in Snapshot Isolation mode. I would add that if you ...


13

For SQL Server, you could argue that a commit operation is nothing more than writing LOP_COMMIT_XACT to the log file and releasing locks, which is of course going to be faster than the ROLLBACK of every action your transaction performed since BEGIN TRAN. If you are considering every action of a transaction, not just the commit, I'd still argue your ...


13

A SQL statement always runs in a transaction. If you don't start one explicitly, every SQL statement will run in a transaction of itself. The only choice is whether you bundle multiple statements in one transaction. Transactions that span multiple statements leave locks that hurt concurrency. So "always" creating a transactions is not a good idea. You ...


11

For Oracle, rollback can take many times longer than the time it took to make the changes that are rolling back. This often does not matter because No locks are held while the transaction is rolling back It is handled by a low priority background process For SQL Server I'm not sure if the situation is the same but someone else will say if it isn't... As ...


10

A connection from the pool will have the isolation level set by the last client to use that connection. Yes, it really is that scary. The long and the short of it is that if you change the isolation level of a connection you must explicitly set it back to READ COMMITTED before closing. Better is to explicitly declare your required isolation level at the ...


10

when I explicitly lock a row or a table, am I employing the exact same constructs that are used by the database's transaction facilities under the covers to make the transaction work properly? Yes. If that would not be true, then your own 'locking' would only be scoped to other similar 'locking' and not interact with the engine own locking. So you would ...


9

Extending Mark's answer... When a client timeout event occurs (.net CommandTimeout for example), the client sends an "ABORT" to SQL Server. SQL Server then simply abandons the query processing. No transaction is rolled back, no locks are released. Now, the connection is returned to the connection pool, so it isn't closed on SQL Server. If this ever happens ...


9

I'm only familiar with SQL Server: Each operation is atomic. If you run a delete, and it cascades to other tables, those records are gone, too, as soon as the statement is over. They don't magically come back into existence unless the transaction is rolled back. If you're relying on the ID values and don't want to cascade the related tables, consider ...


8

Yes. Transactions apply to DDL and span batches. I'd do something like this. Note the use of SERIALIZABLE ISOLATION to ensure full isolation and XACT_ABORT which will force a rollback on any error. SET XACT_ABORT ON GO SET TRANSACTION ISOLATION LEVEL SERIALIZABLE GO begin Transaction GO exec sp_rename LogTable, LogTableOld; GO CREATE TABLE dbo.LogTable( ...


8

What Oracle doesn't have is a read uncommitted isolation mode. In other words you will not be able to query uncommitted data in another transaction. There are ways of getting information out of a long running transaction - one not mentioned so far is autonomous transactions (which should be used with caution)


8

There are two points of confusion here. The first is that you seem to think 'NULL' and NULL are the same; they're not. The former is a string literal, the latter merely signifies the absence of a value. The second is that you can use NULL in equality comparisons; and you cannot. Try this: SELECT CASE NULL WHEN NULL THEN 1 ELSE 0 END; Result: ---- 0 ...


7

Yes - LogMiner can do this. In fact, if you only want committed transactions, you have to specifically filter the output! And there is TABLE_NAME in V$LOGMINER_CONTENTS, that's how you would look at a single table.


7

Not all transactions will have their commit activity perform much better than their rollback. One such case is the delete operation in SQL. When a transaction deletes rows, these rows are marked as ghost records. Once a commit is issued and a ghost record cleanup task starts, then only are these records 'deleted'. If a rollback was issued instead, it just ...


7

In an ideal world you would have two choices, SNAPSHOT and READ COMMITTED SNAPSHOT (RCSI). Make sure you understand the basics of transaction isolation levels before you decide which is appropriate for your workload. Specifically be aware of the different results you may see as a result of moving to RCSI. This sounds like it's not an ideal world as you ...


7

No. If you try the following from two different connections then the second one will be blocked by the first (visible in sys.dm_os_waiting_tasks) but neither will result in any transaction log activity and running DBCC OPENTRAN will report "No active open transactions" (assuming no other activity). SELECT COUNT_BIG(*) FROM master..spt_values v1 WITH ...


7

I'm answering this with hesitation as there isn't enough information in your description of the problem to be 100% sure this is the best advice. "Hangs or throws an exception" suggests the source of the issue isn't properly understood, so proceed with caution. The simplest solution to this is probably SET XACT_ABORT ON. XACT_ABORT determines whether SQL ...


7

You do not have to shrink the log (ie. make the file smaller) but instead you have to truncate the log (ie. allow the file to be reused internally, so it doesn't grow to start with). How to achieve this depends on your database recovery model. Under SIMPLE recovery model the truncation occurs automatically. Under other models (FULL or BULK-LOGGED) the ...


7

Yes this can be done but you need a third party acting as a transaction coordinator. The standard protocol for this is called Two Phase Commit (2PC). This is usually done with a transaction manager acting as the coordinator. This can also be generalized further to more than two databases. In fact it doesn't even have to be databases as the approach is ...


7

http://dev.mysql.com/doc/refman/5.6/en/innodb-implicit-commit.html says: If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction. That means if your session disconnects for any reason, either by choice, or else because an error occurs like the network connection fails, etc., ...


6

edit: this was written before the question was clarified You could use flashback queries to see the table without your own uncommited data. Consider: SQL> CREATE TABLE my_table 2 AS SELECT ROWNUM ID FROM dual CONNECT BY LEVEL <= 5; Table created SQL> INSERT INTO my_table VALUES (6); 1 row inserted To see the difference between the table ...


6

Correct, use SNAPSHOT isolation to get consistent, commited data from before the transaction started. The READ UNCOMMITTED isolation (aka NOLOCK hint) will read dirtz, inconsistent data When you enable SNAPSHOT isolation, then it takes effect for all SELECTs going forward. You run ALTER DATABASE with READ_COMMITTED_SNAPSHOT in this case Edit: added link ...


6

You can get the SCN for a row with the ORA_ROWSCN pseudocolumn Unless you have set row-level dependency tracking for the table, this will report the SCN of the last change to the block the row is in, which may not be much use. You can turn on row-level dependency tracking at create table time only, so you may need to drop and re-create your table. Note ...



Only top voted, non community-wiki answers of a minimum length are eligible