項目中數據量比較大需要每個月分一次表,按照時間分表;
分表查詢邏輯:先按條件查詢出要查詢的表,再去多張表里面查詢符合條件的數據
MySQL查表語句:
// 這種方式不支持條件查詢 show tables; // 這種方式可以根據table_name字段匹配符合條件的表 select * from information_schema.tables where table_name like 'th%';
分出來的表一定是表結構相同的數據表,多張相同數據結構的表查詢語句:
// union all 關鍵字查詢 select * from thermal_oil_pipeline_202009060553 p union all select * from thermal_oil_pipeline_202009060553 p union all select * from thermal_oil_pipeline_202009060553 p
分頁查詢會用到符合條件數據總數量這個數據,分表查詢符合條件數據總量sql如下:
select sum(count) from ( select count(p.id) as count from thermal_oil_pipeline as p where equipmentNo='FIC4' and createTime >= '2020-09-02 14:30:44' and createTime <= '2020-09-06 14:30:44' union all select count(p.id) as count from thermal_oil_pipeline_202009060553 as p where equipmentNo='FIC4' and createTime >= '2020-09-02 14:30:44' and createTime <= '2020-09-06 14:30:44' ) as total;