且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到多列索引的最优顺序

更新时间:2023-01-29 17:22:01

索引中列的顺序是不是无关。有一种方法。

The order of columns in an index is not irrelevant. There is a method.


  • 首先,相等条款中涉及的一个或多个列合并与AND。

  • First, one or more columns that are involved in equality terms combined with AND.

WHERE a = 1 AND b = 2


  • 其次,参与范围术语的单个列。要么是这样,要么是一个或多个列参与排序。

  • Second, a single column involved in a range term. Either that, or else one or more columns involved in sorting.

    WHERE a = 1 AND b = 2 AND c > 3
    

    WHERE a = 1 AND b = 2
    ORDER BY c, d
    


  • 第三,select-list中引用的列,但未通过搜索或排序引用。

  • Third, columns referenced in the select-list, but which aren't referenced by searching or sorting.

    SELECT x, y, z
    . . .
    WHERE a = 1 AND b = 2 AND c > 3
    


  • 这将导致(a,b,c,x,y,z)

    我在演讲中更详细地解释了这一点如何设计索引,真的

    I explain this in more detail in my presentation How to Design Indexes, Really.