Quantcast
Channel: Optimizing this MySQL query with a subquery inside - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Optimizing this MySQL query with a subquery inside

$
0
0

For given longitude, latitude and radius I should select the averagePrice, numberOfListings, ... from a database with 500'000 data records.

(id) 1 (select_type) SIMPLE (table) database (partitions) NULL (type) ALL (possible_keys) NULL (key) NULL (key_len) NULL (ref) NULL (rows) 623612 (filtered) 100.00 (Extra) NULL

CREATE TABLE `database` (
  `id` varchar(255) DEFAULT NULL,
  `longitude` varchar(255) DEFAULT NULL,
  `latitude` varchar(255) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `bathrooms` int(11) DEFAULT NULL,
  `bedrooms` int(11) DEFAULT NULL,
  `person_capacity` int(11) DEFAULT NULL,
  `rev_count` int(11) DEFAULT NULL,
  KEY `hosting_id` (`hosting_id`),
  KEY `price` (`price`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Select Query without grouping.

SELECT
   avg(price) as averagePrice,
   count(*) as numberOfListings,
   min(price) as minprice,
   max(price) as maxprice,
   avg(bedrooms) as averagebedrooms,
   avg(bathrooms) as averagebathrooms,
   avg(person_capacity) as averagepc,
   avg(rev_count) as averageReviews,
   avg(time_appartement) as averageDateHasBeenListed  
FROM
   (SELECT
      r.*,
      ( 6371 * acos( cos( radians(37.774929) ) * cos( radians( ANY_VALUE(`latitude` )) ) * cos( radians( ANY_VALUE(`longitude`) ) - radians(-122.419416) ) + sin( radians(37.774929) ) * sin( radians( ANY_VALUE(`latitude`) ) ) ) ) AS distance        
   FROM
      `database` r       ) r  
WHERE
   distance <= 25 
   AND price >= 10  
ORDER BY
   distance ASC

This works well with a query time about 1 sec. Now my next steps will be to group the subquery with the id and select there for every id the average price, bedrooms, bathrooms, person_capacity, rev_count and time_appartement.

SELECT
   avg(price) as averagePrice,
   count(*) as numberOfListings,
   min(price) as minprice,
   max(price) as maxprice,
   avg(bedrooms) as averagebedrooms,
   avg(bathrooms) as averagebathrooms,
   avg(person_capacity) as averagepc,
   avg(rev_count) as averageReviews,
   avg(time_appartement) as averageDateHasBeenListed      
FROM
   (SELECT
      id,
      avg(r.price) as price,
      avg(r.bedrooms) as bedrooms,
      avg(r.bathrooms) as bathrooms,
      avg(r.person_capacity) as person_capacity,
      avg(r.rev_count) as rev_count,
      avg(r.time_appartement) as time_appartement,
      ( 6371 * acos( cos( radians(37.774929) ) * cos( radians( ANY_VALUE(`latitude` )) ) * cos( radians( ANY_VALUE(`longitude`) ) - radians(-122.419416) ) + sin( radians(37.774929) ) * sin( radians( ANY_VALUE(`latitude`) ) ) ) ) AS distance            
   FROM
      `database` r 
   GROUP BY
      r.id           ) r      
WHERE
   distance <= 25 
   AND price >= 10      
ORDER BY
   distance ASC

It works, but the problem is that the time for this query is about 7 sec.

Is it possible to reduce the time? Thanks for your replies.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images