Marconi, the example you cite specifically deals with SQL Server.
What is the difference between clustered index and nonclustered index?
The basic difference between index clustered (grouped) and nonclustered (unbundled) is that in the indices clustered, the structure of the index and the data are in the same file; hence the term clustered (grouped). They are two structures implemented in the same file. And, in the case of indexes nonclustered, these are not grouped with the data, that is, they are in separate files.
In SQL Server the indexes (both clustered how much nonclustered) are implemented using b tree+. Direct research (Seek) is made walking in the tree, until it reaches a leaf node. In the case of sequential reading (scan), occurs directly at sheet level as there is a doubly chained list at that level. If the index is of type clustered, the data is in the sheet node. If it is of type nonclustered, there’s a pointer (Row Locator) indicating where the data are, in the table.
In the indexes clustered lines are kept ordered logically. To understand the implementation, I suggest reading the item Table and Items Structures
, pages 188 to 197 of the book Inside Microsoft SQL Server 2008: T-SQL Querying
, by Itzik Ben-Gan.
On what occasions should I wear one and the other?
For this question there is no single answer, because it depends on the context and, mainly, it is necessary to understand the concepts of natural key, primary key, substitute key etc. This concept you find in the article Primary Key Primer for SQL Server, phil Factor.
According to the previously mentioned article, primary key and index clustered are different things. A primary key is a logical construct and an index clustered is an index with a special physical implementation. When defining index clustered for a key, you determine how the key is implemented.
And the author also points out that the choice of the index clustered can have a strong impact on performance. The candidate key that makes sense as a primary key may not have the characteristics that are required for an index clustered with good performance. A good index clustered is light and easy to make comparisons with it. A good primary key is not always so.
Although by default (default) in SQL Server the primary keys are implemented using index of type clustered, this is not required. You can implement primary key using index nonclustered. Or even have no index for the primary key, but only a statement of uniqueness (Unic).
The choice of index clustered or nonclustered depends on the context. The following tasks make up the recommended index creation strategy:
- Understand the characteristics of the database;
- Understand the characteristics of the most used queries;
- Understand the characteristics of columns used in queries;
- Determine which index options could increase performance in
creation or maintenance of the index;
- Determine the best storage location for the index.
I can say that a grouped index is performed a binary query, and an unassembled index is used a query with Tree B?
From the initial answer, you already know there’s no relationship.
In accordance with SQL Server Index Creation Guide, an index is a disk structure associated with a table, which speeds the recovery of the rows. An index contains keys created from one or more table columns. These keys are stored in a structure (B tree) that enables SQL Server to locate the line or lines associated with the key values quickly and effectively.
In the document Clustered and ungrouped indices described is that grouped indexes classify and store the table data rows based on their key values.
Related: https://answall.com/questions/55118/como-aplica-indexes-para-melhorar-a-performance-das-queries
– Jeferson Almeida