标量子查询需要花费大量时间来执行
Scalar subquery taking a lot of time to execute
我有以下表格:
table1
-
session_id、company_id、session_start_time
table2
-
id, session_id, message_time, message_type, 消息
table3
-
company_id、company_name
table1 存储在公司上完成的会话。每个会话都有很多消息,这些消息存储在表2中。对于特定的 message_type(例如 message_type = 2),需要从消息中提取子字符串。该子字符串是公司名称。现在我需要从表 1 中找到下一个 session_id,其中 company_id 匹配从消息中提取的公司名称和 session_start_time >= message_time。
我正在使用以下查询。
select t1.session_id as session1,
t1.company_id as company1,
@transfer_time := t2.message_time as transfer_time,
@company2 := trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 as t1
inner join table3 as t3 on t1.company_id = t3.company_id
where t1.session_start_time >= @transfer_time
and t3.company_name = @company2
order by t1.session_start_time
limit 1) as session 2
from table1 as t1
inner join table2 as t2 on t1.session_id = t2.session_id
inner join table3 as t3 on t1.company_id = t3.company_id
where t2.message_type = 2
原始查询稍微复杂一些,有更多的标量子查询。此查询的执行时间特别长。我检查了解释函数,标量子查询似乎需要很长时间才能执行。但是,我想不出更好的办法。
这是查询:
select t1.session_id as session1, t1.company_id as company1,
@transfer_time := t2.message_time as transfer_time,
@company2 := trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 t1 inner join
table3 t3
on t1.company_id = t3.company_id
where t1.session_start_time >= @transfer_time and
t3.company_name = @company2
order by t1.session_start_time
limit 1
) s session 2
from table1 t1 inner join
table2 t2
on t1.session_id = t2.session_id inner join
table3 t3
on t1.company_id = t3.company_id
where t2.message_type = 2;
首先,变量的使用不正确。 MySQL 不保证 SELECT
中变量的求值顺序。因此,您需要输入原始定义:
select t1.session_id as session1, t1.company_id as company1,
t2.message_time as transfer_time,
trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 t1 inner join
table3 t3
on t1.company_id = t3.company_id
where t1.session_start_time >= t2.message_time and
t3.company_name = trim(substring(t2.message, 38, locate(' abc', t2.message) - 38))
order by t1.session_start_time
limit 1
) s session 2
from table1 t1 inner join
table2 t2
on t1.session_id = t2.session_id inner join
table3 t3
on t1.company_id = t3.company_id
where t2.message_type = 2;
接下来,我会尝试索引:table3(company_name, company_id)
和 table1(company_id, session_start_time, session_id)
。
我有以下表格:
table1
- session_id、company_id、session_start_timetable2
- id, session_id, message_time, message_type, 消息table3
- company_id、company_name
table1 存储在公司上完成的会话。每个会话都有很多消息,这些消息存储在表2中。对于特定的 message_type(例如 message_type = 2),需要从消息中提取子字符串。该子字符串是公司名称。现在我需要从表 1 中找到下一个 session_id,其中 company_id 匹配从消息中提取的公司名称和 session_start_time >= message_time。
我正在使用以下查询。
select t1.session_id as session1,
t1.company_id as company1,
@transfer_time := t2.message_time as transfer_time,
@company2 := trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 as t1
inner join table3 as t3 on t1.company_id = t3.company_id
where t1.session_start_time >= @transfer_time
and t3.company_name = @company2
order by t1.session_start_time
limit 1) as session 2
from table1 as t1
inner join table2 as t2 on t1.session_id = t2.session_id
inner join table3 as t3 on t1.company_id = t3.company_id
where t2.message_type = 2
原始查询稍微复杂一些,有更多的标量子查询。此查询的执行时间特别长。我检查了解释函数,标量子查询似乎需要很长时间才能执行。但是,我想不出更好的办法。
这是查询:
select t1.session_id as session1, t1.company_id as company1,
@transfer_time := t2.message_time as transfer_time,
@company2 := trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 t1 inner join
table3 t3
on t1.company_id = t3.company_id
where t1.session_start_time >= @transfer_time and
t3.company_name = @company2
order by t1.session_start_time
limit 1
) s session 2
from table1 t1 inner join
table2 t2
on t1.session_id = t2.session_id inner join
table3 t3
on t1.company_id = t3.company_id
where t2.message_type = 2;
首先,变量的使用不正确。 MySQL 不保证 SELECT
中变量的求值顺序。因此,您需要输入原始定义:
select t1.session_id as session1, t1.company_id as company1,
t2.message_time as transfer_time,
trim(substring(t2.message, 38, locate(' abc', t2.message) - 38)) as company2,
(select t1.session_id
from table1 t1 inner join
table3 t3
on t1.company_id = t3.company_id
where t1.session_start_time >= t2.message_time and
t3.company_name = trim(substring(t2.message, 38, locate(' abc', t2.message) - 38))
order by t1.session_start_time
limit 1
) s session 2
from table1 t1 inner join
table2 t2
on t1.session_id = t2.session_id inner join
table3 t3
on t1.company_id = t3.company_id
where t2.message_type = 2;
接下来,我会尝试索引:table3(company_name, company_id)
和 table1(company_id, session_start_time, session_id)
。