gpt4 book ai didi

添加和或子句时MySql Left Join速度慢

转载 作者:行者123 更新时间:2023-11-29 00:16:42 35 4
gpt4 key购买 nike

我对查询的性能有疑问。我有以下内容:

SELECT DISTINCT anuncios.id, precios.precio
FROM anuncios
LEFT JOIN precios ON ( (
precios.idcentroanuncio = anuncios.idcentro
AND anuncios.tipoubicacion = 'centropropio'
AND precios.tipocentroanuncio = 'centro'
) )
GROUP BY anuncios.id
LIMIT 0 , 30

花费 0.001 段。另一方面,我有这个,非常相似:

 SELECT DISTINCT anuncios.id, precios.precio
FROM anuncios
LEFT JOIN precios ON (
(
precios.idcentroanuncio = anuncios.id
AND anuncios.tipoubicacion = 'centropropio'
AND precios.tipocentroanuncio = 'centro'
) )
GROUP BY anuncios.id
LIMIT 0 , 30

也花费相同的时间,或多或少。问题是当我将两个查询合并到这个查询中时:

 SELECT DISTINCT anuncios.id, precios.precio
FROM anuncios
LEFT JOIN precios ON ( (
precios.idcentroanuncio = anuncios.idcentro
AND anuncios.tipoubicacion = 'centropropio'
AND precios.tipocentroanuncio = 'centro'
)
OR (
precios.idcentroanuncio = anuncios.id
AND anuncios.tipoubicacion = 'centropropio'
AND precios.tipocentroanuncio = 'centro'
) )
GROUP BY anuncios.id
LIMIT 0 , 30

此处执行时间乘以 100。

这些都是表描述:

宝仕奥:

+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| usuario | varchar(40) | NO | MUL | NULL | |
| tipocentroanuncio | varchar(50) | NO | | NULL | |
| idcentroanuncio | int(10) unsigned | NO | MUL | NULL | |
| unidades | float | NO | | NULL | |
| tipounidades | varchar(15) | NO | | NULL | |
| precio | float | NO | | NULL | |
| otrosdatosprecio | varchar(100) | NO | | NULL | |
| principal | int(1) unsigned | NO | | NULL | |
+-------------------+------------------+------+-----+---------+----------------+

通告:

+------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_ |
+------------------+------------------+------+-----+---------------------+------

这是查询的解释:

id  select_type     table   type    possible_keys   key     key_len     ref     rows Extra
1 SIMPLE anuncios ALL NULL NULL NULL NULL 1048578 Using temporary; Using filesort

1 SIMPLE precios index idcentroanuncio idcentroanuncio 156 NULL 30 Using index

我做错了什么?这次将两个独立快速运行的查询结合起来是否合乎逻辑?

最佳答案

是的。您已经了解到 确实会打乱执行计划。尝试使用 union 代替:

SELECT a.id, p.precio
FROM anuncios a LEFT JOIN
precios p
ON p.idcentroanuncio = a.idcentro AND
a.tipoubicacion = 'centropropio' AND
p.tipocentroanuncio = 'centro'
UNION
SELECT a.id, p.precio
FROM anuncios a LEFT JOIN
precios p
ON p.idcentroanuncio = a.id AND
a.tipoubicacion = 'centropropio' AND
p.tipocentroanuncio = 'centro'
LIMIT 0 , 30;

一些注释,然后是更好的查询。首先,当您有group by 时,您不需要distinct。其次,此版本不需要 group bydistinct,因为 union 负责使值不同。

为了提高性能,您可以采用不同的路线。试试这个:

select p.idcentroanuncio, p.price
from precios p
where p.tipocentroanuncio = 'centro' and
(exists (select 1
from annuncios a
where p.idcentroanuncio = a.id and a.tipoubicacion = 'centropropio'
) or
exists (select 1
from annuncios a
where p.idcentroanuncio = a.idcentro and a.tipoubicacion = 'centropropio'
)
);

这可能会消除对 distinct 的需要。每个子查询都可以利用索引:annuncios(id, tipoubicacion)annuncios(idcentro, tipoubicacion)

关于添加和或子句时MySql Left Join速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731901/

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