5

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

我使用下面的查询一次返回 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 
4

2 回答 2

2

避免子查询

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

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为零。imagenameNULL

临时表

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

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 服务器来决定。

于 2012-09-25T14:41:42.690 回答
0

我认为 MySQL 使用文件排序 + 临时表来执行您的查询。这就是为什么在大桌子上你的建议会产生更好的结果。一般来说,您最好执行较小的查询然后 1 大。

于 2012-09-25T13:16:00.007 回答