Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to have an index on every combination of queryable fields in a SQL table to optimize performance?

If my User table has several fields that are queryable (say DepartmentId, GroupId, RoleId) will it make any speed difference if I create an index for each combination of those fields?

By "queryable", I'm referring to a query screen where the end user can select records based on Department, Group or Role by selecting from a drop-down.

At the moment, I have a index on DepartmentId, GroupId and RoleId. That's a single non-unique index per field.

If an end user selects "anyone in Group B", the SQL looks like:

select * from User where GroupId = 2

Having an index on GroupId should speed that up.

But if the end user select "anyone in Group B and in Role C", the SQL would look like this:

select * from User where GroupId = 2 and RoleId = 3

Having indexes on GroupId and RoleId individually may not make any difference, right?

A better index for that search would be if I had one index spanning both GroupId and RoleId.

But if that's the case, than that would mean that I would need to have an index for every combination of queryable fields. So I would need all these indexes:

  • DepartmentId
  • GroupId
  • RoleId
  • DepartmentId and GroupId
  • DepartmentId and RoleId
  • GroupId and RoleId
  • Department Id, GroupId and RoleId

Can anyone shed some light on this? I'm using MySQL if that makes a difference.

like image 440
sohtimsso1970 Avatar asked Jul 27 '11 20:07

sohtimsso1970


People also ask

Why should we not create indexes on every column in a SQL table?

Having too many indexes could potentially affect the overall write speed of your database and result in unneeded contention and table locking for too long. Also indexes are optimal when they cover (are defined) on the fields that are part of the predicates ( JOIN , WHERE , or HAVING clauses) of your queries.

Should every table have an index?

Yes, every table should have a clustered index. The clustered index sets the physical order of data in a table.

What is the purpose of having an index on a table in SQL?

A SQL index is used to retrieve data from a database very fast. Indexing a table or view is, without a doubt, one of the best ways to improve the performance of queries and applications. A SQL index is a quick lookup table for finding records users need to search frequently.

What is the purpose of the index in SQL Server to enhance the query performance all of the mentioned to provide an index to a record to perform fast searches?

1 Answer. The correct answer to the question “What is the purpose of the index in SQL server” is option (d). All of the mentioned Indexes are used in SQL Server to gain enhance the query performance and to provide an index to a record. Therefore, the correct answer is All of the mentioned.

How do you determine if an index is required or necessary in SQL Server?

The views can tell you which columns should be key columns, which columns should be included, and most importantly, how many times the index would have been used. A good approach would be to sort the missing indexes query by number of seeks, and consider adding the top indexes first.

What happens if index is created on all columns?

No, you should not index all of your columns, and there's several reasons for this: There is a cost to maintain each index during an insert, update or delete statement, that will cause each of those transactions to take longer. It will increase the storage required since each index takes up space on disk.


2 Answers

A multi-column index can be used for any left prefix of that index. So, an index on (A, B, C) can be used for queries on (A), (A, B) and (A, B, C), but it cannot, for example, be used for queries on (B) or (B, C).

If the columns are all indexed individually, MySQL (5.0 or later) may also use Index Merge Optimization.

like image 54
Joe Stefanelli Avatar answered Nov 15 '22 20:11

Joe Stefanelli


Generally speaking, indexes will increase query speed, but decrease insert/update speed, and increase disk space/overhead. So asking if you should index each combination of columns is like asking if you should optimize every function in your code. It may make some things faster, or it may barely help, and it might just hurt more than it helps.

The effectiveness of indexes depends on:

  • Percentage of SELECTs vs. INSERTs and UPDATEs
  • The specifics of the SELECT queries, and whether they use JOINs
  • Size of table being indexed
  • RAM and processor speed
  • MySQL settings for how much RAM to use, etc

So, it's hard to give a general answer. The basic sound advice would be: Add indexes if queries are too slow. And remember to use EXPLAIN to see which indexes to add. Note that this is kind of like the database version of the general advice: Profile your app before spending time on optimization.

like image 21
Steven Avatar answered Nov 15 '22 20:11

Steven