应该怎么理解 rowid 在 sqlite 中的使用 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
youthfire
V2EX    SQLite

应该怎么理解 rowid 在 sqlite 中的使用

  •  
  •   youthfire 2022-07-13 22:44:21 +08:00 1439 次点击
    这是一个创建于 1236 天前的主题,其中的信息可能已经有所发展或是发生改变。
    S.O 上我记得有个贴
    https://stackoverflow.com/questions/35876171/sqlite-rowid-after-deleting-rows
    其中有段话我印象很深
    The data for rowid tables is stored as a B-Tree structure containing one entry for each table row, using the rowid value as the key. This means that retrieving or sorting records by rowid is fast. Searching for a record with a specific rowid, or for all records with rowids within a specified range is around twice as fast as a similar search made by specifying any other PRIMARY KEY or indexed value.
    总结起来:rowid - 也就是 true primary key 性能优势大

    在 Sqlite 官网的 rowid table 中,又是这么阐述的:If the rowid is not aliased by INTEGER PRIMARY KEY then it is not persistent and might change. In particular the VACUUM command will change rowids for tables that do not declare an INTEGER PRIMARY KEY. Therefore, applications should not normally access the rowid directly, but instead use an INTEGER PRIMARY KEY.
    总结起来:不应该直接使用 rowid

    自己平时在操作 sqlite select row 的时候,一直都是直接用 rowid 来选取执行的,一开始是不知道 declare primary key as alias of rowid ,后来是发现没有使用场景用到。官网让不要 direct access 的原因是 rowid 有时候会变,比如 VACUUM 。我没有理解这句,我就是需要 rowid 重新 rebuild ,才会去用 VACUUM 的啊,否则当频繁操作添加,删除动作后,不进行 rebuild ,我再用语句操作 rowid ,可能就不是我真正需要操作的行。所以我才每次在这些动作后,都会进行 VACUUM 。主要是为了逻辑不出错,同时之后读取性能好,空间小。

    但有时候 VACUUM 的时间会很长,可是不操作,此 rowid(true primary key )不是彼 rowid 啊,例如删除了第 6 行,如果不 VACUUM, 我想再操作新的第 6 行,这个时候 rowid 其实是 7 ,不是 6 ,就产生了逻辑问题啊。

    阐述得略混乱,请见谅!
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     880 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 18ms UTC 22:48 PVG 06:48 LAX 14:48 JFK 17:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86