Using query result data for another query - mysql

The following query works for me. However, is there a way to speed it up (table1/2 contain each more than 300000 entries). I also would like to query more data in the subquery and use the distance just to filter results. The two tables have not much in common except the lat/lon.
SELECT `lat1`,
`lon1`,
(SELECT Sqrt(Pow( 69.1 * ( `lat1` - `lat2` ), 2 )
+ Pow( 69.1 * ( `lon2` - `lon1` ) * Cos( `lat` / 57.3 ), 2 )
) AS
distance
FROM table1
ORDER BY distance
LIMIT 0, 1) AS `test`
FROM `table2`
Thanks in advance

Related

mySQL returning different row from calculated value

SELECT table1.id, (SELECT MIN(SQRT( POW( 69.1 * ( latitude - table1.lat ) ,
2 ) + POW( 69.1 * ( table1.lon - longitude ) * COS( latitude / 57.3 ) , 2 )
)) from table2 limit 1) AS distance FROM table1
The result itself seems correct, however I actually want the query to return table2.id based on the calculated value instead. How can I achieve that?
table1 contains id, lat, lon
table2 contains id, latitude, longitude
I do need to get the smallest distance's ID(table2.id) for each row in table1 using the data in table2.
SELECT table1.id, (SELECT table2.id from table2 order by (SQRT( POW( 69.1 * (
latitude - table1.lat ) , 2 ) + POW( 69.1 * ( table1.lon - longitude ) * COS(
latitude / 57.3 ) , 2 ) )) limit 0,1 ) as table2_id FROM table1

Searching within a distance in SQL

I have database with a list of contacts with puesdo coordinates of where they live. Here's a sample:
name e_point n_point
David 102 345
James 174 746
Ali 460 584
Kevin 364 479
Mark 385 274
I was wondering is it possible to create a query that can search within a distance of the two coordinates? E.g., I want a list of people who live within a 20 sqr mile radius of James.
What functions can help me do this?
Mysql syntax:
SELECT name FROM `table` WHERE SQRT(
POW(e_point - (SELECT e_point FROM `table` WHERE name='james'), 2) +
POW(n_point - (SELECT n_point FROM `table` WHERE name='james'), 2)) < 20
AND name <> 'james'
Notice:
you need to change 'name' in 3 places.
subqueries added for let you run one query with just one variable (name). If you remove subqueries, you need to run 2 queries (first query retrieving coord, and second query searching near people)
equation is:
Where p1=(p1x, p1y) and p2=(p2x, p2y)
To calculate the distance between 2 coordinates you need to get the Great-circle distance, as the earth is rounded and measurement of distance is affected by this fact.
Doing this with SQL would be something like this according to the Google Maps API docs:
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )
AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;
if your coordinates represent like x and y coordinates you can use something like this (just replaces occurrences of contacts with your actual table name) distance returned is the distance from 'James':
SELECT T2.name,
SQRT(POWER(T2.e_point-T1.e_point,2)+
POWER(T2.n_point-T1.n_point,2)) as distance
FROM contacts T1
INNER JOIN contacts T2 ON
SQRT(POWER(T2.e_point-T1.e_point,2)+
POWER(T2.n_point-T1.n_point,2)) <= 20
WHERE T1.name = 'James'
AND T1.name != T2.name
sqlfiddle

MySQL - Filtering results by other tables

