加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

POSTGRESQL / mysql 索引区别(where)

发布时间:2020-12-13 17:08:26 所属栏目:百科 来源:网络整理
导读:在如下的表中: create table aaa ( a int ,b int ,c int ,d int ); create index indexabc on aaa(a,b,c); 对比是否使用到了索引: 发现postgresql的索引更加智能一些. 它可以使用索引集合的任意子集 而 mysql不行. 版本: mysql 5.6.22 postgresql: 9.5 官方

在如下的表中:

create table aaa ( a int,b int,c int,d int);

create index indexabc on aaa(a,b,c);

对比是否使用到了索引:

发现postgresql的索引更加智能一些.
它可以使用索引集合的任意子集 而 mysql不行.
版本:
mysql 5.6.22 postgresql: 9.5
官方文档参考:
mysql多列索引

If an index exists on (col1,col2,col3),only the first two queries use the index. The third and fourth queries do involve indexed columns,but (col2) and (col2,col3) are not leftmost prefixes of (col1,col3).

postgresql多列索引

A multicolumn B-tree index can be used with query conditions that involve any subset of the index’s columns,but the index is most efficient when there are constraints on the leading (leftmost) columns. The exact rule is that equality constraints on leading columns,plus any inequality constraints on the first column that does not have an equality constraint,will be used to limit the portion of the index that is scanned. Constraints on columns to the right of these columns are checked in the index,so they save visits to the table proper,but they do not reduce the portion of the index that has to be scanned. For example,given an index on (a,c) and a query condition WHERE a = 5 AND b >= 42 AND c < 77,the index would have to be scanned from the first entry with a = 5 and b = 42 up through the last entry with a = 5. Index entries with c >= 77 would be skipped,but they’d still have to be scanned through. This index could in principle be used for queries that have constraints on b and/or c with no constraint on a — but the entire index would have to be scanned,so in most cases the planner would prefer a sequential table scan over using the index.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读