{"id":431,"date":"2018-11-27T01:27:16","date_gmt":"2018-11-26T12:27:16","guid":{"rendered":"http:\/\/www.helenanderson.co.nz\/?p=431"},"modified":"2020-07-23T22:13:43","modified_gmt":"2020-07-23T10:13:43","slug":"sql-indexes","status":"publish","type":"post","link":"https:\/\/helenanderson.co.nz\/sql-indexes\/","title":{"rendered":"Speed up SQL queries with indexes"},"content":{"rendered":"\n

The classic analogy for SQL indexes goes … Databases are like libraries.<\/p>\n\n\n\n

Tables are like books stored in a library.<\/p>\n\n\n\n

Rows are stored on pages of a book.<\/p>\n\n\n\n

Flipping through a textbook page by page looking for that one page you need is going to take time. The same way scanning millions of rows is going be time-consuming and tedious. That’s where SQL indexes come in.<\/p>\n\n\n\n


\n\n\n\n

Why do I need an index?<\/a>
Are there different types of indexes?<\/a>
Where can I find my index once it’s created?<\/a>
What are good candidates for indexes?<\/a>
Can’t I do this later?<\/a>
Do I need to do this at all?<\/a>
How many indexes are too many?<\/a>
Is this the answer to all my performance problems?<\/a><\/strong><\/p><\/blockquote>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Why do I need an index?<\/h2>\n\n\n\n

Indexes speed up performance by either ordering the data on disk so it’s quicker to find your result or telling the SQL engine where to go to find your data. If you don’t apply an index, the SQL engine will scan through every row one by one.<\/p>\n\n\n\n

While this isn’t necessarily a bad thing, as your database grows things could start to slow down.<\/p>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Are there different types of indexes?<\/h2>\n\n\n\n

There are two main types in SQL Server:<\/p>\n\n\n\n

Clustered Index – the contents page<\/h4>\n\n\n\n

Physically arranges the data on disk in a way that makes it faster to get to.<\/p>\n\n\n\n

You can only apply one per table because the data can only be ordered one way.<\/p>\n\n\n\n

<\/figure><\/div>\n\n\n\n
create clustered index [id_idx] \non [dbo].[actor_registration](actor_id)<\/pre>\n\n\n\n
\n\n\n\n

Non-clustered Index – the index at the back of a book.<\/h4>\n\n\n\n

These create a lookup<\/a> that points to where the data is.<\/p>\n\n\n\n

You can create up to 999 but as each index carries overhead and maintenance, you’ll probably want to stick to just a few.<\/p>\n\n\n\n

<\/figure><\/div>\n\n\n\n
create nonclustered index [last_name_idx] \non [dbo].[actor_registration](last_name)<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Where can I find my index?<\/h2>\n\n\n\n

You can find the indexes on a table by expanding the table where the index is, then expanding Indexes.<\/p>\n\n\n\n

<\/figure><\/div>\n\n\n\n
\n\n\n\n

This is also where you can create an index using the wizard. In the example below the option for Clustered Index is now greyed out because we have one on this table already.<\/p>\n\n\n\n

<\/figure><\/div>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

What are good candidates for indexes?<\/h2>\n\n\n\n

Ideally something unique, sequential and small that you are using in SELECTs and JOINs<\/a>.<\/p>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Can’t I do this later?<\/h2>\n\n\n\n

Sure, no problem.<\/p>\n\n\n\n

Sometimes it’s good to get a handle on how you are querying the data and then add them later.<\/p>\n\n\n\n

If you have no clustered indexes on your table the data is stored in the order it comes in. This is called Heaped Storage<\/em> and is effectively an expensive way of storing a spreadsheet.<\/p>\n\n\n\n

Be aware indexes take time to apply if your tables are large by the time you get to this task it may take some time.<\/p>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Do I need to do this at all?<\/h2>\n\n\n\n

There’s no rule saying you should or shouldn’t. The advantages of not adding indexes are that your INSERTs and UPDATEs will be faster and your database will be physically smaller.<\/p>\n\n\n\n

If you do notice things getting slow, check out the Execution Plan for any suggestions and more information on where the effort is going to execute your query.<\/p>\n\n\n\n

<\/figure><\/div>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

How many indexes are too many?<\/h2>\n\n\n\n

As always, it depends. Too many indexes may slow down performance. Once you’ve created an index for your Primary Key and Unique Keys it will be up to you, the Execution Plan and perhaps your friendly DBA<\/a> as to what you do next.<\/p>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Is this the answer to all my performance problems?<\/h2>\n\n\n\n

Indexes need maintenance. They may improve performance initially, but need to be reviewed, updated and maintained as your database grows<\/a>. They aren’t a ‘set and forget’ magic bullet and should be reviewed, and deleted, as your requirements<\/a> change.<\/p>\n\n\n\n

Your best bet at first is to use the Execution Plan to view its suggestions or ask your friendly DBA to lend a hand.<\/p>\n\n\n\n


\n\n\n\n

Photo by Nathan Salt<\/a><\/strong> from Pexels<\/a><\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"

Your query is running slow, have you checked your indexes?<\/p>\n","protected":false},"author":1,"featured_media":2571,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":""},"categories":[214],"tags":[198,199],"yoast_head":"\nSpeed up SQL queries with indexes | Helen Anderson<\/title>\n<meta name=\"description\" content=\"Scanning millions of rows each time you run a query is going be time-consuming and tedious. That's where SQL indexes come in.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Speed up SQL queries with indexes | Helen Anderson\" \/>\n<meta property=\"og:description\" content=\"Scanning millions of rows each time you run a query is going be time-consuming and tedious. That's where SQL indexes come in.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/\" \/>\n<meta property=\"og:site_name\" content=\"Helen Anderson\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-26T12:27:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-23T10:13:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2018\/11\/sql-indexes.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"853\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:site\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/helenanderson.co.nz\/#website\",\"url\":\"https:\/\/helenanderson.co.nz\/\",\"name\":\"Helen Anderson\",\"description\":\"Data Analyst | Technical Writer\",\"publisher\":{\"@id\":\"https:\/\/helenanderson.co.nz\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/helenanderson.co.nz\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#primaryimage\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2018\/11\/sql-indexes.jpg\",\"width\":1280,\"height\":853,\"caption\":\"sql-indexes\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#webpage\",\"url\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/\",\"name\":\"Speed up SQL queries with indexes | Helen Anderson\",\"isPartOf\":{\"@id\":\"https:\/\/helenanderson.co.nz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#primaryimage\"},\"datePublished\":\"2018-11-26T12:27:16+00:00\",\"dateModified\":\"2020-07-23T10:13:43+00:00\",\"description\":\"Scanning millions of rows each time you run a query is going be time-consuming and tedious. That's where SQL indexes come in.\",\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#webpage\"},\"author\":{\"@id\":\"https:\/\/helenanderson.co.nz\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"headline\":\"Speed up SQL queries with indexes\",\"datePublished\":\"2018-11-26T12:27:16+00:00\",\"dateModified\":\"2020-07-23T10:13:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/helenanderson.co.nz\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"image\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/sql-indexes\/#primaryimage\"},\"keywords\":\"analysis,sql\",\"articleSection\":\"Data Analysis\",\"inLanguage\":\"en-NZ\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/helenanderson.co.nz\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\",\"name\":\"Helen Anderson\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/helenanderson.co.nz\/#personlogo\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/helenanderson.co.nz\/wp-content\/uploads\/2019\/11\/helen-anderson-profile-selects-FA-1000.jpg\",\"width\":1000,\"height\":1000,\"caption\":\"Helen Anderson\"},\"logo\":{\"@id\":\"https:\/\/helenanderson.co.nz\/#personlogo\"},\"description\":\"Hi, I'm Helen. I'm a data analyst, technical writer, and AWS Data Hero. I interpret the story behind the numbers, build data applications, and grow analyst and developer communities - currently at Kiwibank.\",\"sameAs\":[\"http:\/\/www.helenanderson.co.nz\/\",\"https:\/\/www.instagram.com\/helenanders26\/\",\"https:\/\/www.linkedin.com\/in\/helenanders26\/\",\"https:\/\/twitter.com\/helenanders26\",\"https:\/\/www.youtube.com\/channel\/UCttVhJizwkhgmMlDBMUE0wQ\"]}]}<\/script>\n","_links":{"self":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/431"}],"collection":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":24,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":3389,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/431\/revisions\/3389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/media\/2571"}],"wp:attachment":[{"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helenanderson.co.nz\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}