imherer

表索引创建问题

  •  
  •   imherer · Jul 4, 2019 · 3151 views
    This topic created in 2525 days ago, the information mentioned may be changed or developed.

    PostgreSQL 数据库,一张金币 log 表结构如下:

    id, uid, amount, type, total, time,其中 ID 是自增主键,除了 time 以外其他全是 int 类型

    在日常的查询中 uid, amount, type, time 这些字段都有可能单独作为条件或者是和某一个(些)组合作为条件

    大部分情况是 uid 和其中某一个或几个作为条件

    像上面这样的查询条件,我应该如何建索引呢?

    这 4 个字段联合索引,还是 4 个分别索引呢?

    另外这样的表结构主键 ID 这个字段其实是用不到的,那么在设计表的时候是不是可以不要这个字段?

    4 replies    2019-07-04 11:24:17 +08:00
    dog82
        1
    dog82  
       Jul 4, 2019
    (uid type),(time)建索引就行
    单独对 type 建索引没意义,因为选择性不高
    dog82
        2
    dog82  
       Jul 4, 2019
    我想不出对 amount 建索引的意义,避免回表?
    imherer
        3
    imherer  
    OP
       Jul 4, 2019
    @dog82 感谢。 那还有 amount 呢?
    imherer
        4
    imherer  
    OP
       Jul 4, 2019
    @dog82 经常作为条件的字段建索引不是能提高查询速度嘛😂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3258 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:10 · PVG 22:10 · LAX 07:10 · JFK 10:10
    ♥ Do have faith in what you're doing.