我使用的是MariaDB 10.2.21 版本
有多行具有重复的user_id(其中一列(。
我想获得每个user_id的第一次出现,但不仅仅是user_id,还有与user_id第一次出现相关联的所有行。
select
distinct user_id,
from
user_table
where
user_id in (a,b,c,d,e,f,....) and
date = '2020-02-25';
这正确地给了我所有不同的,现在当我尝试添加相关列时
EXPLAIN
select
distinct user_id,
ST_X(location) as lng
from
user_table
where
user_id in (a,b,c,d,e,f,....) and
date = '2020-02-25';
它告诉我会得到200多万行。。。我只有3000个唯一的user_id我只想获取第一个出现的user_id及其关联位置。
由于我的数据库很大,我需要充分利用索引,否则它会崩溃。
我的索引是[user_id,date],其中user_id是第一个索引,date是第二个索引。
所以我的问题是如何通过使用索引来获得唯一的(第一次出现=最上面一行(用户信息,这样我的数据库就不会中断?
所以如果有
user_id location
1 (123.22, 22.33)
1 (111.22, 22.12)
2 (155.33, 41.23)
2 (160.41, 12.31)
我会得到
user_id location
1 (123.22, 22.33)
2 (155.33, 41.23)
如果只有一个位置,请使用聚合:
select user_id, ST_X(min(location)) as lng
from user_table
where user_id in (a,b,c,d,e,f,....) and
date = '2020-02-25'
group by user_id;