Pages

Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Monday, 19 September 2011

Mongo DB

MongoDB is an document-oriented DBMS. Think of it as a persistent object store. It is neither a relational database, nor even "table oriented" like Amazon SimpleDB or Google BigTable. If you have used object-relational mapping layers before in your programs, you will find the Mongo interface similar to use, but faster, more powerful, and less work to set up.

mongo_3

You can download and extract the binaries from here

Features of using Mongo DB:

1- MongoDB is in the sweet spot of performance and features. Its rich data types, querying, and in place updates reduced development time to minutes from days for modeling rich domain objects.  Chandra Patni

2- With foursquare’s growing popularity, we were fast approaching the point where it would no longer be feasible to store user check-ins on a single physical machine. MongoDB's auto- sharding capabilities enabled us to easily transition to a multi-node cluster that will enable us to continue to grow for the foreseeable future.    Harry Heymann

3- MongoDB supports full consistency and transactional updates.

4- MongoDB aims to provide greater agility and scalability for many applications by eliminating joins and relational modeling

 

You will find some useful tutorials for MongoDB : Tutorial here such as How to run MongoDB , Getting a database connection …. etc.

Sample code to connect to MongoDB :

string ConnectionString = "mongodb://localhost/Persons";

var server = MongoServer.Create(ConnectionString);
var db = server.GetDatabase("Persons");

MongoCollection<Person> collection = db.GetCollection<Person>("Persons");
var personRecords = collection.FindAll();
foreach (var p in personRecords)
{
      dataGridViewPersons.Rows.Add(p.Id, p.Name);
}

Some useful links and resources :

How to use MongoDB update operators

MongoDB and C#

CSharp Driver Tutorial

Connections

Friday, 1 April 2011

Alter Microsoft SQL Server 2008 Table in Design Mode

This article is not about sharing some programming ideas / concepts and some excellent code snippets. It's about sharing the necessary settings that you need to change to work in design mode simply as you did in Microsoft SQL Server 2008 because we can’t alter Microsoft SQL Server 2008 Object (design mode).

ImageA

Image A

 

Solution Steps :

1- Open Microsoft SQL Server Management Studio 2008 ,Click on the tools menu and select Options.A window will appear with the various SQL Server settings options.

2- Select the Designers node and after that, click on the Tables and Database Designer sub node. Here you will find some option of a Table. The following image - B shows the general options of SQL Server database Table object.

ImageB

Image B

3- You will find a checkbox (marked in orange color) named “Prevent saving changes that require table re-creation”. By default, the value of the checkbox is checked. You just need to uncheck the checkbox, that’s all.

Note: you can see this link for more info http://support.microsoft.com/kb/956176

Sunday, 30 January 2011

SQL Server 2005 Paging - The Holy Grail

Introduction

The paging and ranking functions introduced in 2005 are old news by now, but the typical ROW_NUMBER OVER() implementation only solves part of the problem.

Nearly every application that uses paging gives some indication of how many pages (or total records) are in the total result set. The challenge is to query the total number of rows, and return only the desired records with a minimum of overhead? The holy grail solution would allow you to return one page of the results and the total number of rows with no additional I/O overhead.

In this article, we're going to explore four approaches to this problem and discuss their relative strengths and weaknesses. For the purposes of comparison, we'll be using I/O as a relative benchmark.

The 'two-bites' approach

The most basic approach is the 'two-bites' approach. In this approach you, effectively, run your query twice; querying the total rows in one pass, and querying your result set in the second. The code is pretty straightforward:

DECLARE @startRow INT ; SET @startrow = 50 
SELECTCOUNT(*) AS TotRows 
FROM [INFORMATION_SCHEMA].columns

;WITH cols 
AS 

SELECT table_name, column_name, 
  ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq 
  FROM [INFORMATION_SCHEMA].columns 

SELECT table_name, column_name 
FROM cols 
WHERE seq BETWEEN @startRow AND @startRow + 49 
ORDERBY seq

It gives the desired results, but this approach doubles the cost of the query because you query your underlying tables twice:

(1 row(s) affected) 
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
(50 row(s) affected) 
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

The temp table approach

The 'two-bites' approach is especially undesirable if your paged query is very expensive and complex. A common workaround is to write the superset into a temporary table, then query out the subset. This is also the most common way to implement paging pre-2005 (in this case, ROW_NUMBER is superfluous).

DECLARE @startRow INT ; SET @startrow = 50 
CREATETABLE #pgeResults( 
  id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, 
  table_name VARCHAR(255), 
  column_name VARCHAR(255) 

