Posts tagged: SQL Server

Compare two tables record (SQL Server)

By Ashish Khandelwal, July 15, 2011

Sometime we want to compare the records between two database tables. These databases could be on same server or on different servers. Here is some information about how we can compare records between two tables. Hope this will help and give you some information.

The ‘EXCEPT’ operator performs the compare between two tables:

When both databases are on same server:

The following query returns any distinct values from the query to the left of the EXCEPT operand that are not also found on the right query.

SELECT ProductID

FROM Production.Product

EXCEPT

SELECT ProductID

FROM Production.WorkOrder ;

–Result: 266 Rows (products without work orders)

Read more »

Quick and Short: Difference between DELETE and TRUNCATE – SQL Server

By Ashish Khandelwal, March 30, 2010

This article is written under ‘Quick and Short’ edition. In this article I will explain about Difference between DELETE and TRUNCATE method.

Read more Quick and Short articles.

Difference between DELETE and TRUNCATE method

DELETE logs the data for each row affected by the statement in the transaction log and physically removes the row from the file, one row at a time. The recording of each affected row can cause your transaction log grow massively if you are deleting huge numbers of rows. However, when you run your databases in full recovery mode, detailed logging is necessary for SQL Server to be able to recover the database to the most recent state, should a problem arise. The fact that each row is logged explains why DELETE statements can be slow.

TRUNCATE is faster than DELETE due to the way TRUNCATE “removes” rows. Actually, TRUNCATE does not remove data, but rather deallocates whole data pages and removes pointers to indexes. The data still exists until it is overwritten or the database is shrunk. This action does not require a lot of resources and is therefore very fast. It is a common mistake to think that TRUNCATE is not logged. This is wrong. The deallocation of the data pages is recorded in the log file. Therefore, BOL refers to TRUNCATE operations as “minimally logged” operations. You can use TRUNCATE within a transaction, and when this transaction is rolled-back, the data pages are reallocated again and the database is again in its original, consistent state.

Some limitations do exist for using TRUNCATE.

  • You need to be db_owner, ddl_admin, or owner of the table to be able to fire a TRUNCATE statement.
  • TRUNCATE will not work on tables, which are referenced by one or more FOREIGN KEY constraints.

So if TRUNCATE is so much faster than DELETE, should one use DELETE at all? Well, TRUNCATE is an all-or-nothing approach. You can’t specify just to truncate those rows that match a certain criteria. It’s either all rows or none.

You can, however, use a workaround here. Suppose you want to delete more rows from a table than will remain. In this case you can export the rows that you want to keep to a temporary table, run the TRUNCATE statement, and finally reimport the remaining rows from the temporary table. If your table contains a column with the IDENTITY property defined on it, and you want to keep the original IDENTITY values, be sure to enabled IDENTITY_INSERT on the table before you reimport from the temporary table. Chances are good that this workaround is still faster than a DELETE operation.

You can also set the recovery mode to “Simple” before you start this workaround, and then back to “Full” one it is done. However, keep in mind that in this case, you might only be able to recover to the last full backup.

——————————————————————————————————————————————————————

Readers are requested to add comment, and add more information in case they want.

Read more Quick and Short articles.

Data Synchronization: Which Technology?

By Ashish Khandelwal, October 23, 2009

You have several options for data synchronization of mobile apps. We look at the advantages and disadvantages of direct access to SQL Server; XML Web services, .NET Remoting, SQL Server CE and Remote Data Access (RDA), SQL Server merge replication, and even give you a handy reference table for making the best choice. 
 
There are a variety of data synchronization techniques for applications that have to operate in the Occasionally Connected Computing (OCC) environment. You should identify at an early design stage which technology is appropriate for a given application. Let’s look at each of several techniques, along with their pros and cons, and a handy table summarizing our findings.Offline or On, Your App Has to Work
The most basic requirement of any mobile application, as defined in the OCC model, is the need for offline capability.You generally consider some form of local data cache, and you must assess the three facets of offline capability: data synchronization, security, and manageability. You need to consider the problems of deciding how often and how much data to cache on the mobile computer. Then we consider how to synchronize changes back to the master data store on the server in a way that is both scalable and manageable. Finally, we have to consider how the communication of data is secured and authenticated.The following sections describe a number of techniques for data synchronization. Each includes a description of the technology, an outline of its advantages and disadvantages, aspects of performance, the proportion of time that a connection has to be available, and the complexity of implementation. Read more »

SQL Query to the remote SQL server

By Ashish Khandelwal, September 25, 2009

Sometimes you want to query to the remote SQL server (Server which is in network). Following statement will help you to fire a query to the remote server:

Select * from [ServerName].[DatabaseName].[Scheme].[TableName]

Before you fire query, you are required to link to the server on which you want to query.  The ‘sp_addlinkedserver’is a system store procedure which can be used to add a link server to the server you are sitting.

EXEC sp_addlinkedserver

@server = ‘SourceServer’

, @Srvproduct = ”

, @Provider = ‘SQLNCLI’

, @datasrc = ‘Remote SQL Server instance name’ Read more »

SQL Server 2005 Performance Dashboard Reports

By Ashish Khandelwal, September 7, 2009

The SQL Server 2005 Performance Dashboard Reports are Reporting Services report files designed to be used with the Custom Reports feature introduced in the SQL Server 2005 SP2 release of SQL Server Management Studio. The reports allow a database administrator to quickly identify whether there is a current bottleneck on their system, and if a bottleneck is present, capture additional diagnostic data that may be necessary to resolve the problem. For example, if the system is experiencing waits for disk IO the dashboard allows the user to quickly see which sessions are performing the most IO, what query is running on each session and the query plan for each statement. Read more »

SQL Server Performance Tuning Tips for Stored Procedures

By Ashish Khandelwal, July 29, 2009

Whenever a client application needs to send Transact-SQL to SQL Server, send it in the form of a stored procedure instead of a script or embedded Transact-SQL. Stored procedures offer many benefits, including:  Read more »