SELECT COUNT(1) FROM (
SELECT m.* FROM (
SELECT id, send_user_id, receive_user_id, send_time, receive_time, content, customer_id, system_id, receive_user_name, send_user_name
FROM tbl_message WHERE send_time >= 1651334400 AND send_time < 1653840000 ORDER BY send_time DESC, id DESC
) AS m GROUP BY m.system_id, m.customer_id ORDER BY m.send_time DESC, m.id DESC
) AS t
WHERE 1=1
AND EXISTS(SELECT 1 FROM tbl_user AS u WHERE u.type = 2 AND u.id = t.receive_user_id)
SELECT COUNT(1) FROM (
SELECT m.* FROM (
SELECT id, send_user_id, receive_user_id, send_time, receive_time, content, customer_id, system_id, receive_user_name, send_user_name
FROM tbl_message WHERE send_time >= 1651334400 AND send_time < 1653840000 ORDER BY send_time DESC, id DESC
) AS m ORDER BY m.send_time DESC, m.id DESC
) AS t
inner join
tbl_user AS u on u.id = t.receive_user_id and u.type = 2
– 1、准备测试表
– 保证有多个region 使用 SHARD_ROW_ID_BITS
drop table employees;
CREATE TABLE employees (
id bigint not null,
fname VARCHAR(30),
store_id INT NOT NULL,
type int not null default 0
)SHARD_ROW_ID_BITS=5 PRE_SPLIT_REGIONS=4;
– 2、插入基础数据
insert into employees(id,fname,store_id,type) values
(1,‘z1’,1,0),
(2,‘z2’,1,0),
(3,‘z3’,1,1);
– 3、执行 多次,让数据在10万左右
insert into employees(id,fname,store_id,type)
select floor(rand()*10000000),concat(‘z’,floor(rand()*10)),floor(rand()*100),floor(rand()*10) from employees;
– 4、确认有多个region
SHOW TABLE TPCH_001.employees REGIONS;
– 5、验证数据
SET sql_mode=(SELECT REPLACE(@@sql_mode,‘ONLY_FULL_GROUP_BY’,’’));
select type,fname from employees where type=2 GROUP BY type;
– 上面的sql和下面的sql 原理一样
– 此sql每次执行的 fname都不一样
select type,fname from employees where type=2 GROUP BY type ;