gpt4 book ai didi

mysql - 更快的 sql 查询然后加入

转载 作者:可可西里 更新时间:2023-11-01 07:33:15 25 4
gpt4 key购买 nike

我有一个超过 10,000 行的大表,在不久的将来它将增长到 1,000,000 行,我需要运行一个查询,为每个用户返回每个关键字的时间值。我现在有一个非常慢,因为我使用左连接并且它需要一个子查询/关键字:

SELECT rawdata.user, t1.Facebook_Time, t2.Outlook_Time, t3.Excel_time
FROM
rawdata left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Facebook_Time'
FROM rawdata
WHERE MainWindowTitle LIKE '%Facebook%'
GROUP by user)t1 on rawdata.user = t1.user left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Outlook_Time'
FROM rawdata
WHERE MainWindowTitle LIKE '%Outlook%'
GROUP by user)t2 on rawdata.user = t2.user left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Excel_Time'
FROM rawdata
WHERE MainWindowTitle LIKE '%Excel%'
GROUP by user)t3 on rawdata.user = t3.user

表格如下所示:

WindowTitle | StartTime | EndTime | User
------------|-----------|---------|---------
Form1 | DateTime | DateTime| user1
Form2 | DateTime | DateTime| user2
... | ... | ... | ...
Form_n | DateTime | DateTime| user_n

输出应该是这样的:

User   | Keyword   | SUM(EndTime-StartTime)
-------|-----------|-----------------------
User1 | 'Facebook'| 00:34:12
User1 | 'Outlook' | 00:12:34
User1 | 'Excel' | 00:43:13
User2 | 'Facebook'| 00:34:12
User2 | 'Outlook' | 00:12:34
User2 | 'Excel' | 00:43:13
... | ... | ...
User_n | ... | ...

问题是,在 MySQL 中哪种方式最快?

最佳答案

我认为您的通配符搜索可能是最慢的原因,因为您无法真正利用这些字段上的索引。此外,如果您可以避免进行子查询而直接进行连接,这可能会有所帮助,但通配符搜索要糟糕得多。无论如何,您是否可以将表更改为具有可以具有索引且不需要通配符搜索的 categoryName 或 categoryID?比如“where categoryName = 'Outlook'”

要优化表中的数据,请添加一个类别 ID(理想情况下这将引用一个单独的表,但我们在此示例中使用任意数字):

alter table rawData add column categoryID int not null

alter table rawData add index (categoryID)

然后为现有数据填充 categoryID 字段:

update rawData set categoryID=1 where name like '%Outlook%'
update rawData set categoryID=2 where name like '%Facebook%'
-- etc...

然后更改您的插入内容以遵循相同的规则。

然后像这样进行 SELECT 查询(将通配符更改为 categoryID):

SELECT rawdata.user, t1.Facebook_Time, t2.Outlook_Time, t3.Excel_time
FROM
rawdata left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Facebook_Time'
FROM rawdata
WHERE categoryID = 2
GROUP by user)t1 on rawdata.user = t1.user left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Outlook_Time'
FROM rawdata
WHERE categoryID = 1
GROUP by user)t2 on rawdata.user = t2.user left join
(SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Excel_Time'
FROM rawdata
WHERE categoryID = 3
GROUP by user)t3 on rawdata.user = t3.user

关于mysql - 更快的 sql 查询然后加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com