美文网首页databases
组合索引区间表达式展开

组合索引区间表达式展开

作者: 沐一同学 | 来源:发表于2020-05-08 18:42 被阅读0次

c1,c2,c3 组合索引 区间表达式展开

2. 展开半闭合区间

(s1,s2,s3) -> (+oo, +oo, +oo)

    1, 2, 3         - (c1 = s1 and c2 = s2 and c3 >= s3)          P3

    1, 2, 4         - /

    1, 3, 1         - (c1 = s1 and c2 > s2) /                               P2

    1, 3, 4          - (c1 > s1)                                                     P1

    2, 1, 3

    2, 3, 5

    4, 3, 10         - /

分析:[(s1,s2,s3), +oo) SQL语句 expr: (c1,c2,c3) >= (s1, s2, s3)

展开成普通表达式,可以集合分为3个子集

子集P1, U(c1 > s1)

子集P2, U(c1 = s1 and c2 > s2)

子集P3, U(c1 = s1 and c2 = s2 and c3 >= s3)

展开成条件表达式: (c1 > s1) OR (c1 = s1 and c2 > s2) OR (c1 = s1 and c2 = s2 and c3 >= s3)

2. 展开区间

(s1,s2,s3) -> (h1,h2,h3) 范围如何展开?

    1, 2, 3         - c1 = s2 and c2 = s2 and c3 >= s3              P3-

    1, 2, 4         - /

    1, 3, 1         - c1 = s1 and c2 > s2                                   P2-

    1, 3, 4        - /

    2, 1, 3        - c1 > s1 and c1 < h1                                   P1

    2, 3, 5

    3, 3, 3        - /

    4, 1, 1        - c1 = h1 and c2 < h2                                   P2+

    4, 1, 2

    4, 2, 4        - /

    4, 3, 8        - c1 = h1 and c2 = h2 and c3 <= 10             P3+

    4, 3, 9

    4, 3, 10       - /

分析: (s1,s2,s3) -> (h1,h2,h3) SQL语句 expr: (c1, c2, c3) >= (s1, s2, s3) and (c1, c2, c3) <= (h1, h2, h3)

展开成普通表达式,可以划分为5个集合

子集P1, U(c1 > s1 and c1 < h1)

子集P2-, U(c1 = s1 and c2 > s2)

子集P2+, U(c1 = h1 and c2 < h2)

子集P3-, U(c1 = s2 and c2 = s2 and c3 >= s3)

子集P3+, U(c1 = h1 and c2 = h2 and c3 <= h3)

展开后: P1 + P2- + P2+ + P3- + P3+

这样可以吗?

当s1=h1时,考虑一下P2-和P2+的集合,会发现,包含了一部分非法数据记录;

需要取P2-和P2+的交集才可以。

P3-和P3+存在同样的问题。

Tip:

P2- 和 P2+ 当s1=h1时,取P2-和P2+的交集

P3- 和 P3_类似,也要考虑s1=s1 并且 s2=h2时,取P3- 和 P3+的交集

解1:s1 != h1

expand:(c1 > s1 and c1 < h1) OR (c1 = s1 and c2 > s2) OR (c1 = h1 and c2 < h2) OR (c1 = s2 and c2 = s2 and c3 >= s3) OR (c1 = h1 and c2 = h2 and c3 <= h3)

解2:s1 == h1 && s2 != h2

expand:(c1 > s1 and c1 < h1) OR (c1 = s1 and c2 > s2 AND c1 = h1 and c2 < h2) OR (c1 = s2 and c2 = s2 and c3 >= s3) OR (c1 = h1 and c2 = h2 and c3 <= h3)

P1集合位空

等价于:c1 = s1 and c2 > s2 AND c1 = h1 and c2 < h2) OR (c1 = s2 and c2 = s2 and c3 >= s3) OR (c1 = h1 and c2 = h2 and c3 <= h3)

等价于:c1 = s1 and c2 > s2 AND c2 < h2) OR (c1 = s2 and c2 = s2 and c3 >= s3) OR (c1 = h1 and c2 = h2 and c3 <= h3)

解3:s1 == h1 && s2 == h2 && s3 != s3

expand: (c1 > s1 and c1 < h1) OR (c1 = s1 and c2 > s2 AND c1 = h1 and c2 < h2) OR (c1 = s2 and c2 = s2 and c3 >= s3 AND c1 = h1 and c2 = h2 and c3 <= h3)

P1集合位空, P2- 和 P2+ 交集也为空

等价于:(c1 = s2 and c2 = s2 and c3 >= s3 AND c1 = h1 and c2 = h2 and c3 <= h3)

等价于:(c1 = s2 and c2 = s2 and c3 >= s3 and c3 <= h3)

解4:s1 == h1 && s2 == h2 && s3 == h3 同解3

相关文章

  • 组合索引区间表达式展开

    c1,c2,c3 组合索引 区间表达式展开 2. 展开半闭合区间 (s1,s2,s3) -> (+oo, +oo,...

  • ThinkPHP查询

    查询方式 表达式查询 快捷查询 区间查询 组合查询 统计查询 动态查询 SQL查询 ThikPHP支持原生SQL查...

  • Kotlin之for与区间表达式

    区间表达式infor 区间表达式 运算符:..,是闭合的区间,即:1..10,包含1与10; 关键字:util,是...

  • mysql 索引

    索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,...

  • 查询优化

    MySql组合索引应该注意的细节 1. MySql 组合索引 MySQL组合索引具有...

  • repeatable read下的 gap locks

    1、表结构 2、验证1 (主键索引 等值查询) 会话1 会话2 3、验证2 会话1 会话2(主键索引 区间区间)...

  • 索引

    分类 mysql的索引分为单列索引(主键索引,唯一索引,普通索引)和组合索引.单列索引:一个索引只包含一个列组合索...

  • MySQL索引进阶

    组合索引 常见误区: 表上建多个索引,就是组合索引(不是);组合索引创建时应该把整数字段放前面(要根据查询情况而定...

  • 普通索引和组合索引

    索引分单列索引和组合索引: 单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引; ...

  • 索引

    单列索引(主键索引、唯一索引、普通索引) 一个索引只包含一个列,一个表中可以有多个单列索引 组合索引 一个组合索引...

网友评论

    本文标题:组合索引区间表达式展开

    本文链接:https://www.haomeiwen.com/subject/vydqnhtx.html