Mysql COUNT(*) and myql_num_rows in php: Which is faster? [duplicate] - php

This question already has answers here:
SELECT COUNT() vs mysql_num_rows();
(3 answers)
Closed 7 years ago.
I wan to know the difference between getting row count via select COUNT(*) from table fetching data AND mysqli_num_rows($ofquery)(in php)?
I tried in both ways and works well. But method is faster?

Use COUNT, internally the server will process the request differently.
When doing COUNT, the server will only allocate memory to store the result of the count.
When using mysqli_num_rows, the server will process the entire result set, allocate memory for all those results, and put the server in fetching mode, which involves a lot of different details, such as locking.
Think of it like the following pseudo scenario:
1) Hey , how many people are in the class room? (count)
2) Hey , get me a list of all the people in the classroom, ... I'll calculate the number of people myself (mysqli_num_rows)
count is the best than mysqli_num_rows
Reference from here.

Related

Why have 2 values when i use query select in php? [duplicate]

This question already has answers here:
PHP PDO retrieves duplicate data
(2 answers)
Closed 1 year ago.
I'm newbie in PHP and when I use PHP to query select, I got 2 values, I realize it may be make slow if have 2 duplicated values.
So I want to ask about it and I also want to get just 1 values from select
thanks for reading my question.
here is picture:
It dipends on what your i guess sql query respondes to your query. However if you want cut down your result in sql you can use LIMIT 1
SELECT ...
FROM ...
WHERE ...
LIMIT 1

How do I use LIMIT and OFFSET properly in PDO MySql [duplicate]

This question already has answers here:
How do I calculate the offsets for pagination?
(4 answers)
How to apply bindValue method in LIMIT clause?
(11 answers)
Closed 5 years ago.
I asked a question yesterday titled "How do i use binded variables with MySql LIMIT clause. Sorry, I guess it was not clear enough and got 30 views but no suggestions. So here is what I found after a day of trial and error. First, I discovered that the the LIMIT is the actual physical position of a record in a table not my auto_incremented id_ number. A lot of documentation i read also indicated it was the OFFSET that held the record position, so I wasted a lot of time with weird results. Second, I found documentation with a couple of ways to bind a variable to LIMIT. None of them worked, so I resorted to assigning a $variable to the LIMIT as follows.
("SELECT * FROM $livetable WHERE cust_zip = :cust_zip
HAVING distance < :mydistance ORDER BY client_id ASC LIMIT $start_record_position
, $how_many_records_to_display");
This works for me but any better suggestions, would be appreciated. And I hope this will save someone else a day of figuring this out.

Can a pagination do with only one sql query? [duplicate]

This question already has answers here:
MySQL pagination without double-querying?
(9 answers)
Closed 7 years ago.
As I know a pagination structure requires minimum two sql query.
First, find total row
Second, limit your query.
Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?
You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.
However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.

How to get the nearest places from a Latitude/Longitude in MySQL table? [duplicate]

This question already has answers here:
SQL query, select nearest places by a given coordinates [duplicate]
(4 answers)
Closed 9 years ago.
I want to make a table of with columns in a MySQL database:
Index
Latitude
Longitude
Places like place of Countries, Cities, People, Building etc.
With huge number of rows, in order of hundred thousands until million of rows.
If I want to get nearest places of a selected row in the table, how can I do that in the fastest way?
It is no problem if more information, indexing, or presorting are necessary.
======
Edit 1:
I have read the answer and the answer is using a formula, for example from the best answer:
(((acos(sin((".$latitude."*pi()/180)) * sin((geo_latitude*pi()/180))+cos((".latitude."*pi()/180)) * cos((geo_latitude*pi()/180)) * cos(((".$longitude."- geo_longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344)
If I have 1 million rows, that means, there are 1 million of expensive calculation. I thing it will be very slow.
Are the optimization, for example using filtering in the beginning:
1. If the input is City A in location 10.000, 20.000, then filter cities that located at 9.000 to 11.00.
2. Calculate with the formula above.
How to optimize the speed of that algorithm?
====
Edit 2:
Sorry, I've only read the best answer.
I found what I've looked for in the other answer: http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
You can use a quadkey. A quadkey is a spatial index like a quadtree. It sort the points into a grid and then you can search the grid around the center point. It's not easy to understand but you can download my php class hilbert-curve # phpclasses.org. Or you can use the native MySQL spatial extension and the point datatype. However my implementation uses a quadkey and a hilbert-curve and can be better. It depends much on the data. The problem with the harvesine formula is that it is very slow. But you can use both algorithms together to achieve better results.

how to extend the maximum number of expression in a list of oracle? [duplicate]

This question already has answers here:
How to put more than 1000 values into an Oracle IN clause [duplicate]
(11 answers)
Closed 9 years ago.
I am using an IN clause, whereby, it needs to search the id IN these set of IDs that are not in order, meaning, it came from a different criteria., how to solve this ?
SELECT DISTINCT ID FROM "projeck"."mytable" "t" WHERE staffID IN (75953,196262,196387,133585,195639,196702,195790,195820,192903,145383,179603,175896,176554,43545,154843,183798,195767,195715,etc..etc.. etc..)
and i am getting this oracle error
General error: 1795 OCIStmtExecute: ORA-01795: maximum number of expressions in a list is 1000
My first choice would be to reference the function that generates these values directly.
If the values were being used for multiple queries and were expensive to calculate then I'd think about loading them into a global temporary table and joining to it.

Categories