When working on building targeted foreign key indices to speed up a search, I came up with this code block to auto generate the script for me.
SELECT 'IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = N''IX_' + FK.TABLE_NAME + '_' + CU.COLUMN_NAME + ''') BEGIN CREATE INDEX IX_' + FK.TABLE_NAME + '_' + CU.COLUMN_NAME + ' ON ' + FK.TABLE_NAME + '(' + CU.COLUMN_NAME + '); END GO print N''create IX_' + FK.TABLE_NAME + '_' + CU.COLUMN_NAME + ' done''; RAISERROR (N'' --------------------'', 10,1) WITH NOWAIT', FK.TABLE_NAME AS K_Table, CU.COLUMN_NAME AS FK_Column, PK.TABLE_NAME AS PK_Table, PT.COLUMN_NAME AS PK_Column, C.CONSTRAINT_NAME AS Constraint_Name FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN (SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY') AS PT ON PT.TABLE_NAME = PK.TABLE_NAME WHERE PT.Column_name = 'ID' AND PK.Table_Name = '{COMMONLY QUERIED TABLE}' AND FK.TABLE_NAME <> PK.TABLE_NAME;In this statement, I would replace {COMMONLY QUERIED TABLE} with the table name holding the primary key that was used in queries often. This will generate statements to create an index if one does not exist. You could easily modify it to do a drop and add as well.
converse outlet, herve leger, hollister clothing, lululemon, valentino shoes, oakley, vans, nike air max, bottega veneta, soccer jerseys, mont blanc pens, wedding dresses, vans outlet, celine handbags, nike huaraches, north face outlet, insanity workout, ray ban, mcm handbags, baseball bats, hollister, instyler, p90x workout, ghd hair, nike trainers uk, chi flat iron, iphone cases, nfl jerseys, mac cosmetics, nike roshe run, gucci, babyliss, nike air max, soccer shoes, asics running shoes, beats by dre, north face outlet, ferragamo shoes, converse, abercrombie and fitch, hermes belt, hollister, reebok outlet, new balance shoes, ralph lauren, longchamp uk, jimmy choo outlet, lancel, timberland boots, louboutin
ReplyDeletecanada goose outlet, pandora uk, louis vuitton, louis vuitton, karen millen uk, toms shoes, ugg,ugg australia,ugg italia, ugg uk, moncler uk, louis vuitton, montre pas cher, wedding dresses, canada goose, pandora jewelry, moncler, canada goose outlet, juicy couture outlet, moncler, canada goose, canada goose, moncler, louis vuitton, canada goose uk, ugg, moncler, hollister, thomas sabo, moncler outlet, ugg,uggs,uggs canada, ugg pas cher, coach outlet, pandora jewelry, replica watches, swarovski crystal, supra shoes, louis vuitton, moncler outlet, swarovski, links of london, canada goose outlet, juicy couture outlet, marc jacobs, doudoune moncler, canada goose jackets, pandora charms
ReplyDeletemichael kors outlet
ReplyDeletetrue religion jeans
louis vuitton outlet online
tiffany and co
louis vuitton
louboutin outlet
coach outlet
fitflops sale
oakley sunglasses sale
coach outlet
hzx20161221
فرسان العرب
ReplyDeleteارخص شركة تنظيف سجاد في الرياض
افضل شركة تنظيف سجاد في الرياض
replica bags philippines greenhills replica gucci bags x8l51y6p69 best replica ysl bags read what he said y3d40j4q08 replica bags from china replica bags qatar replica bags high quality home n9k29w3w84 replica bags in gaffar market
ReplyDelete