[英]How do I speed up a sql query searching from two tables?
这是一个示例,因此我有表user
和table city
,它们是这样的:
user
列是(user_id,city_id,时间戳) [user_id和city_id是唯一的]
city
列是(city_name,city_id) [city_id是唯一的]
我想从特定城市获取给定日期的用户数,因此基本上我这样做是这样的:
select city_id, city_name,
(select count(user.user_id)
from user, city
where DATE_FORMAT(user.timestamp, '%Y-%m-%d') = '2017-03-07'
and user.city_id = ct.city_id) as user_count
from city ct
where (city_id = 20 or city_id = 30)
结果:
city_id, city_name, user_count
20 New York 100
30 LA 200
然后我意识到这比直接搜索要慢得多
select count(user.user_id)
from user, city
where DATE_FORMAT(user.timestamp, '%Y-%m-%d') = '2017-03-07'
and user.city_id = 20
为什么是这样? 原始搜索中的ct.city_id
是否已设置为20或30? 如何优化搜索并以所需的表格格式获取结果?
您可以改进查询,避免子选择,而使用内部联接和分组依据
select city_id, city_name, count(user.user_id)
from user
inner join city on user.city_id = city.city_id
where DATE_FORMAT(user.timestamp, '%Y-%m-%d') = '2017-03-07'
and city_id in (city_id = 20 or city_id = 30)
group by city_id, city_name
我假设MySQL选择在第一个查询中将派生表具体化为内部临时表,而不在第二个查询中做出选择。
对于派生表(FROM子句中的子查询),优化器具有以下选择:
- 将派生表合并到外部查询块中
- 将派生表具体化为内部临时表
尝试这个:
select city_id, city_name, count(user.user_id) as user_count
from city ct
inner join user on user.city_id = ct.city_id
where (ct.city_id = 20 or ct.city_id = 30)
AND DATE_FORMAT(user.timestamp, '%Y-%m-%d') = '2017-03-07'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.