Code Monkey home page Code Monkey logo

Comments (1)

xbox1994 avatar xbox1994 commented on July 22, 2024

谢谢,该原则我没有解释清楚,会立即更正。

最左前缀匹配原则:在mysql建立联合索引时会遵循最左前缀匹配的原则,即最左优先,在检索数据时从联合索引的最左边开始匹配。mysql会一直向右匹配直到遇到范围查询(>/</between/like)就停止匹配,比如a=3 and b=4 and c>5 and d=6,如果建立(a,b,c,d)联合索引,d就用不到索引,如果建立(a,b,d,c)则都能用到索引。

这里的 (name,age) 索引使用的不是最左前缀原则,而是索引下推

对于这条sql语句,使用到了最左前缀匹配原则,从name开始匹配,匹配到了name和age,才能使用到联合索引。我做过实验,改sql并没有使用索引下推。
索引下推的条件是使用Innodb的二级索引有并试图访问全表行的操作如范围查询。

最左前缀原则用于 name like 'A%'这样的查询,会使用到索引,加速查询

最左前缀原则不只用于like查询,具体规则在上面提到

from java-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.