"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

July 26, 2010

Table Partitioning - Basics

Today I want to revisit on SQL Server Table partitioning basics. To get started

Step 1 - Refresh the SQL Server Notes on Partitioning by Jose Barreto

Step 2 - Horizontal Vs Vertical Partitioning (Reference - Link)
Hortizontal Partition - Partition based on certain column value. Requires Partition Scheme, Partition Function and Partition Tables. Table will have all the columns
Vertical Partitioning - Divide table into multiple tables with fewer columns

Step 3 - Experiment Hortizontal Partitioning
a. Partition Function - A partition function specifies how the table or index is partitioned
b. Partition Scheme - Defines File groups mapping
c. Creating Partitioned Table

Creating a Partitioned Table for Class (Partitioned based on Class 1-12). When we query on a class only the particular partition is queried.
--STEP 1
IF EXISTS (SELECT * FROM sys.partition_functions WHERE name = 'TestPartitionFunction')
DROP PARTITION FUNCTION TestPartitionFunction;
GO
create partition function TestPartitionFunction (int) as range for values (1,2,3,4,5,6,7,8,9,10);

--STEP 2
IF EXISTS (SELECT * FROM sys.partition_schemes WHERE name = 'TestPartitionScheme')
DROP PARTITION SCHEME TestPartitionScheme;
GO
create partition scheme TestPartitionScheme as partition TestPartitionFunction all to ([PRIMARY]);
GO

--STEP 3
IF EXISTS (SELECT * FROM sys.tables
WHERE name = 'CLASSTable')
DROP TABLE CLASSTable;
GO

--STEP 4
create table dbo.CLASSTable (Class int, Name VARCHAR(200), Studentid int) on TestPartitionScheme (Class);
GO
CREATE CLUSTERED INDEX CIX ON CLASSTable(Class) ON TestPartitionScheme(Class)
GO

--STEP 5 Insert Data
declare @i int, @j int, @k int;
set @i=1;
set @j=100;
set @k = 1;
while (1=1)
begin;
INSERT INTO CLASSTable (Class, Name, Studentid)
VALUES (@i, Convert(VARCHAR(100),@j)+'Name',@j)
set @j=@j+1;
set @k = @k+1;
if(@k>100)
begin
set @k = 0;
set @j = @j+100;
set @i = @i+1;
end
if(@i>10)
begin
break;
end
end;
go

--Data in Each Partition
SELECT $PARTITION.TestPartitionFunction(class) AS Partition, COUNT(*) AS [COUNT]
FROM dbo.CLASSTable
GROUP BY $PARTITION.TestPartitionFunction(class)
ORDER BY Partition ;

--Data Searched only in required partition
SELECT *,$partition.TestPartitionFunction(Class) AS 'Partition Detail'
FROM CLASSTable WHERE Studentid=108

--Verify
set statistics profile on;
SELECT * FROM dbo.CLASSTable WHERE Studentid = 108 and Class = 1

Related Reads - Enabling Partition Level Locking in SQL Server 2008
Using Filtered Statistics with Partitioned Tables.
Partitioned Tables and Indexes in SQL Server 2005
Partitioning Summary: SQL Server 2005 vs SQL Server 2008


Happy Learning!!!

July 17, 2010

Alogorithm Problems

Alogorithm Problems
Question #1 - Given an array of N integers in which each element from 1 and N-1 occurs once and one occurs twice. Write an algorithm to find duplicate integer. Note- You may destroy the array.
Solution1:
Example Array is {1,2,4,7,8,9,4}
Use Negation to Identify the duplicate one. When you negate every element when you parse it would be like
{0,0,4,0,0,0}. Since 4 occurs more than once for the second time it would be 0-(-4). The resulting <> 0 number in the array is the duplicate occurence.
Note: Same logic can be used to count number of unique elements in an array.

Solution 2: Use XOR for solving this problem
Question#2 - Write a function to reverse a word in place O(n) time and O(1) space.
Solution:
void ReverseWord( char *t, int len)
{
  if(len<=1) return 1;
  Swap(&s[0],&s[len-1]);
  ReverseWord(s+1,len-2);
}

Question#3- Find Longest Sequence Palindrome in a String "abcabcad"
This is a Dynamic Programming problem.
Answer: Link1, Link2

Dynamic Programming
Good problems and answers
From the above link posting the solution
Let us define the maximum length palindrome for the substing x[i,...,j] as L(i,j)
Procedure compute-cost(L,x,i,j)

Input: Array L from procedure palindromic-subsequence; Sequence x[1,...,n] i and j are indices
Output: Cost of L[i,j]
if i = j:
    return L[i,j]
else if x[i] = x[j]:
    if i+1 < j-1:
        return L[i+1,j-1] + 2
    else:
         return 2
else:
return max(L[i+1,j],L[i,j-1])

