gpt4 book ai didi

MYSQL查询优化,多查询还是一次大查询

转载 作者:可可西里 更新时间:2023-11-01 08:51:55 25 4
gpt4 key购买 nike

我有一个查询,其中有一些子查询(内部选择),我正在尝试找出哪个对性能更好,一个较大的查询或许多较小的查询,我发现很难尝试对差异进行计时因为它在我的服务器上一直在变化。

我使用下面的查询一次返回 10 个结果以显示在我的网站上,使用分页(偏移和限制)。

SELECT adverts.*, breed.breed, breed.type, sellers.profile_name, sellers.logo, users.user_level , 
round( sqrt( ( ( (adverts.latitude - '51.558430') * (adverts.latitude - '51.558430') ) * 69.1 * 69.1 ) + ( (adverts.longitude - '-0.0069345') * (adverts.longitude - '-0.0069345') * 53 * 53 ) ), 1 ) as distance,
( SELECT advert_images.image_name FROM advert_images WHERE advert_images.advert_id = adverts.advert_id AND advert_images.main = 1 LIMIT 1) as imagename,
( SELECT count(advert_images.advert_id) from advert_images WHERE advert_images.advert_id = adverts.advert_id ) AS num_photos
FROM adverts
LEFT JOIN breed ON adverts.breed_id = breed.breed_id
LEFT JOIN sellers ON (adverts.user_id = sellers.user_id)
LEFT JOIN users ON (adverts.user_id = users.user_id)
WHERE (adverts.status = 1) AND (adverts.approved = 1)
AND (adverts.latitude BETWEEN 51.2692837281 AND 51.8475762719) AND (adverts.longitude BETWEEN -0.472015213613 AND 0.458146213613)
having (distance <= '20')
ORDER BY distance ASC
LIMIT 0,10

从主查询中删除下面的 2 个内部选择,然后在我的 php 循环中,调用 2 个选择 10 次,循环中的每条记录一次,这样会更好吗?

( SELECT advert_images.image_name FROM advert_images WHERE advert_images.advert_id = adverts.advert_id AND advert_images.main = 1 LIMIT 1) as imagename, 
( SELECT count(advert_images.advert_id) from advert_images WHERE advert_images.advert_id = adverts.advert_id ) AS num_photos

最佳答案

避免子查询

据我了解,您的内部选择有两个用途:查找关联图像的任何名称,以及计算关联图像的数量。您可能可以使用左连接而不是内部选择来实现这两个目标:

SELECT …,
advert_images.image_name AS imagename,
COUNT(advert_images.advert_id) AS num_photos,

FROM …
LEFT JOIN advert_images ON advert_images.advert_id = adverts.advert_id

GROUP BY adverts.advert_id

LIMIT 0,10

我还没有尝试过这个,但也许 MySQL 引擎足够聪明,可以只对您实际返回的行执行那部分查询。

请注意,对于给定的图像集,此查询将返回哪个图像名称完全没有保证。如果你想要可重现的结果,你应该在那里使用一些聚合函数,例如MIN(advert_images.image_name) 选择字典顺序的第一张图片。

单独选择但不循环

如果上述方法不起作用,即查询仍将检查 advert_images 表以获取计算结果的所有行,那么您可能真的会更好执行第二个查询。但是,您可以尝试避免 for 循环,而是在单个查询中获取所有这些行:

SELECT advert_images.image_name AS imagename,
COUNT(advert_images.advert_id) AS num_photos
FROM advert_images
WHERE advert_images.advert_id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
GROUP BY advert_images.advert_id

此查询中的十个参数对应于您当前生成的结果的十行。请注意,没有相关照片的广告将不会包含在该结果中。因此,请确保在您的代码中将 num_photos 默认为零,并将 imagename 默认为 NULL

临时表

实现您尝试做的事情的另一种方法是使用显式临时内存表:首先选择您感兴趣的结果,然后检索所有相关信息。

CREATE TEMPORARY TABLE tmp
SELECT adverts.advert_id, round(…) as distance
FROM adverts
WHERE (adverts.status = 1) AND (adverts.approved = 1)
AND (adverts.latitude BETWEEN 51.2692837281 AND 51.8475762719)
AND (adverts.longitude BETWEEN -0.472015213613 AND 0.458146213613)
HAVING (distance <= 20)
ORDER BY distance ASC
LIMIT 0,10;

SELECT tmp.distance, adverts.*, …
advert_images.image_name AS imagename,
COUNT(advert_images.advert_id) AS num_photos,

FROM tmp
INNER JOIN adverts ON tmp.advert_id = adverts.advert_id
LEFT JOIN breed ON adverts.breed_id = breed.breed_id
LEFT JOIN sellers ON adverts.user_id = sellers.user_id
LEFT JOIN users ON adverts.user_id = users.user_id
LEFT JOIN advert_images ON advert_images.advert_id = adverts.advert_id
GROUP BY adverts.advert_id
ORDER BY tmp.distance ASC;

DROP TABLE tmp;

这将确保仅针对您当前正在处理的结果查询所有其他表。毕竟,advert_images 表除了您可能需要其中的多行之外没有什么神奇之处。

子查询作为连接因子

基于上一段的方法,您甚至可以避免管理临时表,而使用子查询代替:

SELECT sub.distance, adverts.*, …
advert_images.image_name AS imagename,
COUNT(advert_images.advert_id) AS num_photos,

FROM ( SELECT adverts.advert_id, round(…) as distance
FROM adverts
WHERE (adverts.status = 1) AND (adverts.approved = 1)
AND (adverts.latitude BETWEEN 51.2692837281 AND 51.8475762719)
AND (adverts.longitude BETWEEN -0.472015213613 AND 0.458146213613)
HAVING (distance <= 20)
ORDER BY distance ASC
LIMIT 0,10;
) AS sub
INNER JOIN adverts ON sub.advert_id = adverts.advert_id
LEFT JOIN breed ON adverts.breed_id = breed.breed_id
LEFT JOIN sellers ON (adverts.user_id = sellers.user_id)
LEFT JOIN users ON (adverts.user_id = users.user_id)
LEFT JOIN advert_images ON advert_images.advert_id = adverts.advert_id
GROUP BY adverts.advert_id
ORDER BY sub.distance ASC

您再次通过仅使用 adverts 表中的数据来确定相关行,并仅连接其他表中的所需行。最有可能的是,该中间结果将在内部存储在一个临时表中,但这由 SQL 服务器决定。

关于MYSQL查询优化,多查询还是一次大查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12583751/

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