this is my first post, so I hope I follow all the conventions :D
I have a MySql database with x and y coordinates and a php web service that receives other x and y coords. I want to calculate the square distance: (x2-x1)^2 + (y2-y1)^2 and sort from closest to farthest
SQL query (in php) would be:
SELECT ((x_coor-$x)*(x_coor-$x)+(y_coor-$y)*(y_coor-$y)) AS SquareDis FROM $table ORDER BY SquareDis
What is more gentle to performance, doing this in a SQL query or in the php program?
Thanks for all answers in advance!
If you want to sort by the results, then do the calculation in SQL.
Otherwise, you are just using the database as a "file store" and not taking advantage of the functionality that it offers. In addition, by doing the ordering in the database, you can limit the number of rows being returned -- another optimization.
Related
I need to get the peak from a dataset using PHP. This dataset is made with timestamp and value. I need to get the 3 peak like the image with the 3 relative timestamp
This is a graphic rappresentation of the dataset:
But i don't need to rappresentate graphically, i'd like just a simple return of an array of the three value/timestamp. I need also a sort of threshold for avoid flase positive peak, for example minimum variation like from 0 to 400 (i'll define it in case but i need a threshold)
You can find the example dataset here:
https://wetransfer.com/downloads/d7d20a726285ea29ae2ff682764b045020210401192032/13e788
Many thanks for the help, i'm stuck with this. I have searched on Stackoverflow, i have see some algorithm but i cant apply to my necessity
I think that your sample data size suggests a database as a proper tool for the job. So assuming that data is already stored in table readings with two numeric columns - ts and reading, then this query may help.
select ts, reading
from
(
select ts, reading, lag(reading) over (order by ts) as variation from readings
) as t
where variation < 400 -- or whatever your threshold value may be
order by reading desc
limit 3;
This is Postgresql dialect that I am most comfortable with. You can re-write it in another SQL dialect if necessary and then easily pull the result data in PHP using PDO for example.
I'm working on a PHP / MySQLI application, where the user needs to input a number, and then it should get the 5 closest records to that given number.
Can this be done by a simple SQL string, or do I need to get all the numbers into an array, and then match by that..?
Thanks!
This is possible through following query:
SELECT * FROM [table]
ORDER BY ABS([column] - [userinput])
LIMIT 5
However, if you could provide more information we would also be able to provide you with a better solution. This query is not very scalable and will after a couple of thousand rows start to get slow.
How are you going to use this query? Are we talking thousands of records? What kind of numbers is it? Is there some pattern? All such questions would allow for a more precise solution that could possible scale better with your system.
For example, how can I randomize rows ten through 20?
I only know how to randomize all rows using RANDR().
I am using PHP + MYSQLi.
Can someone explain me using MYSQL RANDR() ? I know it's not efficient.
Thanks,
Tom.
I have used a technique with SQL Server that might port to MYSQL. I assign a new GUID to each row, then sort on that.
I have a PHP form that grabs user-entered data and then posts it to a MySQL database. I'd like to know, how I can take the mathematical difference between two fields and post it to a third field in the database?
For example, I'd like to subtract "travel_costs" from "show_1_price" and write the difference to the "total_cost" variable. What's the best way to do this? Thanks so much.
You can lately process a select query: SELECT show_1_price - travel_costs AS pricediff FROM my_table; and then grab value in php and again do an insert query...
Should be simple to do on the PHP side of things how about
query=sprintf("INSERT INTO table VALUES(%d, %d, %d)", travel_costs,
show_1_price, show_1_price - travel_cost);
Generally though it is bad form to store a value in a database that can be calculated from other values. The reason being that you may never ever access this value again yet you are using storage for it. CPU cycles are much more abundant today so calculate the value when need. This is not a golden rule though - there are times when it could be more efficient to store the calculated value - although this is not usually the case.
I have a table with columns ID(int), Number(decimal), and Date(int only timestamp). There are millions of rows. There are indexes on ID and Date.
On many of my pages I am querying this four or five times for a list of Numbers in a specified date range (the range being different each query).
Like:
select number,date where date < 111111111 and date >111111100000
I'm querying these sets of data to be placed on several different charts. "Today vs Yesterday", "This Month vs Last Month", "This Year vs Last Year".
Would querying the largest possible result set with the sql statement and then using my programming language to filter down the query via a sorted and spliced array be better than waiting for each of these 0.3 second queries to finish?
Is there something else that can be done to speed this up?
It depends on the result set and the executing speed of your queries. There is no ultimate answer to this question.
You should benchmark and calculate the results if you really need to speed up things.
But keep in mind that premature optimization should be avoided besides that you'll implement an already implemented logic in your code which can contain bugs, etc. etc.
While it may cause the query to perform quicker you have to ask yourself about the potential impacts to memory if you were to attempt to load in the entire range of records and then aggregating it programatically.
Chances are that the MySQL optimatizations based on index will perform better than anything you could come up with anyway so it sounds like a bad idea.