0

我在同一个数据库上有两个表,Device并且EventData;两个表都有列accountIDdeviceID,它们也是主键。

Device表中有一个名为 的列linkDescription

我需要在 table 中复制一些行,在 table中EventDataaccountID在column 中有一些文本。deviceIDDevicelinkDescription

例子:

设备表

accountID  DeviceID linkDescription
12345       5800      444
12345       5700      445
12345       5500      null <--literally null
12388       4400      555
12388       4450      555

事件数据表

accountID  DeviceID timestamp
12345       5800      123335544
12345       5700      123335544
12345       5500      123335544
12388       4400      123335544
12388       4450      123335544
12345       5800      123335548
12345       5700      123335549
12345       5500      123335549
12388       4400      123335545
12388       4450      123335546

现在我需要复制一些行并使用from表EventData更改;所以现在有以下数据:accountIDlinkDescriptionDeviceEventData

accountID  DeviceID timestamp
12345       5800      123335544
12345       5700      123335544
12345       5500      123335544
12388       4400      123335544
12388       4450      123335544
12345       5800      123335548
12345       5700      123335549
12345       5500      123335549
12388       4400      123335545
12388       4450      123335546
444         5800      123335544 <-duplicated data with new accountID from here
445         5700      123335544
555         4400      123335544
555         4450      123335544
444         5800      123335548
445         5700      123335549
555         4400      123335545
555         4450      123335546

所以现在我正在测试以下查询,它将成为更大的一部分INSERT INTO

explain 
select * 
from EventData
where 
    EventData.accountID in (
        select accountID 
        from Device 
        where Device.linkDescription > '0')
    and EventData.deviceID in (
        select deviceID 
        from Device 
        where Device.linkDescription> '0')
    and timestamp > (unix_timestamp(now()-interval 20 minute));

但是是两个慢,EXPLAIN命令显示:

ID   select_type           table      type            posible_keys    key   key_len  ref     rows     Extra
1    PRIMARY              EventData   ALL             null            null   null    null  47555718   Using where
3    DEPENDENT SUBQUERY    Device     ALL             null            null   null    null    8043     Using where
2    DEPENDENT SUBQUERY    Device     index_subquery    PRIMARY     PRIMARY    34    func     3       Using where

因此,至少据我所知,正在检查整个表格,这就是为什么这么慢。

我怎样才能更快地做我想做的事?

4

3 回答 3

0

I am not sure from your question what you are trying to do, but you should use JOIN to speed things up. Your SELECT could be written more like this:

SELECT EventData.accountID from
EventData
RIGHT JOIN Device
ON
(
    (
        Device.accountID = EventData.accountID
        OR Device.deviceID = Event.deviceID
    )
    AND Device.linkDescription> '0'
    AND timestamp > (unix_timestamp(now()-interval 20 minute))
);

You can probably work out how the INSERT would work from there.

于 2013-05-07T18:38:56.260 回答
0

考虑使用 JOIN 代替where条件:

select ed.*
from
    EventData as ed
    inner join Device as d1 on ed.accountId = d1.accountId
    inner join Device as d2 on ed.deviceId = d2.deviceId
where
    d1.linkDescription > '0'
    and d2.linkDescription > '0'
    and  ed.timestamp > (unix_timestamp(now()-interval 20 minute));

我复制Device表格只是为了更正您的查询。如果必须同时满足两个条件,则只需使用该Device表一次:

select ed.*
from
    EventData as ed
    inner join Device as d on ed.accountId = d.accountId and ed.deviceId = d.deviceId
where
    d.linkDescription > '0'
    and d.linkDescription > '0'
    and  ed.timestamp > (unix_timestamp(now()-interval 20 minute));

希望这可以帮助

于 2013-05-07T18:41:28.383 回答
0

不一定是您的子查询很慢,它的主表是 4700 万行:

和时间戳 > (unix_timestamp(now()-interval 20 分钟));

在时间戳上添加索引。那应该解决它。我会指出,在现代版本的 mysql 中,子查询和连接在性能上没有真正的区别。然而,连接更干净,更容易理解。

于 2013-05-07T18:43:49.947 回答