INSERTINTO #pgeResults(Table_name, column_name) 
SELECT table_name, column_name 
FROM [INFORMATION_SCHEMA].columns 
ORDERBY [table_name], [column_name] 
SELECT@@ROWCOUNT AS TotRows 
SELECT Table_Name, Column_Name 
FROM #pgeResults 
WHERE id between @startrow and @startrow + 49 
ORDERBY id

DROPTABLE #pgeResults

Looking at the query plan, you can see that your underlying tables are queried only once but the I/O stats show us that you take an even bigger hit populating the temporary table.

Table '#pgeResults_________________________________________________________________________________________________________000000001A9F'. Scan count 0, logical reads 5599, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 14, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 39, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
(2762 row(s) affected) 
(1 row(s) affected) 
(50 row(s) affected) 
Table '#pgeResults_________________________________________________________________________________________________________000000001A9F'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

In this case, it would be better to query the tables twice. Maybe some new 2005 functionality can yield a better solution.

The COUNT(*) OVER() Approach

OVER() can also be used with Aggregate Window Functions. For our purposes this means we can do a COUNT(*) without the need for a GROUP BY clause, returning the total count in our result set. The code definitely looks much cleaner and, if your application permits it, you can simply return one dataset (eliminating the overhead of writing to a temp table).

DECLARE @startRow INT ; SET @startrow = 50 
;WITH cols 
AS 

SELECT table_name, column_name, 
  ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq, 
COUNT(*) OVER() AS totrows 
  FROM [INFORMATION_SCHEMA].columns 

SELECT table_name, column_name, totrows 
FROM cols 
WHERE seq BETWEEN @startRow AND @startRow + 49 
ORDERBY seq

Unfortunately this approach has it's own hidden overhead: 
Table 'Worktable'. Scan count 3, logical reads 5724, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Where did that come from? In this case, SQL Server implements the COUNT(*) OVER() by dumping all the data into a hidden spool table, which it then aggregates and joins back to your main output. It does this to avoid re scanning the underlying tables. Although this approach looks the cleanest, it introduces the most overhead. 

I've spent most of today cleaning up and old data-paging proc that is both very inefficient and frequently called enough for me to notice it. I've explored probably a dozen other approaches to solving this problem before I came up with the solution below. For the sake of brevity—and because they rest are pretty obscure and equally inefficient —we'll now skip to the best solution.

The Holy Grail

In theory, ROW_NUMBER() gives you all the information you need because it assigns a sequential number to every single row in your result set. It all falls down, of course, when you only return a subset of your results that don't include the highest sequential number. The solution is to return a 2nd column of sequential numbers, in the reverse order. The total number of the records will always be the sum of the two fields on any given row minus 1 (unless one of your sequences is zero-bound).

DECLARE @startRow INT ; SET @startrow = 50 
;WITH cols 
AS 

SELECT table_name, column_name, 
  ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq, 
  ROW_NUMBER() OVER(ORDER BY table_name DESC, column_name desc) AS totrows 
  FROM [INFORMATION_SCHEMA].columns 

SELECT table_name, column_name, totrows + seq -1 as TotRows 
FROM cols 
WHERE seq BETWEEN @startRow AND @startRow + 49 
ORDERBY seq

This approach gives us our page of data and the total number of rows with zero additional overhead! (well, maybe one or two ms of CPU time, but that's it) The I/O statistics are identical to querying just the subset of records. 
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Compare the stats above with the stats and query below (just returning one page of data).

;WITH cols 
AS 

SELECT table_name, column_name, 
  ROW_NUMBER() OVER(ORDER BY table_name, column_name) AS seq 
  FROM [INFORMATION_SCHEMA].columns 

SELECT table_name, column_name 
FROM cols 
WHERE seq BETWEEN @startRow AND @startRow + 49 
ORDERBY seq

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'syscolpars'. Scan count 1, logical reads 46, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. 
Table 'sysschobjs'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Conclusion

I have found this approach to be best suited for smaller resultsets from complex queries where I/O is the primary bottleneck. Jeff Moden, Peso and others here have pointed out that with larger resultsets, the I/O cost you save is more than outweighed by the CPU cost. You definitly want to compare different approches to find the best solution for your problem.

My real goal here was to try and figure out a way to avoid unnecessary I/O overhead. I am sure that this solution is not the last word on the subject and I greatly look forward to hearing your thoughts, experiences and ideas on this topic. Thank you all for reading and for your feedback.