Saturday, April 10, 2010

common table expression (CTE) vs Cursor in SQL

----------about CTE------------
http://msdn.microsoft.com/en-us/library/ms190766.aspx
----------about CTE------------


Something to be aware of:

Cursors and temp tables are stored in tempdb.

CTEs and derived tables, if there is enough memory, are stored in memory. If they require more storage than is available in memory, they will use space within tempdb.

Views, if they use aggregate functions or are dealing with large volumes of data, may also touch tempdb.

This is important to consider when looking at performance. Operations that take place in memory are much faster. As soon as you have to touch spinning disk, things slow down. You also have to consider file placement (is tempdb on the same spinning disk as something else? Are tempdb's data and log files on the same spinning disk as something else?), disk controllers (including iSCSI NICs) and other sources of I/O contention.

As mentioned above, cursors allow you to access data in a result-set on a row-by-row basis.

Here's a design pattern I like to use:

Load a result set into a table-typed variable. Either use a candidate key or a surrogate key (provided by ROW_NUMBER() OVER () ) to loop through the table. With smaller sets, it can be MUCH faster than using a cursor.

For an example using a candidate key from within the resultset, instead of deriving a candidate key from ROW_NUMBER, just use test_data.id to identify a row.

Sorry about the lack of intentation... linkedin strips out leading spaces and tabs.

IF EXISTS (SELECT * FROM sys.tables WHERE name = 'test_data')
DROP TABLE test_data
GO

CREATE TABLE test_data (column_1 nvarchar(255), column_2 nvarchar(255)
GO

INSERT INTO test_data (column_1, column_2)
VALUES
('1', 'one'),
('2', 'two'),
('3', 'three')
('4', 'four')

DECLARE @results TABLE
(
row_id int,
column_1 nvarchar(255),
column_2 nvarchar(255)
)

-- load up @results with only those rows where column_2 starts with 't'

INSERT INTO @results (id, column_1, column_2)
SELECT
ROW_NUMBER() OVER(ORDER BY column_1),
column_1,
column_2
FROM
test_data
WHERE
UPPER(LEFT(column_2, 1)) = 'T'

/* since row_id is a candidate key for @results, use it as a bookmark. */

DECLARE @current_row int
DECLARE @current_column_1 nvarchar(255)
DECLARE @current_column_2 nvarchar(255)

SELECT @current_row = MIN(id) FROM @results
WHILE @current_row IS NOT NULL
BEGIN
SELECT @current_column_1 = column_1, @current_column_2
FROM @results
WHERE id = @current_row

PRINT 'Current Row ID:' + CHAR(9) + CAST(@current_row as varchar)
PRINT 'Current Column_1:' + CHAR(9) + @current_column_1
PRINT 'Current Column_2':' + CHAR(9) + @current_column_2

-- get the next id value and start the loop again.
-- If there are no more rows, @current_row will be NULL,
-- thus exiting the loop

SELECT @current_row = MIN(id)
FROM @results
WHERE id > @current_row
END

How to obfuscate a DLL in .NET?

whatis obfuscate in .NET?? Code access security is alternative of obfuscate.
Obfuscate = it the process of scrambling and encrypting the software so that it CAN NOT be easily reversed engineered. The goal is to stop casual hacking to crack the code.


here is some good links
http://www.csharp411.com/net-obfuscators/

Code Access Security (CAS) in .NET

Some good articls
http://www.codeproject.com/KB/security/UB_CAS_NET.aspx
http://msdn.microsoft.com/en-us/library/aa302424.aspx