我将这两个查询结合起来:
SELECT `ad_general`.`id`, `ad_general`.`status`, `ad_general`.`category`, `ad_general`.`type`, `ad_general`.`specification`, `ad_general`.`m2`, `ad_general`.`price`, `ad_general`.`title`, `ad_general`.`city`, `ad_general`.`parabirimi`, `ad_general`.`tarih`, `kgsim_districts`.`isim` as district, `kgsim_cities`.`isim` as city, (
SELECT `adpictures`.`picturelink`
FROM `adpictures`
WHERE `adpictures`.`adid` = `ad_general`.`id` LIMIT 1
) AS picture
FROM (`ad_general`)
LEFT JOIN `kgsim_cities` ON `kgsim_cities`.`id` = `ad_general`.`city`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
WHERE `ad_general`.`city` = '708'
AND `ad_general`.`status` = '1'
AND `ad_general`.`category` = '1'
AND `ad_general`.`type` = '3'
ORDER BY `ad_general`.`id` desc
LIMIT 15
SELECT count(`ad_general`.`id`) as sayi
FROM (`ad_general`)
WHERE `city` = '708'
AND `status` = '1'
AND `category` = '1'
AND `type` = '3'
在该查询中:
SELECT `ad_general`.`id`, (
SELECT count(`ad_general`.`id`) as sayi
FROM (`ad_general`)
WHERE `city` = 708 AND `status` = 1 AND `category` = 1 AND `type` = 3 ) AS sayi, `ad_general`.`status`, `ad_general`.`category`, `ad_general`.`type`, `ad_general`.`specification`, `ad_general`.`m2`, `ad_general`.`price`, `ad_general`.`title`, `ad_general`.`city`, `ad_general`.`parabirimi`, `ad_general`.`tarih`, `kgsim_districts`.`isim` as district, `kgsim_cities`.`isim` as city, (
SELECT `adpictures`.`picturelink`
FROM `adpictures`
WHERE `adpictures`.`adid` = `ad_general`.`id` LIMIT 1
) AS picture
FROM (`ad_general`)
LEFT JOIN `kgsim_cities` ON `kgsim_cities`.`id` = `ad_general`.`city`
LEFT JOIN `kgsim_districts` ON `kgsim_districts`.`id` = `ad_general`.`district`
WHERE `ad_general`.`city` = '708'
AND `ad_general`.`status` = '1'
AND `ad_general`.`category` = '1'
AND `ad_general`.`type` = '3'
ORDER BY `ad_general`.`id` desc
LIMIT 15
我想知道哪一个是最好的方法,为什么?因为它让我对每一行都有“sayi”结果感到有些困惑。
答案 0 :(得分:1)
我会在两个查询中使用mysql explain command,并根据结果调整数据库。
我支持“一体化”方法,因为它可以一次性为mysql提供更多信息,因此可以更好地决定如何运行搜索。
然而,变量太多了。通常运行解释然后调整表或索引并再次运行说明给出最好的性能恕我直言。
答案 1 :(得分:1)
如果仅考虑查询时间,则第一个选项可能会更快。但是,两个独立的查询占用服务器的次数更多,如果连接时间很长,查询速度会很慢。
如果你正确使用了索引和缓存表,第二种是更好的方法,特别是如果你有很多查询或连接。