Popular Posts

Sunday, 29 September 2013

How do database indexes work/How to Create database index

How do database indexes work/How to Create database index


Let’s start out our tutorial and explanation of why you would need a database index by going through a very simple example
SUPPOSE we have a database table named EMP as:
desc emp
Name     Null     Type        
-------- -------- ------------
EMPNO    NOT NULL NUMBER(4)   
ENAME             VARCHAR2(10)
JOB               VARCHAR2(9) 
MGR               NUMBER(4)   
HIREDATE          DATE        
SAL               NUMBER(7,2) 
COMM              NUMBER(7,2) 
DEPTNO            NUMBER(2) 


And we query as:

SELECT * FROM emp WHERE ename = 'KING'
OUTPUT:
EMPNO                  ENAME      JOB       MGR                    HIREDATE                  SAL                    COMM                   DEPTNO                
---------------------- ---------- --------- ---------------------- ------------------------- ---------------------- ---------------------- ----------------------
7839                   KING       PRESIDENT                        17-11-81                  5000                                          10                    


What would happen without an index on the table?

Once we run that query, what exactly goes on behind the scenes to find employees who are named KING? Well, the database software would literally have to look at every single row in the Employee table to see if the Enamefor that row is ‘KING’. And, because we want every row with the name ‘KING’ inside it, we can not just stop looking once we find just one row with the name ‘KING’, because there could be other rows with the name KING. So, every row up until the last row must be searched – which means thousands of rows in this scenario will have to be examined by the database to find the rows with the name ‘KING’. This is what is called a FULL TABLE SCAN....

 

How a database index can help performance..


The whole point of having an index is to speed up search queries by essentially cutting down the number of records/rows in a table that need to be examined.

What is an index?

So, what is an index? Well, an index is a data structure (most commonly a B- tree) that stores the values for a specific column in a table. An index is created on a column of a table. So, the key points to remember are that an index consists of column values from one table, and that those values are stored in a data structure. The index is a data structure – remember that.

How does a hash table index work?

Hash tables are another data structure that you may see being used as indexes – these indexes are commonly referred to as hash indexes. The reason hash indexes are used is because hash tables are extremely efficient when it comes to just looking up values. So, queries that compare for equality to a string can retrieve values very fast if they use a hash index. For instance, the query we discussed earlier (SELECT * FROM Employee WHERE Ename= ‘KING’) could benefit from a hash index created on the Ename column. The way a hash index would work is that the column value will be the key into the hash table and the actual value mapped to that key would just be a pointer to the row data in the table. Since a hash table is basically an associative array, a typical entry would look something like “KING => 0×28939″, where 0×28939 is a reference to the table row where KING is stored in memory. Looking up a value like “KING” in a hash table index and getting back a reference to the row in memory is obviously a lot faster than scanning the table to find all the rows with a value of “KING” in the Ename column.

The disadvantages of a hash index

Hash tables are not sorted data structures, and there are many types of queries which hash indexes can not even help with. For instance, suppose you want to find out all of the employees who are less than 40 years old. How could you do that with a hash table index? Well, it’s not possible because a hash table is only good for looking up key value pairs – which means queries that check for equality (like “WHERE name = ‘KING’”). What is implied in the key value mapping in a hash table is the concept that the keys of a hash table are not sorted or stored in any particular order.

What are some other types of indexes?

Indexes that use a R- tree data structure are commonly used to help with spatial problems. For instance, a query like “Find all of the Starbucks within 2 kilometers of me” would be the type of query that could show enhanced performance if the database table uses a R- tree index.


What exactly is inside a database index?

So, now you know that a database index is created on a column in a table, and that the index stores the values in that specific column. But, it is important to understand that a database index does not store the values in the other columns of the same table. For example, if we create an index on the Ename column, this means that the Job and emp_no or dept_no column values are not also stored in the index. If we did just store all the other columns in the index, then it would be just like creating another copy of the entire table – which would take up way too much space and would be very inefficient.

How does a database know when to use an index?

When a query like “SELECT * FROM Employee WHERE Ename= ‘KING’ ” is run, the database will check to see if there is an index on the column(s) being queried. Assuming the Ename column does have an index created on it, the database will have to decide whether it actually makes sense to use the index to find the values being searched – because there are some scenarios where it is actually less efficient to use the database index, and more efficient just to scan the entire table.

Can you force the database to use an index on a query?

Generally, you will not tell the database when to actually use an index – that decision will be made by the database itself. Although it is worth noting that in most databases (like Oracle and MySQL), you can actually specify that you want the index to be used.


How to create an index in SQL:

Here’s what the actual SQL would look like to create an index on the Employee_Name column from our example earlier:
CREATE INDEX name_index
ON Emp (Ename)

