V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
imherer
V2EX  ›  PostgreSQL

表索引创建问题

  •  
  •   imherer · 2019-07-04 11:13:31 +08:00 · 2539 次点击
    这是一个创建于 1729 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

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

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

    4 条回复    2019-07-04 11:24:17 +08:00
    dog82
        1
    dog82  
       2019-07-04 11:21:04 +08:00
    (uid type),(time)建索引就行
    单独对 type 建索引没意义,因为选择性不高
    dog82
        2
    dog82  
       2019-07-04 11:22:59 +08:00
    我想不出对 amount 建索引的意义,避免回表?
    imherer
        3
    imherer  
    OP
       2019-07-04 11:23:17 +08:00
    @dog82 感谢。 那还有 amount 呢?
    imherer
        4
    imherer  
    OP
       2019-07-04 11:24:17 +08:00
    @dog82 经常作为条件的字段建索引不是能提高查询速度嘛😂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3314 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:47 · PVG 21:47 · LAX 06:47 · JFK 09:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.