Saturday, October 27, 2007

Much ado about Stored procedures

1. Stored procedure execution plans can be reused, staying cached in SQL Server's memory, reducing server overhead.
2. Reduce network traffic
3. Provide better security to your data. If you use stored procedures exclusively, you can remove direct SELECT, INSERT, UPDATE, and DELETE rights from the tables and force developers to use stored procedures as the method for data access.
4. Encapsulate business logic or decision making processes


Whenever a stored procedure is executed for the first time, its execution plan is compiled and stored in the SQL Server’s procedure cache. For any subsequent run of the stored procedure, it doesn’t need to compile it again as it is already pre-compiled. This is unlike any embedded statement in the application that needs to be compiled each time it is fired.

Indexes, anyone?

An Index stores the location of the row being looked up. It speeds up data retrieval as the engine does not have to perform a complete ‘table scan’ to find the row being queried.

A clustered index physically sorts the table upon the member columns. A non-clustered index stores the key value and the row index where to find that row.

A nice example to explain this difference is a book index that stores terms with page numbers and behaves like non-clustered index; while a phone book stores names and numbers – if the number is stored with the name – behaves like a clustered index.

Little on Temporary tables

Temporary tables are used to hold temporary data for the connection that creates the table. These tables get created in the tempdb system database.

There is one more interesting difference from a permanent – a foreign key cannot be defined on a temporary table.

The temp table exists only for the connection that creates it. No other connection can see the same temp table.


Difference between temporary tables and table variables:


1. Table variables are transaction neutral, i.e., transaction logs are not maintained for changes to temporary variables
2. Table variables exist only in the same scope as variables. Contrary to the temporary tables, they are not visible to inner stored procedures and exec (string) statements. Also, they cannot be used in an insert/exec statement.


create table #T (s varchar(128))
declare @T table (s varchar(128))

insert into #T select 'old value #'
insert into @T select 'old value @'
begin transaction
update #T set s='new value #'
update @T set s='new value @'
rollback transaction

select * from #T
select * from @T

s
---------------
old value #

s
---------------
new value @

Wednesday, September 19, 2007

Count Rows

select rowcnt from sysindexes where id = object_id('tablename') and indid = 1 --assuming there exist a clustered index

select rowcnt from sysindexes where id = object_id('tablename') and indid = 0 --assuming table is a heap

Run following to fix errors with non-clustered indexes:
dbcc updateusage(0,'t') with count_rows

n-th largest/smallest value in a table

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


Change > TO < for Smallest Number

B-tree reference

http://en.wikipedia.org/wiki/B-tree

SQL Memory

SQL Server memory is primarily used to store data (buffer cache) and query plans (procedure cache).