【问题标题】:How to handle large data query on mysql with better performances如何在 mysql 上以更好的性能处理大数据查询
【发布时间】:2016-11-26 05:11:33
【问题描述】:

目前,我正在尝试通过 MySQL 工作台从 2 个不同数据库架构中的三个不同表运行特定查询,但我无法实现。

我目前在数据库架构中有一个 trackcompleted 表,在不同的服务器中有一个 location 表数据库。

  1. track 表收集待办事项的开始和结束。

  2. completed 表保存结果

  3. location 数据库用于获取待办事项的创建和完成位置。

跟踪

+----+----------+---------------+---------------------+
| tid | user_id | function_name | track_time          |
+-----+---------+---------------+---------------------+
|  1  | des     | create        | 2015-02-29 1 pm |
|  2  | des     | complete      | 2015-02-29 2 pm |
|  3  | greg    | create        | 2015-02-29 3 pm |
|  4  | greg    | complete      | 2015-02-29 4 pm |
+-----+---------+---------------+---------------------+

完成

+-----+------+---------------------+
| tid | uid  |  insert_time        |
+-----+------+---------------------+
|  1  | des  | 2015-02-29 1 pm |
|  2  | des  | 2015-02-29 2 pm |
|  3  | greg | 2015-02-29 3 pm |
|  4  | greg | 2015-02-29 4 pm |
+-----+------+---------------------+

位置

+----+----------+---------------+----------+
| tid | user_id | action        | location |
+-----+---------+---------------+----------+
|  1  | des     | create        | subways  |
|  2  | des     | complete      | home     |
|  3  | greg    | create        | home     |
|  4  | greg    | complete      | market   |
+-----+---------+---------------+----------+

我能够从以下同一数据库架构中的两个表中获取连接结果:

查询结果

+-----+---------+---------------+-----------------+-----+------+---------------+
| tid | user_id | function_name | track_time      | tid | uid  | insert_time   |
+-----+---------+---------------+-----------------+-----+------+---------------+
|  2  | des     | complete      | 2015-02-29 1 pm | 2   | des  | 15-02-29 2 pm |
|  4  | greg    | complete      | 2015-02-29 3 pm | 4   | greg | 15-02-29 4 pm |
+-----+---------+---------------+-----------------+-----+------+---------------+


select * from 
svr1.tracking t, 
svr1.completed c 
where 
t.user_id = c.uid 
and t.tid = c.tid 
and t.function_name = 'create' 
and t.track_time > '2015-02-29 00:00:00' 
and t.track_time < '2015-02-29 23:59:59'

但是,我的查询中还需要 location 信息,但是一天的位置表有 1.5 亿条记录,并且由于我的 mac 的 16gb ram 用完,即使它们被索引,它也需要很长时间才能运行。

我要求输出有

user_id,
create tid, 
function_name, 
track_time, 
create location, 
complete tid, 
function_name,
track_time,
location

这会给我如下输出:

des, 1, create, 2015-02-29 1 pm, subways, 2,complete, 2015-02-29 2 pm, home

这是合并的曲目信息和结果

用户信息

在位置 ID 是用户设备的哈希 MAC 地址,我想找出每个特定用户的跟踪时间和位置记录时间。

我想知道要编写什么最好的脚本来实现它,因为在工作台上运行对我不起作用。

感谢您的阅读,非常感谢任何 cmets!

【问题讨论】:

  • 你有索引吗?
  • @Sane 是的,它们是索引
  • 能否请您从您的表中发布 show create table xxx 以查看是否确实存在正确的复合索引。
  • (1) 表格设计没有任何意义。 (2) 请提供您尝试运行的查询(包括location
  • @DuduMarkovitz 我没有设法获得位置结果查询,不知道该怎么做。我可以通过 track 和 result 得到结果...

标签: mysql sql database mysql-workbench


【解决方案1】:

我最终通过另一个问题帖子解决了这些问题: How to get latest results by date when selecting from two table?

通过将结果导出到另一个服务器数据库,我可以通过下面的代码得到我需要的东西。

SELECT
r1.uid,r1.tid,r1.insert_time,l1.location,l1.time,timeDiff
FROM
(
select 
r.uid,
r.tid,
l.time,
l.location,
r.androidId,
r.insert_time,
min(abs(TIME_TO_SEC(TIMEDIFF(insert_time,l.time)))) as timeDiff
from
locationDB.track_result_submitted_mac r inner join
locationDB.location_archival_2015_09 l

on androidId = id
where

l.time > '2015-09-23 00:00:00' and l.time < '2015-09-30 23:59:59'  and
r.insert_time > '2015-09-23 00:00:00' and r.insert_time < '2015-09-30 23:59:59'

and abs(TIME_TO_SEC(TIMEDIFF(insert_time,l.time))) < 18000 
group by uid,tid

) as t,

indoorloc.track_result_submitted_mac r1 inner join
indoorloc.location_archival_2015_09 l1
on r1.androidId = l1.id

WHERE 
(abs(TIME_TO_SEC(TIMEDIFF(r1.insert_time,l1.time)))) = t.tim and
r1.uid = t.uid and
r1.tid = t.tid

group by uid, tid

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2017-11-12
    • 1970-01-01
    • 2020-09-30
    • 2012-10-19
    • 2017-03-07
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    相关资源
    最近更新 更多