{"id":153,"date":"2017-09-18T21:15:28","date_gmt":"2017-09-18T21:15:28","guid":{"rendered":"http:\/\/www.manualsqlserver.com\/?p=153"},"modified":"2020-05-20T04:17:27","modified_gmt":"2020-05-20T04:17:27","slug":"indices-ejercicios","status":"publish","type":"post","link":"https:\/\/manualsqlserver.com\/?p=153","title":{"rendered":"Indices en SQL Server &#8211; ejercicios"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"267\" src=\"https:\/\/manualsqlserver.com\/wp-content\/uploads\/2020\/05\/ManualSQL_IndicesD.png\" alt=\"\" class=\"wp-image-1279\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Indices<\/strong> en SQL Server<\/h1>\n\n\n\n<p>Los \u00edndices permiten ordenar los registros de una tabla o vista de acuerdo a uno a mas campos en orden ascendente o descendente. (<a href=\"http:\/\/www.manualsqlserver.com\/?p=158\">Ver \u00edndices<\/a>)<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Al crear <a href=\"http:\/\/www.manualsqlserver.com\/?p=141\">tablas particionadas <\/a>es recomendable crear <a href=\"http:\/\/www.manualsqlserver.com\/?p=557\">\u00edndices particionados<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Ejercicios<\/h3>\n\n\n\n<p><strong>Usando la base de datos Northwind<\/strong><\/p>\n\n\n\n<p>use Northwind<br>go<\/p>\n\n\n\n<p><strong>Crear INDICES<\/strong><\/p>\n\n\n\n<p>Create index NombreIndice<br>on Tabla\/Vista (Campo1 ASC|DESC [, Campo2 Asc|Desc])<br>with (fillfactor = Valor , drop_existing = on) *\/<\/p>\n\n\n\n<p><strong>Crear \u00edndice para la tabla Categories, campo CategoryName<\/strong><\/p>\n\n\n\n<p>create index CategoriaNombreIDXa on Categories(Categoryname)<br>go<\/p>\n\n\n\n<p><strong>Listar los \u00edndices<\/strong><\/p>\n\n\n\n<p>Select * from sys.indexes<br>go<\/p>\n\n\n\n<p><strong>Buscar un \u00edndice espec\u00edfico<\/strong><\/p>\n\n\n\n<p>Select * from sys.indexes where name = &#8216;CategoriaNombreIDXa&#8217;<br>go<\/p>\n\n\n\n<p><strong>Script para crear el \u00edndice s\u00f3lo si el \u00edndice no existe<\/strong><\/p>\n\n\n\n<p>if not exists (Select * from sys.indexes where name = &#8216;CategoriaNombreIDXa&#8217;)<br>Begin<br>create index CategoriaNombreIDXa on Categories(Categoryname)<br>End<br>go<\/p>\n\n\n\n<p><strong>RECOMENDACION: Usar un factor de relleno (fillfactor) de 80<\/strong><\/p>\n\n\n\n<p><strong>\u00cdndice para la tabla Empleados<\/strong><\/p>\n\n\n\n<p>select * from Employees order by LastName, FirstName<br>go<\/p>\n\n\n\n<p><strong>\u00cdndice para LastName + FirstName<\/strong><\/p>\n\n\n\n<p>if not exists (Select * from sys.indexes where name = &#8216;EmpleadosApellidosNombresIDXa&#8217;)<br>Begin<br>Create index EmpleadosApellidosNombresIDXa on Employees (LastName, FirstName)<br>with (fillfactor = 80)<br>End<br>else &#8212; \u00edndice ya existe<br>Begin<br>Create index EmpleadosApellidosNombresIDXa on Employees (LastName, FirstName)<br>with (fillfactor = 80, drop_existing = on)<br>End<br>go<\/p>\n\n\n\n<p><strong>Eliminar un \u00edndice:<\/strong><\/p>\n\n\n\n<p>Drop Index NombreIndice on Tabla\/Vista <\/p>\n\n\n\n<p><strong>Eliminar el \u00edndice CategoriaNombreIDXa de la tabla Categories<\/strong><\/p>\n\n\n\n<p>Drop index CategoriaNombreIDXa on Categories<br>go<\/p>\n\n\n\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2636008503986218\"\n     data-ad-slot=\"1902193434\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Indices en SQL Server Los \u00edndices permiten ordenar los registros de una tabla o vista de acuerdo a uno a mas campos en orden ascendente o descendente. (Ver \u00edndices)<\/p><p><a class=\"more-link btn\" href=\"https:\/\/manualsqlserver.com\/?p=153\">Seguir leyendo<\/a><\/p>\n","protected":false},"author":1,"featured_media":1279,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[53],"class_list":["post-153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-indices","tag-sqlserver","nodate","item-wrap"],"_links":{"self":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":1,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/1280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=\/wp\/v2\/media\/1279"}],"wp:attachment":[{"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manualsqlserver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}