Solved

10774: index clustered is better?? always?

Posted on 2015-02-21
1
81 Views
Last Modified: 2015-02-21
Hi experts, because in this example is better to create a clustered index

--1. Crear una tabla temporal cargada con registros, para ello, ejecutar el siguiente
use AdventureWorks
GO
      Select * into
      PODTemp
      from Purchasing.PurchaseOrderDetail

--2. Para evaluar la cantidad de scans, logical reads, phisical reads, etc., ejecutar el
set statistics io on
      select * from PODTemp
      where productid between 500 and 510
      order by productid
set statistics io off
      --Table 'PODTemp'. Scan count 1, logical reads 78,

--3. Crear un índice nonclustered.
Create Nonclustered Index IDX1 on dbo.PODTemp (ProductId)

--4
--Volver a ejecutar el script del paso 2 y guardar la información en el tab messages.
set statistics io on
      select * from PODTemp
      where productid between 500 and 510
      order by productid
set statistics io off
--Table 'PODTemp'. Scan count 1, logical reads 78,

--5
--Ahora, se elimina y se creamos un índice clustered, para lo cual se ejecutan las siguientes sentencias (en el orden como figuran en cada fila)
      Drop Index IDX1 on dbo.PODTemp
      
      Create Clustered Index IDX1 on dbo.PODTemp (ProductId)

--6
set statistics io on
      select * from PODTemp
      where productid between 500 and 510
      order by productid
set statistics io off

--Table 'PODTemp'. Scan count 1, logical reads 8,
0
Comment
Question by:enrique_aeo
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40623259
>where productid between 500 and 510
this is what is called a "range query"

a clustered index on that field will organise the records with the same values together (in the same page blocks), and hence such range queries will work "best" (least reads) when supported by such a index.

this also is good when you have several records with the same value (like a invoice table with the clustered index on customer id )
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question