Graves
V2EX  ›  Java

shardingjdbc 根据 id 查询扫了所有分表

  •  
  •   Graves · Feb 23, 2022 · 3883 views
    This topic created in 1715 days ago, the information mentioned may be changed or developed.
    比如说有个 order 表拿 userid 进行分表分了 128 个表,然后代码层面去关联查询,在知道 order_id 得情况下,select xx from order where id = {order_id} ,框架是 springboot 集成 shardingjdbc 去查询,会扫描 128 个表,即使中间在某张表找到了结果也没有返回,每次都会查询完整 128 个表,这块大家有遇到过吗,不知道是不是我 shardingjdbc 配置的问题,如何做到根据 id 查询到结果就 return 呢?
    14 replies  •  2022-03-15 17:39:03 +08:00
    brezp
    brezp
    Feb 23, 2022
    1
    等一个回复
    ChovyChu
    ChovyChu
    Feb 23, 2022
    2
    没用过 shardingjdbc ,但是从你的描述看,我怎么觉得挺正常的,shardingjdbc 他也不知道是不是只有一条符合条件的数据,所以查完所有表不是正常的吗?可以考虑 limit 1 试试?只是瞎扯扯
    xiao109
    xiao109
    Feb 23, 2022❤️1
    3
    你是拿 userid 分的表,但是查是根据 order_id 。当然要把所有的表都走一遍
    brezp
    brezp
    Feb 23, 2022
    4
    查询条件带上你的分片字段, 才能指定查某一个分片
    james2013
    james2013
    Feb 23, 2022
    5
    userid 分片字段必须存在并且放在第 1 个位置,才能只查特定的 1 个表
    select xx from order where userid={userId} and id = {order_id}
    issakchill
    issakchill
    Feb 23, 2022
    6
    应该是无解的,查询非分片字段,就是要所有表走一遍
    cheng6563
    cheng6563
    Feb 23, 2022
    7
    limit 1 应该就查到结果就立即 return 了
    mcfog
    mcfog
    Feb 23, 2022
    8
    假设支持你说的功能,那么这个查询就必须串行进行,单次查询的时延 99 线放大 128 倍,平均值放大 64 倍
    agzou
    agzou
    Feb 24, 2022
    9
    分区 key 是 userid ,你用 order_id 去查,肯定是全表扫,并没有命中分区
    mango88
    mango88
    Feb 24, 2022
    10
    缺少分片 key ,当然是扫全表了...
    Chinsung
    Chinsung
    Feb 24, 2022
    11
    不带分片 key ,当然扫全表了。
    要么先查出来分片 key ,用分片 key 挨个查。
    要么再搞个数仓,去数仓查
    要么异构索引表
    Graves
    Graves
    OP
    Feb 25, 2022
    12
    @ChovyChu @cheng6563 测试了一下,加了 limit1 没有效果
    @xiao109 @brezp @james2013 @issakchill @agzou @mango88 @Chinsung 确实加了分片的 id 就会扫其中一个表的数据,order_id 用 snowflake 做了分布式 id ,在所有分表中是唯一的,扫全表我能理解,我感觉 sharding 是不是可以加个可配置项,在扫的过程中找到了就 return 不再继续去扫了,还是说我分片的姿势不对,应该拿 order_id 去做分片比较合理。
    @mcfog 不太理解你的意思,我需求就是 order_id 是唯一的,扫表找到了就返回结果,然后不要再继续剩下的表。
    agzou
    agzou
    Feb 28, 2022
    13
    @Graves #12 看查询场景吧,一般分片的 key 都是必带的查询条件,要不所有表都会查一次。
    slomo
    slomo
    Mar 15, 2022
    14
    如果有资源那就再冗余一份数据用 orderId 分片 = =
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2230 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 15:51 · PVG 23:51 · LAX 07:51 · JFK 10:51
    ♥ Do have faith in what you're doing.