How to create a multi-column index in SQL:

We could also create an index on two of the columns in the Employee table , as shown in this SQL:
CREATE INDEX name_index
ON Employee (ENAME, HIREDATE)


What is the cost of having a database index?

So, what are some of the disadvantages of having a database index? Well, for one thing it takes up space – and the larger your table, the larger your index. Another performance hit with indexes is the fact that whenever you add, delete, or update rows in the corresponding table, the same operations will have to be done to your index. Remember that an index needs to contain the same up to the minute data as whatever is in the table column(s) that the index covers.
As a general rule, an index should only be created on a table if the data in the indexed column will be queried frequently.


HOW TO CHECK COST OF YOUR SQL QUERY ON CPU and PROCESSING TIME

HOW TO CHECK COST OF YOUR SQL QUERY ON CPU and PROCESSING TIME

IF YOUR QUERY IS TAKING TOO MUCH TIME FOR OUTPUTING THE RESULT THEN SOMETHING IS WRONG...EITHER THE QUERY IS NOT OPTIMIZED OR SAY THE INDEX ARE NOT SET ON THE CROSSPONDING TABLES COLUMNS...ETC..
THUS YOU NEED TO FIGURE IT OUT ??????????????????????????
Its quite simple in oracle ..THE EXPLAIN Command help us to fid the cost of processing the query.
this need to be Done on cmd .lets see how?

1. first you need to log on to the database you are using using the credentials,then enter the following command followeded by your query-

SQL>  Explain plan for select a.rain-b.rain as "02:30:00+05:30"  from  rainfall_2010 a,rainfall_2010 b  where  a.raingauge=452 and b.raingauge=452 and a.raindate=to_date('03-07-2010','dd-MM-yyyy' ) and b.rainda
te=to_date('03-07-2010','dd-MM-yyyy' )  and a.raintime= ('02:30:00+05:30') and b.raintime=('01:30:00+05:30');

Explained.

SQL> set long 999999999
SQL> set lines 3000
SQL> @?/rdbms/admin/utlxpls.sql

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------

---------------------------------------------------------------------------
| Id  | Operation            | Name          | Rows  | Bytes | Cost (%CPU)|
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |               |     1 |    60 | 29289   (2)|
|   1 |  MERGE JOIN CARTESIAN|               |     1 |    60 | 29289   (2)|
|   2 |   INDEX SKIP SCAN    | INDX_RAINFALL |     1 |    30 |   361   (0)|

|   3 |   BUFFER SORT        |               |     1 |    30 | 28928   (2)|
|   4 |    TABLE ACCESS FULL | RAINFALL_2010 |     1 |    30 | 28928   (2)|

---------------------------------------------------------------------------



PLAN_TABLE_OUTPUT

Thus here you can see that we get all the operation that are being performed when the query executes .
here we can simply figure it out that TABLE ACCESS FULL | RAINFALL_2010 is done hence the cost is high .
this is due to non availblity of index on the table.

after correcting the necessary things when we again executed the plan then we got much better cost.

-----------------------------------------------------------------------
| Id  | Operation        | Name          | Rows  | Bytes | Cost (%CPU)|
-----------------------------------------------------------------------
|   0 | SELECT STATEMENT |               |     1 |    60 |   722   (1)|
|   1 |  HASH JOIN       |               |     1 |    60 |   722   (1)|
|   2 |   INDEX SKIP SCAN| INDX_RAINFALL |     1 |    30 |   361   (0)|

|   3 |   INDEX SKIP SCAN| INDX_RAINFALL |     1 |    30 |   361   (0)|-----------------------------------------------------------------------


THus it help you to analyze your query and take necessary action.

To see WHAT IS INDEX ,WHY TO PUT INDEX,HOW TO PUT INDEX VISIT THE ARTICLE:


HOW TO CONVERT WEBPAGE INTO PDF FILE

HOW TO CONVERT WEBPAGE INTO PDF FILE

Here I will explain how to export webpage with images to PDF in asp.net using iTextSharp in c#, vb.net.

In asp.net we don’t have a direct feature to export gridview data to PDF for that reason here I am using third party library ITextSharp reference. First download dll from this site http://sourceforge.net/projects/itextsharp/[^]
OR
after that create one new website in visual studio and add ITextsharp dll reference to newly created website after that write the following code in aspx page like this


THE CODE DESIGN :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebpageToPdf.aspx.cs" Inherits="WebpageToPdf" EnableEventValidation="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">

