MySQL分表后,如何做分頁查詢?
閱讀本文大概需要 2.8 分鐘。
來自:blog.csdn.net/joy_tom/article/details/109857573

DROP table IF EXISTS tb_member1;
create table tb_member1(
id bigint primary key auto_increment ,
name varchar(20),
age tinyint not null default '0'
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
DROP table IF EXISTS tb_member2;
create table tb_member2(
id bigint primary key auto_increment ,
name varchar(20),
age tinyint not null default '0'
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
insert into tb_member1(id,name,sex) select id,name,sex from dd_user where id%2=0;
insert into tb_member2(id,name,sex) select id,name,sex from dd_user where id%2=1;


DROP table IF EXISTS tb_member_all;
create table tb_member_all(
id bigint primary key auto_increment ,
name varchar(20),
age tinyint not null default '0'
)ENGINE=MERGE UNION=(tb_member1,tb_member2) INSERT_METHOD=LAST CHARSET=utf8 AUTO_INCREMENT=1 ;
ERROR 1168 (HY000): Unable to open underlying table which is differently defined
or of non-MyISAM type or doesn't exist
查看上面的分表數(shù)據(jù)庫引擎是不是MyISAM. 查看分表與指標(biāo)的字段定義是否一致。
member1或者member2中創(chuàng)建數(shù)據(jù)member_all表中也會出現(xiàn)同樣的數(shù)據(jù)tb_member_all表就是tb_member1,tb_member2的并集,剛剛實(shí)現(xiàn)到這里,我也沒理解,后來看了一些文檔,了解了一下:
tb_member_all表里面是沒有存儲數(shù)據(jù),它就是一個(gè)外殼,里面的數(shù)據(jù)是tb_member1,tb_member2的并集,數(shù)據(jù)的存儲是放在分表中;


tb_member_all,我們就可以實(shí)現(xiàn)數(shù)據(jù)查詢的分頁;
id%2這是取模處理,分配數(shù)據(jù)進(jìn)入哪個(gè)數(shù)據(jù);tb_member_all表去做分頁查詢實(shí)現(xiàn)



推薦閱讀:
為了隨時(shí)能敲代碼,我用樹莓派手搓一臺 Linux 掌上電腦
互聯(lián)網(wǎng)初中高級大廠面試題(9個(gè)G) 內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)??! 朕已閱


