Posts

Showing posts with the label Database

How SQL Indexes Work internally ?

Image
When an index is created, it records the location of values in a table that are associated with the column that is indexed. Entries are added to the index when new data is added to the table. When a query is executed against the database and a condition is specified on a column in the WHERE clause that is indexed, the index is first searched for the values specified in the WHERE clause. If the value is found in the index, the index returns the exact location of the searched data in the table. Figure 16.1 illustrates how an index functions. Suppose the following query was issued: SELECT *  FROM TABLE_NAME WHERE NAME = 'SMITH'; As shown in Figure 16.1, the NAME index is referenced to resolve the location of all names equal to 'SMITH'. After the location is determined, the data can be quickly retrieved from the table. The data, in this case names, is alphabetized in the index. A full table scan would occur if there were no index on the table and the same que...

What is single and two-phase Commit? Diff Between Xa and non-xa datasource?

The difference is that one-phase commit is usually used within one system or database while two-phase commit is used for distributed transactions which span multiple DBs or systems. Let me show you simple example for each One-phase commit BEGIN    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)       VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)       VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); COMMIT; This is classic atomic transaction written in PL/SQL (for Java EE world, imagine EJB method which can be transactional too), there is just one phase where all actions are performed and either the commit or rollback is made. Two-phase commit //pseodocode BEGIN     UPDATE db1; //updates DB on another machine     UPDATE someCloudStorage; //update something on the cloud     INSERT INTO SomeTable VALUES(...); COMMIT; ...