<div><b>Export Webpage with images to pdf using itextsharp dll</b></div><br />
<div>
THIS IS THE TEST IMAGE ADDED FROM INTERNET:
 
  <div><img src="http://i328.photobucket.com/albums/l336/glamgalz/funzug/imgs/inspirational/cool_thoughts_persons_08.jpg" /></div>
 
  THIS IS THE TEST IMAGE ADDED FROM WEBAPPLICATION/PC FOLDER:
   <div>       <img src="images/thumbs/13.jpg" /></div>


</div>

<div>
SOME CONTENT IN WEB PAGE:
<asp:GridView ID="gvDetails" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
<asp:Button ID="btnPDF" runat="server" Text="Export to PDF" OnClick="btnPDF_Click"/>
</form>
</body>
</html>YOU HAVE NOTICE THAT I HAVE KEPT EnableEventValidation="false" in bold due to following reason:
ERROR :
RegisterForEventValidation can only be called during Render();

This error occurs whenever we are trying to render control to response. To solve this problem I have added EnableEventValidation="false" to @Page directive of aspx page that should be just like this 

By Setting EnableEventValidation="false" property to @Page directive of aspx page the problem has solved automatically.




THE CODE BEHIND:



using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class WebpageToPdf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Columns.Add("Location", typeof(string));
        DataRow dtrow = dt.NewRow();    // Create New Row
        dtrow["UserId"] = 1;            //Bind Data to Columns
        dtrow["UserName"] = "PIYUSHRANA";
        dtrow["Education"] = "B.Tech";
        dtrow["Location"] = "NOIDA";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();               // Create New Row
        dtrow["UserId"] = 2;               //Bind Data to Columns
        dtrow["UserName"] = "PRAVEEN";
        dtrow["Education"] = "MBA";
        dtrow["Location"] = "MUMBAI";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();              // Create New Row
        dtrow["UserId"] = 3;              //Bind Data to Columns
        dtrow["UserName"] = "GANESH";
        dtrow["Education"] = "B.Tech";
        dtrow["Location"] = "MUMBAI";
        dt.Rows.Add(dtrow);

        dtrow = dt.NewRow();              // Create New Row
        dtrow["UserId"] = 3;              //Bind Data to Columns
        dtrow["UserName"] = "MAHESH";
        dtrow["Education"] = "B.Tech";
        dtrow["Location"] = "CHENNAI";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();              // Create New Row
        dtrow["UserId"] = 3;              //Bind Data to Columns
        dtrow["UserName"] = "SANDEEP";
        dtrow["Education"] = "B.Tech";
        dtrow["Location"] = "MUMBAI";
        dt.Rows.Add(dtrow);

        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void btnPDF_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        //if you want to add/insert images from your folder then add the below two lines
        string imageFilePath = Server.MapPath(".") + "/images/thumbs/13.jpg";
        iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imageFilePath);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
      
       png.ScaleToFit(110, 110);
        png.Alignment = iTextSharp.text.Image.UNDERLYING;
          png.SetAbsolutePosition(480, 800);

        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(png);
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
}


How to: Secure Connection Strings When Using Data Source Controls

How to: Secure Connection Strings When Using Data Source Controls 



When working with data source controls it is recommended that you centralize the location of your connection strings by storing them in the application's Web.config file. This simplifies the management of connection strings by making them available to all of the ASP.NET pages in a Web application. In addition, you do not need to modify numerous individual pages if your connection string information changes. Finally, you can improve the security of sensitive information stored in a connection string, such as the database name, user name, password, and so on, by encrypting the connection string section of the Web.config file using protected configuration.


To store a connection string in the Web.config file

Open the Web.config file for your application. If a Web.config file does not already exist, create a text file named Web.config and add the following content:

<?xml version="1.0"?>
<configuration
        xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <connectionStrings>
    </connectionStrings>
    <appSettings/>
    <system.web>
    </system.web>
</configuration>
In the connectionStrings element, create an add element for each connection string you will use in your Web application. Include the attributes shown in the following table.
Attribute Description
name
A name for this connection string configuration object. This name will be used by data source controls and other features to reference the connection string information.
connectionString
The connection string to the data source.
providerName
The namespace of the NET Framework data provider to use for this connection, such as System.Data.SqlClient, System.Data.OleDb or System.Data.Odbc.
A completed connectionStrings element might look like the following example:
<connectionStrings>
  <add 
    name="NorthwindConnection" 
    connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />
</connectionStrings>
Save and close the Web.config file.
You can now reference the connection string for your data source control by referring to the name you specified for the name attribute.
In the ConnectionString attribute for your data source control, use the connection string expression syntax to reference the connection information from the Web.config file.
The following example shows a SqlDataSource control in which the connection string is read from the Web.config file:
<asp:SqlDataSource ID="ProductsDataSource" Runat="server"
    SelectCommand="SELECT * from Products"
    ConnectionString="<%$ ConnectionStrings: NorthwindConnection %>"
</asp:SqlDataSource>

To encrypt connection string information stored in the Web.config file

At the Windows command line, run the ASP.NET IIS registration tool (aspnet_regiis.exe) with the following options:

1.The -pe option, passing it the string "connectionStrings" to encrypt the connectionStrings element.
2.The -app option, passing it the name of your application.
3.The aspnet_regiis.exe tool is located in the %systemroot%\Microsoft.NET\Framework\versionNumber folder.
The following example shows how to encrypt the connectionStrings section of the Web.config file for an application named SampleApplication:

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"

When the command has finished, you can view the contents of the Web.config file. The connectionStrings configuration section will contain encrypted information instead of a clear-text connection string, as shown in the following example:
<configuration>
   <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
      <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
         xmlns="http://www.w3.org/2001/04/xmlenc#">
         <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
         <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
               <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
               <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                  <KeyName>RSA Key
                  </KeyName>
               </KeyInfo>
               <CipherData>
                  <CipherValue>WcFEbDX8VyLfAsVK8g6hZVAG1674ZFc1kWH0BoazgOwdBfinhcAmQmnIn0oHtZ5tO2EXGl+dyh10giEmO9NemH4YZk+iMIln+ItcEay9CGWMXSen9UQLpcQHQqMJErZiPK4qPZaRWwqckLqriCl9X8x9OE7jKIsO2Ibapwj+1Jo=
                  </CipherValue>
               </CipherData>
            </EncryptedKey>
         </KeyInfo>
         <CipherData>
            <CipherValue>OpWQgQbq2wBZEGYAeV8WF82yz6q5WNFIj3rcuQ8gT0MP97aO9SHIZWwNggSEi2Ywi4oMaHX9p0NaJXG76aoMR9L/WasAxEwzQz3fexFgFSrGPful/5txSPTAGcqUb1PEBVlB9CA71UXIGVCPTiwF7zYDu8sSHhWa0fNXqVHHdLQYy1DfhXS3cO61vW5e/KYmKOGA4mjqT0VZaXgb9tVeGBDhjPh5ZlrLMNfYSozeJ+m2Lsm7hnF6VvFm3fFMXa6+h0JTHeCXBdmzg/vQb0u3oejSGzB4ly+V9O0T4Yxkwn9KVDW58PHOeRT2//3iZfJfWV2NZ4e6vj4Byjf81o3JVNgRjmm9hr9blVbbT3Q8/j5zJ+TElCn6zPHvnuB70iG2KPJXqAj2GBzBk6cHq+WNebOQNWIb7dTPumuZK0yW1XDZ5gkfBuqgn8hmosTE7mCvieP9rgATf6qgLgdA6zYyVV6WDjo1qbCV807lczxa3bF5KzKaVUSq5FS1SpdZKAE6/kkr0Ps++CE=
            </CipherValue>
         </CipherData>
      </EncryptedData>
   </connectionStrings>
</configuration>
Leave the command prompt open for later steps.
Determine the user account or identity under which ASP.NET runs by retrieving the current WindowsIdentity name.
The following example shows one way to determine the WindowsIdentity name:
VB
<%@ Page Language="VB" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name)
%>
C#
<%@ Page Language="C#" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>

Note:

By default, on Windows Server 2003 with impersonation for an ASP.NET application disabled in the Web.config file, the identity under which the application runs is the NETWORK SERVICE account. On other versions of Windows, ASP.NET runs under the local ASPNET account.
The user account or identity under which ASP.NET runs must have read access to the encryption key used to encrypt and decrypt sections of the Web.config file. This procedure assumes that your Web site is configured with the default RsaProtectedConfigurationProvider specified in the Machine.config file named "RsaProtectedConfigurationProvider". The RSA key container used by the default RsaProtectedConfigurationProvider is named "NetFrameworkConfigurationKey".
At the command prompt, run the aspnet_regiis.exe tool with the following options:
The -pa option, passing it the name of the RSA key container for the default RsaProtectedConfigurationProvider.
The identity of your ASP.Net application, as determined in the preceding step.

The following example shows how to grant the NETWORK SERVICE account access to the machine-level "NetFrameworkConfigurationKey" RSA key container:

aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"

To decrypt the encrypted Web.config file contents, run the aspnet_regiis.exe tool with the -pd option. 

The syntax is the same as encrypting Web.config file contents with the -pe option except that you do not specify a protected configuration provider. The appropriate provider is identified in the configProtectionProvider attribute for the protected section.
The following example shows how to decrypt the connectionStrings element of ASP.NET application SampleApplication.
aspnet_regiis -pd "connectionStrings" -app "/SampleApplication"
.................................................................................................................................................................