Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL联盟:哪种方法最快_Mysql_Union - Fatal编程技术网

MySQL联盟:哪种方法最快

MySQL联盟:哪种方法最快,mysql,union,Mysql,Union,通常我会选择最小的代码,但我目前正在构建一个支付网关,它可能每天处理数百万个连接,因此代码中的每一个调整都有助于我构建的性能,但是我有一个问题,只有mysql专家才能真正回答 对于联合,最好使用where子句过滤联合的每一方,还是整理记录,然后对联合集进行过滤 哪一个最快: (一) 2) 或者这是一个更小的代码,更快(但不是更慢) 从逻辑上讲,我会说(1)更快,因为每个联合集都经过过滤,所以需要合并的记录更少 使现代化 我找到了一个更好的方法,不用工会来写这篇文章 select r.*, g.`

通常我会选择最小的代码,但我目前正在构建一个支付网关,它可能每天处理数百万个连接,因此代码中的每一个调整都有助于我构建的性能,但是我有一个问题,只有mysql专家才能真正回答

对于联合,最好使用where子句过滤联合的每一方,还是整理记录,然后对联合集进行过滤

哪一个最快:

(一)

2) 或者这是一个更小的代码,更快(但不是更慢)

从逻辑上讲,我会说(1)更快,因为每个联合集都经过过滤,所以需要合并的记录更少

使现代化 我找到了一个更好的方法,不用工会来写这篇文章

select r.*, g.`name` 'gateway_name' 
from payment_routes r
join gateways g on r.gateway_id = g.id
where (
    (channel_id is null and currency_id != v_sell_currency_id)
    or
    (channel_id = p_channel_id and currency_id = v_sell_currency_id)
)
and card_id = v_card_id
and currency_id = v_pay_currency_id
and enabled = 1

你试过了吗?查看它们的执行时间?好的,连接表以及where主要取决于性能的索引,UNION将进行聚合,因此在一小部分数据中,所有这些看起来都很好,但当数据增长时,情况就不同了。我建议使用
解释您的查询
并查看结果,这将提供更多信息。网关id上有一个索引,所有id都是主键,因此也被索引。可能会有超过10K行的支付路线,我想随着数据的增长,我只需要测试响应时间并制定计划,但我觉得选项1可能是最快的。感谢您的回复。我希望带有独立和完整where子句的双边联合将大大超过融合联合
JOIN
性能不仅取决于索引,还取决于数据集的大小(记录数)。性能取决于。您还没有提供足够的详细信息供我们确定,所以请进行实验。MySQL非常擅长选择最好的方法并变得更好。有时候,最好只是问一下你想要什么,然后让MySQL决定怎么做。如果您优化了自己,您将需要观察数据的变化,因为有些方法不能很好地扩展。
select * from (
    select * from payment_routes 
    where channel_id is null 
    and currency_id != v_currency_id
  union 
    select * from payment_routes 
    where channel_id = p_channel_id 
    and currency_id = v_currency_id
) t1
join gateways g on t1.gateway_id = g.id
where t1.currency_id = v_currency_id
and t1.card_id = v_card_id 
and t1.enabled = 1
;
select r.*, g.`name` 'gateway_name' 
from payment_routes r
join gateways g on r.gateway_id = g.id
where (
    (channel_id is null and currency_id != v_sell_currency_id)
    or
    (channel_id = p_channel_id and currency_id = v_sell_currency_id)
)
and card_id = v_card_id
and currency_id = v_pay_currency_id
and enabled = 1