clickhouse数据库, 两个表Join, 右表大概1000行记录, 左表7亿行(单个分区), 左表检索时索引没有生效, 全表扫描.
-- vgot.mme definition
CREATE TABLE vgot.mme
(
`starttime` UInt32,
`endtime` UInt32,
`mme_groupid` UInt16,
`mme_code` UInt8,
`mme_s1apid` UInt32,
`msisdn` String,
`eci` UInt32,
`tmsi` String,
INDEX mmeuserid (mme_groupid, mme_code, mme_s1apid, starttime, endtime) TYPE minmax GRANULARITY 5
)
ENGINE = MergeTree
PARTITION BY toYYYYMMDD(FROM_UNIXTIME(starttime))
TTL FROM_UNIXTIME(starttime) + INTERVAL 1 MONTH
ORDER BY msisdn
SETTINGS index_granularity = 8192;
-- vgot.MsgIndex definition
CREATE TABLE vgot.MsgIndex
(
`fileid` UInt64,
`mme_groupid` UInt16,
`mme_code` UInt8,
`mme_s1apid` UInt32,
`starttime` UInt32,
`endtime` UInt32,
`file_name` String
)
ENGINE = MergeTree
PARTITION BY toYYYYMMDD(FROM_UNIXTIME(starttime))
TTL FROM_UNIXTIME(starttime) + INTERVAL 1 MONTH
PRIMARY KEY (mme_groupid, mme_code, mme_s1apid)
ORDER BY (mme_groupid, mme_code, mme_s1apid, starttime)
SETTINGS index_granularity = 8192;
-- 疑问, MsgIndex表使用下面的查询语句, 索引没有生效, 查询时遍历了全部的行;
select t.mme_groupid , t.mme_code , t.mme_s1apid, t.fileid, m.msisdn
from vgot.MsgIndex t,
(select mme_groupid , mme_code , mme_s1apid, starttime, endtime, msisdn from vgot.mme where msisdn in ('93AF26FD6A437EA989F11D4C9EB679BF') and starttime >= 1661788800 AND endtime <=1661875199 ) m
where m.mme_groupid = t.mme_groupid and m.mme_code = t.mme_code and m.mme_s1apid = t.mme_s1apid
and t.starttime >= m.starttime - 120 and t.endtime <= m.endtime + 120
扫描了MsgIndex表全部记录.
不关联右表, 单独查询左表, 索引有效.
MsgIndex表的starttime和endtime的where条件不是常量,是和另外一张表中的时间字段做关联比较,感觉这个条件不会使用分区键。
可以试着增加starttime的查询条件。
您好,我感觉可能是where条件的问题,你把后面的大于小于的条件去掉试试。。只有这样的:
where m.mme_groupid = t.mme_groupid and m.mme_code = t.mme_code and m.mme_s1apid = t.mme_s1apid
感谢大家,不过都没有效果,是不是ck只要是两个表join,索引文件都无效。