Skip to content

Instantly share code, notes, and snippets.

@hoverlover
Created September 22, 2011 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoverlover/ac9d688e0274846af55c to your computer and use it in GitHub Desktop.
Save hoverlover/ac9d688e0274846af55c to your computer and use it in GitHub Desktop.
Analyzing performance between two queries
explain analyze SELECT *, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(lon - -93.3993751), RADIANS(lat - 37.1415852))) + 360 AS decimal) % 360 AS bearing FROM "businesses" WHERE (lat BETWEEN 35.69426736889152 AND 38.58890303110848 AND lon BETWEEN -95.21499983156514 AND -91.58375036843486) GROUP BY businesses.id,businesses.merchant_name,businesses.lat,businesses.lon HAVING 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) <= 100 ORDER BY 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) ASC;
-------------------------- QUERY PLAN --------------------------
Sort (cost=19711.51..19712.09 rows=234 width=1289) (actual time=137.987..138.038 rows=453 loops=1)
Sort Key: ((7917.511728464::double precision * asin(sqrt((power(sin((((((37.1415852 - lat))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.797145911237304::double precision * cos((((lat)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-93.3993751) - lon))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))))
Sort Method: quicksort Memory: 426kB
-> HashAggregate (cost=19679.48..19702.30 rows=234 width=1289) (actual time=131.046..136.257 rows=453 loops=1)
-> Seq Scan on businesses (cost=0.00..19659.01 rows=234 width=1289) (actual time=0.704..128.545 rows=453 loops=1)
Filter: ((lat >= 35.69426736889152) AND (lat <= 38.58890303110848) AND (lon >= (-95.21499983156514)) AND (lon <= (-91.58375036843486)) AND ((7917.511728464::double precision * asin(sqrt((power(sin((((((37.1415852 - lat))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.797145911237304::double precision * cos((((lat)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-93.3993751) - lon))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))) <= 100::double precision))
Total runtime: 138.280 ms
(7 rows)
explain analyze SELECT *, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(lon - -93.3993751), RADIANS(lat - 37.1415852))) + 360 AS decimal) % 360 AS bearing FROM "businesses" WHERE (lat BETWEEN 35.69426736889152 AND 38.58890303110848 AND lon BETWEEN -95.21499983156514 AND -91.58375036843486 AND 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) <= 100) ORDER BY 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((37.1415852 - lat) * PI() / 180 / 2), 2) + COS(37.1415852 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((-93.3993751 - lon) * PI() / 180 / 2), 2) )) ASC;
-------------------------- QUERY PLAN --------------------------
Sort (cost=19688.69..19689.28 rows=234 width=1289) (actual time=139.075..139.122 rows=453 loops=1)
Sort Key: ((7917.511728464::double precision * asin(sqrt((power(sin((((((37.1415852 - lat))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.797145911237304::double precision * cos((((lat)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-93.3993751) - lon))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))))
Sort Method: quicksort Memory: 426kB
-> Seq Scan on businesses (cost=0.00..19679.48 rows=234 width=1289) (actual time=0.701..137.275 rows=453 loops=1)
Filter: ((lat >= 35.69426736889152) AND (lat <= 38.58890303110848) AND (lon >= (-95.21499983156514)) AND (lon <= (-91.58375036843486)) AND ((7917.511728464::double precision * asin(sqrt((power(sin((((((37.1415852 - lat))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision) + ((0.797145911237304::double precision * cos((((lat)::double precision * 3.14159265358979::double precision) / 180::double precision))) * power(sin(((((((-93.3993751) - lon))::double precision * 3.14159265358979::double precision) / 180::double precision) / 2::double precision)), 2::double precision)))))) <= 100::double precision))
Total runtime: 139.289 ms
(6 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment