如何使用;或;mysql 5.7中的编码
我在一个分区中有3亿个数据,现在我想使用2列(索引)上的or条件过滤该记录。 可能吗如何使用;或;mysql 5.7中的编码,mysql,database,query-optimization,mysql-5.7,Mysql,Database,Query Optimization,Mysql 5.7,我在一个分区中有3亿个数据,现在我想使用2列(索引)上的or条件过滤该记录。 可能吗 CREATE TABLE `temp_bulk_tesing` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `request_account` INT(11) NOT NULL DEFAULT '0', `responce_account` INT(11) NOT NULL DEFAULT '0', `creatition_date` DATE
CREATE TABLE `temp_bulk_tesing` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`request_account` INT(11) NOT NULL DEFAULT '0',
`responce_account` INT(11) NOT NULL DEFAULT '0',
`creatition_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `request_account` (`request_account`),
INDEX `responce_account` (`responce_account`),
INDEX `creatition_date` (`creatition_date`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=16371
;
从临时批量测试中选择计数(临时批量测试.id)
内部加入组织帐户
在(temp\u bulk\u tesing.request\u account=org\u account.id
或temp_bulk_tesing.response_account=org_account.id)
和临时批量测试。创建日期>='2019-10-10 22:22:18'
和临时批量测试。创建日期您可以尝试联合所有人:
SELECT COUNT(*)
FROM (
select temp_bulk_tesing.id
from temp_bulk_tesing
join org_account
on temp_bulk_tesing.request_account=org_account.id
and temp_bulk_tesing.creatition_date BETWEEN '2019-10-10 22:22:18'AND'2019-10-13 22:22:18'
union all
select temp_bulk_tesing.id
from temp_bulk_tesing
join org_account
on temp_bulk_tesing.responce_account=org_account.id
and temp_bulk_tesing.creatition_date BETWEEN '2019-10-10 22:22:18'AND'2019-10-13 22:22:18'
) sub
存储过程的实现可能有点棘手,但它是值得的
首先从大表中切掉要匹配的数据
create temporary table tmp_temp_bulk_tesing
select * from temp_bulk_tesing where creatition_date between '2019-10-10 22:22:18'and
'2019-10-13 22:22:18';
添加一个索引,使事情变得更快
alter table tmp_temp_bulk_tesing add INDEX ttbt (id);
然后进行连接
select count(temp_bulk_tesing.id) from tmp_temp_bulk_tesing ttbt
inner join org_account oa
on (ttbt.request_account=oa.id or ttbt.responce_account=oa.id)
Join对数据库来说是一项非常繁重的任务,通过切分正确的数据,您可以在几小时到几秒钟甚至几秒钟内处理它
另一个可能有用的技巧是使用而不是
and temp_bulk_tesing.creatition_date >='2019-10-10 22:22:18'
and temp_bulk_tesing.creatition_date <= '2019-10-13 22:22:18'
然后在查询中使用这些id。int中的索引应该比datetime中的索引快,id的顺序应该使它更容易
这远不如临时表有效,但对您来说可能足够了。
- 如果这就是你所做的全部,那么就进行个人计数
- 如果
org\u account
中的行始终存在,请不要为加入而烦恼
- 使用
上的
了解表格的关联方式;使用WHERE
进行过滤
制定最优指标
不要包括额外的一秒钟
现在每个表中有多少行?我在一张桌子上看到了16371张,但你说其中一张桌子每天大约有3亿张。
select count(temp_bulk_tesing.id) from tmp_temp_bulk_tesing ttbt
inner join org_account oa
on (ttbt.request_account=oa.id or ttbt.responce_account=oa.id)
and temp_bulk_tesing.creatition_date >='2019-10-10 22:22:18'
and temp_bulk_tesing.creatition_date <= '2019-10-13 22:22:18'
select max(id), min(id) from temp_bulk_tesing where creatition_date between '2019-10-10 22:22:18' and '2019-10-13 22:22:18'
SELECT
( select COUNT(*)
from temp_bulk_tesing AS tbt
join org_account AS oa
on tbt.request_account = oa.id -- the only line different
WHERE tbt.creatition_date >= '2019-10-10 22:22:18'
and tbt.creatition_date < '2019-10-10 22:22:18' + INTERVAL 3 DAY
)
+
( select COUNT(*)
from temp_bulk_tesing AS tbt
join org_account AS oa
on tbt.responce_account = oa.id
WHERE tbt.creatition_date >= '2019-10-10 22:22:18'
and tbt.creatition_date < '2019-10-10 22:22:18' + INTERVAL 3 DAY
)
INDEX(request_account, creatition_date),
INDEX(responce_account, creatition_date)