Thursday, May 14, 2009

Using SET ROWCOUNT to limit number of deleted records

I was given MS SQL database with no normalization, no primary keys in tables and lot of duplicates.
How can you delete a duplicate rows from the table, if you have 2 rows where no unique key exists, all fields are equal, so there is no way to distinguish one row from another?

In Oracle you always have row_id so 2 between 2 rows you can delete a row with max(row_id)

In MS SQL you can use SET ROWCOUNT to limit the number of deleted records.

Run the following example, and you wil get and idea.

create table t
(
id int,
name varchar(100)
)
GO

insert t values(1, 'Number One')
insert t values(2, 'Number Two')
insert t values(3, 'Number Three')
insert t values(1, 'Number One')

GO


SELECT count(*) FROM t /* will return 4 */

GO

SET ROWCOUNT 1
DELETE from t where ID = 1 /* will delete only one record from 2 records with ID = 1 */

GO

SELECT count(*) FROM t /* will return 3 */

GO

Friday, May 8, 2009

The cheapest option to deploy Windows SharePoint Service solution for external users

Recently, we were estimating a solution for our customers. They want a Sharepoint (WSS 3.0) solution to be installed on a dedicated server and then used by their 100+ employees (they do not want to install a box in their own domain because don't want to bear additional administrative expenses, and don't want to pay too much for hardware, while renting a dedicated server will let them safely starts with small monthly payments for the hardware.

We were to find the cheapest solution with as less licensing costs as possible

The licensing politics of the Microsoft is very tricky. :) First of all, they recommend Windows Server Web Edition for front end servers. It has no limits in number of users. It costs only approx. 500$. However it is not suited for WSS deployment well, because you cannot install any database except MS SQL Express there. And SQL Express is limited with 4G, but our customer expects to have more then 100 Gb of documents in WSS document libraries.

Well so we though we will have to use SQL Workgroup Edition (to store more then 4Gb) and that means that we need to use Windows Server Standard Edition. We have 100 users, so per-user licensing model of SQL Server is not good for us, but per processor license is also not very cheap. And what if we have 2 processors...

Hopefully, I find the solution that post . It tells that Windows Internal Database, that is automatically installed when you install WSS in basic mode, is actually a special version of MS SQL Express with no memory limitation !

So, we proceed with installing Windows Server Standard Edition (costs approx. 1000$) and that should be enough to deploy WSS solution on one box, using Basic setup option.

Tuesday, April 7, 2009

Multi-dimensional arrays vs arrays of arrays in F#

Recently, I had to write a module with some array-manipulation functionality, and I decided to write it on F#

I had an array initialization function, something like this

let size = 100
let create_empty_array() = Array.create size (Array.create size 0)

Actually I meant to create an array of arrays initialized with zero. :)
But then I found that I have a lot of bugs with the functions that use that array. What an idiot I was when I was writing this!
Of course it will not work, because this code doesn't create 100 arrays of arrays, it actually initializes 1 array int[100] and then creates 100 references to the same array.

So I have to change it for the following

let create_empty_array() = Array.init size (fun i -> Array.create size 0)

The difference is that init function is evaluated value (using lambda expression) for each row, so it will really initialize 100 arrays of 100 integers

If you will use multidimensional arrays (instead of arrays of arrays) you will find that it will more convenient to use Array2 class (for 2-dimensional arrays) or Array3 (for 3-dimentional)...

let create_empty_array() = Array2.create size size 0

So what will you prefer - multidimensional arrays or arrays of arrays?

In my case I use arrays of arrays only to simplify multithreading processing if the need of it will arise in the future. I have a function that takes one row of array for some long-running processing, and I can run several threads to process rows in parallel (currently my function is not working that long, so it is only an imaginary scenario :) )

For those who is interested in some intorduction to arrays in F#, I will recommend the following post http://mariusbancila.ro/blog/?p=109