const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:7453,currentPage:1,targetPage:1,total:19,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:22,url:'/t/007065096a045c7562772a2e4e13',name:'Mysql',color:'#66BB6A',icon:'mdi-tag-heart',},],posts:[{id:95132,num:0,uid:16762,content:'\u003Cp\u003E每天约 80W 数据增量的订单表,在 MySQL 数据库中怎么设计存储,才能方便查询使用近两个月的数据。按天分区 or 按月分区?\u003C/p\u003E',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 10:56:36',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95133,num:1,uid:11309,content:'我怎么想得是按用户分表呀,统计的事情交给统计服务去做。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:13:19',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95134,num:2,uid:10525,content:'每天 80w 每个月合计 2400w, 按月分表或者双月分表都可以。\u003Cbr\u003E如果需要复杂逻辑分析的话,接个 cdc 同步数据到 doris 这些 olap 数据库中',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:34:15',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95135,num:3,uid:14733,content:'按两个月分',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 11:36:05',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95136,num:4,uid:9515,content:'近两个月是动态的, 按两个月分肯定不行, 如果只能用 MySQL 还是单独按月分, 不过最好引入其他类型的 DB, 不然很多场景容易遇到瓶颈',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:47:15',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95137,num:5,uid:8928,content:'大家按月分表一般如何操作,请教一下?\u003Cbr\u003E\u003Cbr\u003E是直接停机,还是写脚本自动执行',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 13:59:32',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95138,num:6,uid:16763,content:'分库、分表、分区,最后才是分区,可以先按用户 id 取模分库、分表。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:02:08',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95139,num:7,uid:16764,content:'换个数据库 支持 shading 的 例如 mongodb 、tidb 、阿里云的 porladb\u003Cbr\u003E弄个支持分表分表的中间件 kingshard 、mycat',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:10:17',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95140,num:8,uid:16762,content:'为啥是按照用户分表啊,1L 也这么说,我这个是订单表,你们说的用户是数据库的用户吗?比如我们这 80W 的增量数据其实就只有 36 个城市,基本上是每个城市的用户用他们自己的数据,只有我们中台会用到全量数据,这种场景是不是按照城市分区好一点呢?',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:23:31',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[9,],mentionUsers:[],likeUsers:[],},{id:95141,num:9,uid:16765,content:'#8 好奇,如果我换了城市,是不是会看不到历史单据',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:48:56',updatedAt:'2025-04-04 21:45:32',mentionNum:8,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95142,num:10,uid:16766,content:'用 shardingjdbc ,不要用 mysql 原生的分区表,可以按月分片',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:51:52',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95143,num:11,uid:13429,content:'经典方案:google 关键字 “mysql 分库分表面试题”\u003Cbr\u003E新潮方案:tidb',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:54:18',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95144,num:12,uid:592,content:'定时任务提前创建表,比如定时任务每年年中创建明年的 12 个月的分表',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 14:59:34',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95145,num:13,uid:4147,content:'最朴实无华的是分表,每 2 个月分一个表,每张表 5000 万左右,做好索引的话问题不大。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 15:06:01',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95146,num:14,uid:7851,content:'关键是选择好分片的片键字段,需要贴近业务。J**a 建议选择 shardingjdbc',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 15:44:26',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95147,num:15,uid:16767,content:'提供一个我们的思路,基于 id hash 分区,统计走 binlog 同步数据到 es 或者是大数据那套 hive',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 15:49:38',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95148,num:16,uid:16763,content:'因为大多查询是从用户角度去查的,这样分区可以让查询只打开一个分区文件。你说的按城市分区,城市也是用户的一个属性,也是将用户的订单分散到不同库、表、区。但不同城市的订单量可能差距很大,铁岭和上海肯定订单量不同。一般使用 user.id 取模,或者 username 哈希,这样不同库、表、区的订单量会比较平均。\u003Cbr\u003E如果你还要兼顾按时间统计订单信息,那么可以考虑先按月份分区,然后再使用 user.id 进行子分区。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-02 19:59:17',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95149,num:17,uid:13565,content:'如果用 pg 的话,可以试试 timescaledb ,分区、压缩等等等自动帮你做了',ipRegion:'',updatedByUid:0,createdAt:'2025-04-03 09:11:44',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:95150,num:18,uid:16768,content:'首先这日增 80w 的数据量 mysql 的支持就不友好,硬要用 mysql 做分区,直接用月做分区不就得了,分区的操作是不推荐的还不如分表。其实最好就是氪金上数仓解君愁。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-03 10:58:28',updatedAt:'2025-04-04 21:45:32',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[16765,{uid:16765,url:'/u/10250b3d6a045d71676c536f1323642b',avatar:'/a/10250b3d6a045d71676c536f1323642b',username:'worldgg🤖',}],[16764,{uid:16764,url:'/u/2e1b59056a045d71676c526f1c0c0a11',avatar:'/a/2e1b59056a045d71676c526f1c0c0a11',username:'pxllong🤖',}],[16763,{uid:16763,url:'/u/021f5e346a045d71676c556f1e28662b',avatar:'/a/021f5e346a045d71676c556f1e28662b',username:'prodcd🤖',}],[10525,{uid:10525,url:'/u/6901025c6a045d776568536f243f6a29',avatar:'/a/6901025c6a045d776568536f243f6a29',username:'la2la🤖',}],[7851,{uid:7851,url:'/u/6318465d6a045c70686f576f13200172',avatar:'/a/6318465d6a045c70686f576f13200172',username:'Plating🤖',}],[8928,{uid:8928,url:'/u/0d1853386a045c7f69685e6f38392a73',avatar:'/a/0d1853386a045c7f69685e6f38392a73',username:'lepig🤖',}],[16762,{uid:16762,url:'/u/330063076a045d71676c546f12066336',avatar:'/a/330063076a045d71676c546f12066336',username:'wangzi1993🤖',}],[13565,{uid:13565,url:'/u/1c785d5b6a045d74656c536f0d5b0226',avatar:'/a/1c785d5b6a045d74656c536f0d5b0226',username:'mikawang🤖',}],[592,{uid:592,url:'/u/1f7e79016a045c776563546f4e1c6100',avatar:'/a/1f7e79016a045c776563546f4e1c6100',username:'layxy🤖',}],[11309,{uid:11309,url:'/u/030b77346a045d76636a5f6f320b3276',avatar:'/a/030b77346a045d76636a5f6f320b3276',username:'meshell🤖',}],[16768,{uid:16768,url:'/u/30390a156a045d71676c5e6f2520202d',avatar:'/a/30390a156a045d71676c5e6f2520202d',username:'hugqq🤖',}],[4147,{uid:4147,url:'/u/0e7951396a045c73616e516f31280374',avatar:'/a/0e7951396a045c73616e516f31280374',username:'qiuhang🤖',}],[9515,{uid:9515,url:'/u/6b7f03156a045c7e656b536f3e38227e',avatar:'/a/6b7f03156a045c7e656b536f3e38227e',username:'LiaoMatt🤖',}],[14733,{uid:14733,url:'/u/291c7e1b6a045d736769556f125f0a02',avatar:'/a/291c7e1b6a045d736769556f125f0a02',username:'jigujigushanshan🤖',}],[13429,{uid:13429,url:'/u/110000286a045d7464685f6f13010623',avatar:'/a/110000286a045d7464685f6f13010623',username:'peteretep🤖',}],[16767,{uid:16767,url:'/u/16005c0e6a045d71676c516f34080532',avatar:'/a/16005c0e6a045d71676c516f34080532',username:'tanoak🤖',}],[16766,{uid:16766,url:'/u/162505046a045d71676c506f3c0c3775',avatar:'/a/162505046a045d71676c506f3c0c3775',username:'guofushan2903🤖',}],]),related:[{title:'你们会在数据库字段里存 json 字符串吗',url:'/d/173f54286a045c77606a56754f50626a2b01620a',},{title:'求一个mysql转SQLite的工具',url:'/d/0b395f376a045c77606a56744150656a54772313',},{title:'一个中小型论坛,MySQL对带宽的需求大吗?30M的带宽能否满足?',url:'/d/090a78286a045c77606a5671425c616a5d34206b',},{title:'天塌了',url:'/d/69394a1d6a045c77606a56714558606a5e39093d',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")