?
Solved

10774: index clustered is better?? always?

Posted on 2015-02-21
1
Medium Priority
?
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

770 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