Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could this query be optimized in terms of execution speed

Tags:

mysql

This query should give me the closest element to a given :x, :y in cartesian coordinates.

SELECT `type`
FROM `mapgen_centers`
ORDER BY SQRT(POW((:x - `x`), 2) + POW((:y - `y`), 2))
LIMIT 1

It currently takes 0.002s on average which is okay, but I have the feeling this can be better, especially because I currently fire it very, very often and frequent, so that the whole execution of the script piles of to several minutes.

Can (and if, how) this be optimized through any means available on a standard MySQL installation (procedures, functions, indexes, configuration, ...)

like image 493
F.P Avatar asked Aug 08 '13 14:08

F.P


People also ask

How the query is optimized?

Query Optimization in DBMS is the process of selecting the most efficient way to execute a SQL statement. Because SQL is a nonprocedural language, the optimizer can merge, restructure, and process data in any sequence. Query Optimization in DBMS has a significant application in database designing.

How can we increase a speed of SQL query?

Reduce nested views to reduce lags This nesting causes too many data returns for every single query, which either makes the database crawl, or completely give up and give no returns. Minimizing nesting is a simple way to make your SQl query efficient and significantly improve speeds.


1 Answers

1.You may use MySQL spatial extension.

2.Strip the SQRT function, because it is unnecessary when ordering.

like image 163
adamsmith Avatar answered Oct 18 '22 19:10

adamsmith