I have a procedure that has a main purpose of locating 9 users closest to you based on a longitude and latitude coordinate. However, I need to filter these results (and still return 9) based on values of other tables.
Here is the procedure that I'm currently using that doesn't have any filtering.
BEGIN
DECLARE lat FLOAT(11,8);
DECLARE lon FLOAT(11,8);
DECLARE dist INT(11);
SELECT latitude, longitude, search_radius
INTO lat, lon, dist
FROM accounts
INNER JOIN account_settings
ON accounts.account_id = account_settings.account_id
WHERE accounts.account_id = account_in;
SELECT account_id, first_name, bio, birthdate
, ( 3959 * acos( cos( radians(lat) ) * cos( radians(latitude) ) * cos( radians( longitude ) - radians(lon) ) + sin( radians(lat) ) * sin(radians( latitude )) ) )
AS distance
FROM `accounts`
WHERE account_id != account_in
HAVING distance < dist
ORDER BY distance ASC LIMIT 9;
END
However, I need to add some conditions to the select statement at the bottom, making it so it will still return 9 (if available) based on filtering from the following tables.
blocked_users
completed_test
failed_test
The three tables above have the same table structure of [ account_id, other_id ].
How would I make sure that none of the users returned in the last select statement are present in the other_id field of any of the tables listed, while matched with the corresponding account_id passed through the account_in variable.
I've been racking my brain for about 6 hours and I just can't seem to come up with a good conclusion. This query is much higher level (or I'm overthinking it a lot) than I'm used to dealing with. As i only have basic MySQL knowledge.

MySQL command out of sync when using POW()

I have this MySQL query that runs perfectly, until I add the POW function to it. I don't understand why I get this error. I've tried to google it but came up empty for a solution.
Original query:
SELECT 86400 /
(NOW() - `created_at`) *
(
(
SELECT COUNT(`Votes`.`voteID`)
FROM `Votes`
WHERE `postID` = `Posts`.`postID`
LIMIT 1
)
+ 0
* 5
) AS `rank`
FROM `Posts`
query with POW():
SELECT POW(
86400
/ (NOW() - `created_at`)
*
(
(
SELECT COUNT(`Votes`.`voteID`)
FROM `Votes`
WHERE `postID` = `Posts`.`postID`
LIMIT 1
)
+ 0
* 5
),
1.8
) AS `rank`
FROM `Posts`
The error I get is #2014 - Commands out of sync; you can't run this command now
While this seems like interesting error in POW() function, you maybe can get around by encapsulating into another subquery:
SELECT POW(rank,1.8)
FROM
(
SELECT (
86400
/ (NOW() - `created_at`)
*
(
(
SELECT COUNT(`Votes`.`voteID`)
FROM `Votes`
WHERE `postID` = `Posts`.`postID`
LIMIT 1
)
+ 0
* 5
)
) AS `rank`
FROM Posts
) tabX
I tried Ollie Jones' suggestion, which worked perfectly for me!
You may be exposing a bug in POW() by passing it an absurdly large, or non-numeric first argument. (NOW() - created_at) probably doesn't do what you think it does. You may want UNIX_TIMESTAMP() - UNIX_TIMESTAMP(created_at) if you hope for a time-difference computation giving a number of seconds as a result. – Ollie Jones

Slow location based search result query

I have a query that I use to find results that are ordered by location. Results also have to account for VAT so this is also in the query. The query can unfortunately take 4+ seconds to run when not cached. Can anyone spot any glaringly obvious issues or suggest anything I can do to improve it?
Just to clarify what is happening in the query:
The distance is calculation is euclidean distance using lat/long
The incvat fields are used to show the price when vat is included
The WHEN / THEN statement is used to put prices of 0 at the very bottom
The query:
SELECT * , ROUND( SQRT( POW( ( 69.1 * ( company_branch_lat - 52.4862 ) ) , 2 ) + POW( ( 53 * ( company_branch_lng - - 1.8905 ) ) , 2 ) ) , 1 ) AS distance,
hire_car_day + ( hire_car_day * 0.2 * ! hire_car_incvat ) AS hire_car_day_incvat,
hire_car_addday + ( hire_car_addday * 0.2 * ! hire_car_incvat ) AS hire_car_addday_incvat,
hire_car_week + ( hire_car_week * 0.2 * ! hire_car_incvat ) AS hire_car_week_incvat,
hire_car_weekend + ( hire_car_weekend * 0.2 * ! hire_car_incvat ) AS hire_car_weekend_incvat
FROM hire_car
LEFT JOIN company_branch ON company_branch_id = hire_car_branchid
LEFT JOIN hire_cartypelink ON hire_cartypelink_carhireid = hire_car_id
LEFT JOIN users ON company_branch_userid = user_id
WHERE 1
GROUP BY hire_car_id
HAVING distance <=30
ORDER BY CASE hire_car_day_incvat
WHEN 0
THEN 40000
ELSE hire_car_day_incvat
END , distance ASC
LIMIT 0 , 30
You can use the mysql spatial extension and save the latitude and longitude as a point datatype and make it a spatial index. That way you can reorder the coordinates along a curve and reduce the dimension and preserve spatial information. You can use the spatial index as a bounding box to filter the query and then use the harvesine formula to pick the optimal result. Your bounding box should be bigger then the radius of the great circle. Mysql uses a rtree with some spatial index and my example was about a z curve or a hilbert curve: https://softwareengineering.stackexchange.com/questions/113256/what-is-the-difference-between-btree-and-rtree-indexing.
Then you can insert a geocoordinate directly into a point column: http://dev.mysql.com/doc/refman/5.0/en/creating-spatial-values.html. Or you can use a geometry datatype: http://markmaunder.com/2009/10/10/mysql-gis-extensions-quick-start/. Then you can use MBRcontains function like so: http://dev.mysql.com/doc/refman/4.1/en/relations-on-geometry-mbr.html or any other functions: http://dev.mysql.com/doc/refman/5.5/en/functions-for-testing-spatial-relations-between-geometric-objects.html. Hence you need a bounding box.
Here are some examples:
Storing Lat Lng values in MySQL using Spatial Point Type
https://gis.stackexchange.com/questions/28333/how-to-speed-up-this-simple-mysql-points-in-the-box-query
Here is a simple example with point datatype:
CREATE SPATIAL INDEX sx_place_location ON place (location)
SELECT * FROM mytable
WHERE MBRContains
(
LineString
(
Point($x - $radius, $y - $radius),
Point($x + $radius, $y + $radius)
)
location
)
AND Distance(Point($x, $y), location) <= $radius
MySQL latitude and Longitude table setup.
I'm not sure if it works because it's uses a radius variable with a bounding-box function. It's seems to me MBRwithin is a bit simpler, because it doesn't need any argument: Mysql: Optimizing finding super node in nested set tree.
You are using GROUP BY statement together with HAVING, although I don't see anywhere in the query any aggregate functions. I recommend you to re-write the query like this and see if it makes any difference
SELECT * , ROUND( SQRT( POW( ( 69.1 * ( company_branch_lat - 52.4862 ) ) , 2 ) + POW( ( 53 * ( company_branch_lng - - 1.8905 ) ) , 2 ) ) , 1 ) AS distance,
hire_car_day + ( hire_car_day * 0.2 * ! hire_car_incvat ) AS hire_car_day_incvat,
hire_car_addday + ( hire_car_addday * 0.2 * ! hire_car_incvat ) AS hire_car_addday_incvat,
hire_car_week + ( hire_car_week * 0.2 * ! hire_car_incvat ) AS hire_car_week_incvat,
hire_car_weekend + ( hire_car_weekend * 0.2 * ! hire_car_incvat ) AS hire_car_weekend_incvat
FROM hire_car
LEFT JOIN company_branch ON company_branch_id = hire_car_branchid
LEFT JOIN hire_cartypelink ON hire_cartypelink_carhireid = hire_car_id
LEFT JOIN users ON company_branch_userid = user_id
WHERE ROUND( SQRT( POW( ( 69.1 * ( company_branch_lat - 52.4862 ) ) , 2 ) + POW( ( 53 * ( company_branch_lng - - 1.8905 ) ) , 2 ) ) , 1 ) <= 30
ORDER BY CASE hire_car_day_incvat
WHEN 0
THEN 40000
ELSE hire_car_day_incvat
END , distance ASC
LIMIT 0 , 30