Sunday, October 26, 2014

When to Use a Heap

If a table is a heap and does not have any nonclustered indexes, then the entire table must be examined (a table scan) to find any row. This can be acceptable when the table is tiny, such as a list of the 12 regional offices of a company.

When a table is stored as a heap, individual rows are identified by reference to a row identifier (RID) consisting of the file number, data page number, and slot on the page.
The row id is a small and efficient structure. Sometimes data architects use heaps when data is always accessed through nonclustered indexes

the RID is smaller than a clustered index key.

Saturday, October 11, 2014

Rebuild Index Task and Reorganization Index Task

Rebuild Index Task

1. Rebuilds the indexes specified in the task configuration
  • When multiple database are selected in a single task, all indexes in the selected database will be rebuilt
  • When a single database is selected, individual objects can be selected in that database to rebuild all of the indexes on the selected objects

2. As a side effect all index based statistics are updated with full scan

Limitations

1. Indexes are rebuilt regardless of the fragmentation level
2. Does not support partition level rebuilds for partitioned tables or indexes


Free space options -specifies the free space to leave in the leaf level of the index pages during the rebuild

1.Default free space per page - maintains the currently configured fill factor value for the indexes

2. Change free space per page to -specifies that the free space will be changed to the configured value as a part of the rebuild.


Sort in tempdb option - specifies to store intermediate sort runs in the tempdb database rather than in the database the index is being rebuilt in

Offer the ability to rebuild indexes online if using enterprise edition

- for indexes that can not be rebuilt online, this provide the option to rebuild offline or do nothing at all



Reorganize Index Task

This is reorganizes indexes but does not include a update of the index statistics like the rebuild Index task does

=> As a result, when the reorganize index task is used a subsequent update statistics task should also be included to update the column statistics of the database

Monday, May 20, 2013

Delete the duplicate rows in the sql server


DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)
==============================================================
WITH TempUsers (FirstName,LastName, duplicateRecordCount)
AS
(
SELECT FirstName,LastName,
ROW_NUMBER()OVER(PARTITIONBY FirstName, LastName ORDERBY FirstName) AS duplicateRecordCount
FROM dbo.Users
)
DELETE
FROM TempUsers
WHERE duplicateRecordCount > 1
GO
===============================================================
delete x from (
  select *, rn=row_number() over (partition by EmployeeName order by empId)
  from Employee
) x
where rn > 1;
========================================================
delete T1
from MyTable T1, MyTable T2
where T1.dupField = T2.dupField
and T1.uniqueField > T2.uniqueField

Sunday, April 28, 2013

How to exchange the data of column in the same table in the Sql server


1. Swap with the help of temporary variable:
-------------------------------------------------
DECLARE @temp AS varchar(10) UPDATE Employee1 SET @temp=LastName, LastName=FirstName, FirstName=@temp
 
 
2.directly:
------------

UPDATE Employee1 SET LastName=FirstName, FirstName=LastName

Wednesday, April 24, 2013

Find N-th salary from employee table

Row Number :



SELECT Salary,EmpName FROM
   ( SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary) As RowNum
      FROM EMPLOYEE ) As A
    WHERE A.RowNum IN (2,3)

Sub Query :


SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)





Top Keyword :


SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary

Thursday, April 22, 2010

Index Question 23 April 2010

Index Question 23 April 2010

1. Identity columns is index column?
2. Can we use multiple cluster index in sql query? if yes how?
3. what is composite index ?
4. Can an index declared on multiple column ?
5. if a table has already clustered index , can we create primary key on the table ?
6. what is page split?
7. How to rebuild the index?
8. Can we delete an index used by a primary key or unique constraint ?
9. Can we create index on table variable or temporary table?
10. can we create index on system table?

Backup plans Question from Vikram

Question on Backup plans:
1.What is full, differential and transaction log Backup in SQL Server, and how do they interact with each other?
2. How many Recovery model exist in SQL Server, and their role in the Backup strategy?
3. How does the Transaction log backup maintain log space in different recovery models?
4.In which system databases you can not Backup Transaction logs?
5. What is the differential and incremental backup in SQL Server?
6. What is Point-in-Time recovery in the SQL Server?
7. What is Copy-Only Backups feature in SQL Server?
8. Which System databases can not be backed up at all?
9. What is the Tail-end Transaction log backup, and how is it performed?
10. How is file or filegroup Backups helpful for very large databases?