Please answer these questions before submitting your issue. Thanks!
1.What did you do?
create table stake(userId int,day int);
insert into stake VALUES(1,20180602);
insert into stake VALUES(1,20180602);
insert into stake VALUES(2,20180602);
insert into stake VALUES(1,20180603);
insert into stake VALUES(3,20180603);
insert into stake VALUES(3,20180603);
select
t.day,
(select count(distinct userId) from stake where day=t.day) stakes,
(select count(distinct userId) from stake where day=t.day
and userId in(select userId from stake where day=DATE_FORMAT(ADDDATE(str_to_date(t.day,'%Y%m%d'),INTERVAL +1 day),'%Y%m%d'))) stakes2
from(
select
distinct day
from stake
) t;
day | stakes | stakes2
-- | -- | --
20180602 | 2 | 1
20180603 | 2 | 0
day | stakes | stakes2
-- | -- | --
20180602 | 2 | 2
20180603 | 2 | 0
@yowner
Thank you for your report, we'll investigate this issue.
@yowner
I tried this in MySQL, the result is
+----------+--------+---------+
| day | stakes | stakes2 |
+----------+--------+---------+
| 20180602 | 2 | 1 |
| 20180603 | 2 | 0 |
+----------+--------+---------+
yes, but running in TiDB is:
+----------+--------+---------+
| day | stakes | stakes2 |
+----------+--------+---------+
| 20180602 | 2 | 2 |
| 20180603 | 2 | 0 |
+----------+--------+---------+
@yowner I edited the issue to swap the expected and actual.
@yowner
Please try the latest master, it's fixed in master.
create INDEX idx_stake_day_userId on stake(day,userId);
stake.zip
@yowner Thanks for your reporting.
There're some optimization we can do to accelerate this sql.
It may take some time to do this.
Sorry for your inconvenience.
We need:
add_date function down to tikv side. or change the DataSource structure in TiDB.For your side. You'd better change the day column from int type to date type.
@winoros The day column is int type that save less disk space. Int type is 4 Bytes, but date is 8 Bytes.
I am going to close this issue as stale. Please feel free to re-open if you have additional questions. Thanks!