Order of index on multiple columns

前端 未结 3 873
我在风中等你
我在风中等你 2021-02-12 11:29

Just a simple question, but does the order of your index matter when it spans over multiple columns?

For example, taking the query:

SELECT * FROM my_table WHE         


        
相关标签:
3条回答
  • 2021-02-12 12:11

    This shoudl give you a good idea

    SQL Server Clustered Index - Order of Index Question

    There are many more like this on SO.

    0 讨论(0)
  • 2021-02-12 12:22

    The best way to find out is to measure it. Try one, measure the performance, then remove that index and try the other. Typically you want data that is queried together to lie close together in the index on the disk, and the order of the columns in the index makes a difference to the way the index is stored on disk. It's very difficult to guess exactly what combination of indexes will work best, so try a few different possibilities and measure to find out which is best for your data.

    0 讨论(0)
  • 2021-02-12 12:30

    In the example you give, the column order does not matter.

    It would matter if you order on a column; an index on (col1,col2) can be used for ORDER BY col1, col2 but not for ORDER BY col2, col1.

    For WHERE clauses, an index on (col1, col2) works for WHERE col1 = 1 AND col2 = 1. It also works for WHERE col1 = 1. But it can't help with WHERE col2 = 1.

    0 讨论(0)
提交回复
热议问题