Read Quote of Michal Danilák's answer to Dynamic Programming: Are there any good resources or tutorials for Dynamic Programming besides TopCoder tutorial? on Quora

More Reads
Are there any good resources or tutorials for Dynamic Programming besides TopCoder tutorial ?


Happy Learning!!!!

July 12, 2010

Web Driver

WebDriver (Built on top of selenium by google. UI Testing Framework)
Simple Example
1. Installed webdriver from link http://code.google.com/p/selenium/?redir=1
2. Extract and start eclipse plug-in. Create a Java Project and Add the Extracted JAR files in the project

3. Copied the code from Link - http://google-opensource.blogspot.com/2009/05/introducing-webdriver.html. Modified the code to try it out in irctc.co.in
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestPoc1 {
/**
* @param args
*/
public static void main(String[] args)
{
    // TODO Auto-generated method stub
    // Create an instance of WebDriver backed by Firefox
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.irctc.co.in");
    driver.findElement(By.linkText(("Find Agents"))).click();
    WebElement searchBox = driver.findElement(By.name("pincode"));
    searchBox.sendKeys("560001");
    driver.findElement(By.name("Submit")).click();
}
}
4. Run this code as Java Application in elipse editor
5. References
http://google-opensource.blogspot.com/2009/05/introducing-webdriver.html
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions
http://code.google.com/p/selenium/w/list
http://blog.activelylazy.co.uk/2010/05/05/testing-asynchronous-applications-with-webdriver/
http://www.phpvs.net/2008/02/25/10-tips-for-building-selenium-integration-tests/
http://seleniumhq.org/docs/09_webdriver.html
http://www.theautomatedtester.co.uk/tutorials/selenium/xpath_exercise1.html
New Web Testing Tool -- WebDriver
What is Selenium WebDriver
Selenium Vs Webdriver
Getting Started with WebDriver (a Browser Automation Tool)
Good Reads
Tool #1 – WebDriver (Built on top of selenium by google. UI Testing Framework)

Tool#2 -fighting-layout-bugs a library for automatic detection of layout bugs in web page

July 10, 2010

Learning about website performance optimization

Yep. Very Interesting topic. I am learning by attending sessions and from my peers on website perf optimization. Couple of basic ideas

Basics Learning
How HTML, CSS and JS Work
  • HTML - Contains Text, body, paragraph
  • CSS - Font, Color, Size is defined in CSS
  • JS - Handling Events are defined in this file
Reference - How HTML, CSS and JS work together, CSS-Example
1. CDN (Content Delivery Network) - Delivering content based on Network Proximity. Duplicating data across multiple data centres. Downloads times would be reduced. More details CDN Link1, CDN Link2, CDN Link3
2. Image Spriting - Group Images together, Select Appropriate co-ordinates to display. One large image instead of several multiple small images. More Details Image Spriting1, CSS Sprites
3. Optimize the order of styles and scripts - I found this link extremenly good and self explanatory. Minimize round-trip times. "Browser delays rendering any content that follows a script tag until that script has been downloaded, parsed and executed".  By Adjusting the order download time can be minimized http://code.google.com/speed/page-speed/docs/rtt.html#PutStylesBeforeScripts
4. Combine external JavaScript - Combine multiple files into optimal files. http://code.google.com/speed/page-speed/docs/rtt.html#CombineExternalJS
5. Several other useful techniques Enable Compression, Optimize Images, Caching are provided..
6. A better way to load CSS

More Reading - http://code.google.com/speed/page-speed/docs/rules_intro.html
Even Faster Web Sites Presentation 2 - High Performance Web Sites
Best Practices for Speeding Up Your Web Site
WebPageTest Demo
Waterfalls 101: How to understand your site’s performance via waterfall chart
Let's make the web faster
Page Test Tools: Which results should you trust?
Three kinds of page test visuals, and the best ways to use them
JavaScript Architecture
Unit Testing JavaScript with FireUnit
RequestReduce - Instantly make any ASP.NET website faster with almost no effort!

Tools
Top 10 Free Online Broken Link Checkers
Top 10 Tools to Test Website Speed
Top 10 Websites That Let You Check If Links Are Safe


Happy Reading!!!

July 01, 2010

Learning Selenium - Part II

In continuation to my earlier post. One of my friend asked me Pagination in Selenium. I got help from one of my team members Avinash.

Learning's are
1. Install Firebug add in to firefox
2. Install Xpather add in to firefox
3. From Existing post I want to goto page 2 of Search Results
4. Choose Xpather Location as shown below


5. Select the Xpath for Selecting Second page
6. Add the below line of code as last line. Only from footer part I am selecting the xpath.
selenium.click("//div[@id='foot']/table[@id='nav']/tbody/tr/td[3]/a/span[1]");
7. Modified code snapshot as below


7. Search Results would take you to page 2 of results
Good Read - Identifying XPath of a element using XPath Query
Happy Reading!!!