gpt4 book ai didi

mysql - 我需要提高查询的性能

转载 作者:行者123 更新时间:2023-11-29 23:24:33 26 4
gpt4 key购买 nike

对于 10000 条记录它工作得很好,但对于 2300000 条记录它不起作用,我如何处理这种类型的查询以提高性能请帮助我对这个 mysql 比较陌生

 select 
r1.countrycode,
r1.code,
r1.rate,
f.cdr_id,
f.cld,
f.direction,
from
rates1 r1,
calls f
where(r1.code,f.cld) in
(select
max(r1.code),
f.cld
from rates1 r1, calls f
where (f.cld regexp concat('^',r1.code,'[0-9]+$'))
group by f.cld

);

Sub query returns the Unique values for code and cld and main query use the code and cld in where
condition and result the all the values

calls table data
cdr_id cld direction duration
1 1985484555 incoming 59
2 8475858585 outgoing 456
3 1895858888 outgoing 555
4 1548458455 incomimg 895
5 548585665 incoming 585
6 1985484555 outgoing 585

rates1 table data
countryocde code rate
040 19854 0.35
080 198 0.356
578 847 0.25
458 1548 0.50
555 1548 0.75

嗨, friend 们,我写这个查询是为了选择 2 个表的 Code 和 cld 列的唯一值,它在我的记录数量较少的情况下工作正常,并且不会执行超过 1000000 条记录,它会长时间执行而无需给出任何结果,请帮忙提前致谢。

最佳答案

从获取国家/地区代码开始:

select c.*,
(select countrycode
from rates1 r1
where c.cld like concat(r1.code, '%')
order by length(r1.code) desc
limit 1
) as countrycode
from calls c;

然后,将费率信息重新加入:

select *
from (select c.*,
(select r1.code
from rates1 r1
where c.cld like concat(r1.code, '%')
order by length(r1.code) desc
limit 1
) as r1code
from calls c
) c join
rates1 r
on c.r1code = r.code

这仍然会有较差的性能,但它会比两个笛卡尔积更好。

您可以使用 rates1(code) 上的索引和类似以下的过程来加快执行速度:

select c.*, coalesce(r5.countrycode, r4.countrycode, r3.countrycode, r2.countrycode, r1.countrycode) as countrycode
from calls c left join
rates1 r1
on r1.code = left(c.code, 1) left join
rates1 r2
on r2.code = left(c.code, 2) left join
rates1 r3
on r3.code = left(c.code, 3) left join
rates1 r4
on r4.code = left(c.code, 4) left join
rates1 r5
on r5.code = left(c.code, 5);

您需要足够的连接来实现最长的可能代码长度。

关于mysql - 我需要提高查询的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061314/

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