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, ...)
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.
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.You may use MySQL spatial extension.
2.Strip the SQRT function, because it is unnecessary when ordering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With