Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

实时获取数据子表的行数,在主表冗余还是统计子表效率高

每次子表删除一条记录都要返回本子表同主键id的对应的剩余行数。

方案1:在主表冗余一个数量字段num。每次新增子表后update master.num + 1,这样每次要得到值,只需要select num from master
方案2:每次删除一条记录后select count(*) from detail where id = @id

第一种的缺点是要多次执行update,第二种缺点是select count()效率太低
请问,以上2种方式哪种高。数据库是mysql。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...