I apologize if this question has been asked already, I'm sure it has I just was not able to find a similar question/answer that solved my problem.
I am pulling data from PostgreSQL and I have an SQL statement similar to:
$SQL = "SELECT * FROM my_view_table WHERE id='".$my_id."'";
From this I run my query. The request pulls from a view in PostgreSQL which looks through more tables. That is correct. I'm not sure how to limit the number of rows to display. I receive about 1,000 rows of information with about 20 columns of data so it takes an incredible amount of time to query.
Is there a way to ask for rows 0 - 100, then 101 - 200, etc, so I can pull 100 at a time to display? I know I'll have to use a little code to keep track of the count, but I just need some SQL help with querying "x to y" rows.
Thank you for the help with this issue. (If there is another very similar question that has already been answered a link to that would be a sufficient answer!)
I've posted the answer to my question down below.
I found the answer to this question from gnarly's suggestion of LIMIT on SQL
$sql = "SELECT * FROM my_table LIMIT X OFFSET Y";
Where LIMIT only gives the X number of rows you want, and OFFSET gives the Y starting point. So showing rows 0 through 30:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 0";
And showing rows 31 through 60:
$sql = "SELECT * FROM my_table LIMIT 30 OFFSET 30";
Related
This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed last month.
I have a database with 3 million records. I want it to randomly fetch 10 records. I read somewhere that you can randomly select records from a range of 1000 records so that it does not search the entire database.
I am currently using the following code which is very heavy.
$result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM miasta");
$offset_row = mysqli_fetch_object($result);
$offset = $offset_row->offset;
$result = mysqli_query($conn, "select * from (SELECT * FROM miasta) as podseld ORDER BY RAND() LIMIT $offset, 10");
$row = mysqli_fetch_array($result)
Please help me to speed up this query. Thank you in advance.
Randomly selected records take a long time to upload.
With updated statistics, you should be pretty quick at getting the total row count. So, avoid an ORDER BY.
Try:
SELECT
*
FROM miasta
WHERE RAND() <=
10 / (SELECT COUNT(*) FROM miasta)
LIMIT 10
As we use RAND(), here, you might get a higher number than 10 rows, so LIMIT 10. And, sometimes, you might get fewer than 10 rows (it's random), but then just re-run it until you have 10 rows.
I would like to know the syntax of how to construct a sql query that retrieves sets of data from the database. For example, I am able to retrieve a set of 10 records from a table , but then would like to retrieve the next ten after that based on the same query.
PHP
$limit = 10;
$SQl = "SELECT * FROM myTable LIMIT {$limit}";
What would the sql query be for the next 10 records, given that I already have the previous 10 records and would not want them to also be retrieved?
There are two options:
LIMIT 11, 10
and one more:
LIMIT 10 OFFSET 11
Legend:
11 - start row
10 - how much rows
This question already has answers here:
MySQL Great Circle Distance (Haversine formula)
(9 answers)
Closed 6 years ago.
I'm trying to get points around "me" by location and it's pretty simple :
select * from X where ((SQRT(POW(69.1 * (a.lat - 49.201441 ), 2) +
POW(69.1 * (16.161299 - a.lng) * COS(a.lat / 57.3), 2))) <='1') LIMIT 5
*
my location = 49.201441;16.161299
distance = 1 (mile)
This query returns me 5 points around "me" at a distance of 1 mile. This is rly fast, but just when my table have a ... I don't know, maybe 5000 rows.
I'm using this query on 200 000 rows table, and it's very slow! Maybe .. 2-7 sec and even if the limit is 1 = no difference.
Can someone explain it to me and help me? Thanks a lot guys!
BTW: If my query does not contains this part of query = it takes maybe 0.0008s... so second part of query is correct.
I have used this for finding people near you.
table name: user_location
id|username|latitude|longitude
i am not sure that the distances is for a mile, but what this does is takes the users lat minus each persons from the database. If the users lat and long are within 1 it will be put into the results.
The biggest problem is 1 isn't equal to 1 mile. it is closer to 10miles i believe (it was a while ago and might not be 10 miles i can't remember), but you can always change this. Another problem is that you basically always have to update people's locations. Which isn't that bad, but if you have a slow server with a lot of users it can slow you down.
It is fast and works... Not the best solution, but might work for you. For me it atleast showed a nice surrounding area.
$query = "SELECT * FROM `user_location` WHERE (".$user_lat." - latitude) <= 1 AND (".$user_lon." - longitude) <= 1 LIMIT 100";
I tested this with 10,000 lines and it was just under 3 seconds with 254 people near me. (randomly generated content)
I have 2 tables - info and comments.
info has 270,000 rows, while comments has only 100 rows.
I run a php script that selects the data from the database and encodes it to json format.
The PHP script also limits the response to only first 10 rows, and both PHP files Info.php and Comments.php are exactly the same, except the names.
So why is it, that the table with 270,000 rows takes way more time to load than the one with 100 rows when it only prints the first 10 rows?
comments takes 1 seconds while info takes 10 seconds.
This is the PHP query code, pretty simple :
Info.php: $query = "SELECT * FROM info ORDER BY id LIMIT 10;";
Comments.php: $query = "SELECT * FROM comments ORDER BY id LIMIT 10;";
As for testing purposes, they both have the same columns and same data, the only difference is the rows number. So I tested times with PHP and:
Info.php:
select from database time: 0.6090 seconds
time taken to decode JSON: 6.4736 seconds
while Comments.php results:
select from database time: 0.7309 seconds
time taken to decode JSON: 1.7178 seconds
Thanks
It might be because of MySQL select clause execution order. MySQL engine has to sort all the rows in the table first by id column, and after that limit results to 10 rows. Take a look at this answer.
I am being stumped over this problem, and I can't seem to figure it out, hoping someone here can do make heads or tails over this.
When I use the following to query my database:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%')";
the items show up they way they are supposed to. But when I add limit 10, 5 to the end:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%') limit 10, 5";
The query shows up nothing. I have tried everything I can think of, but I must have missed something. Someone help?
Thanks
By using LIMIT 10, 5 you're stating that you want the database to start displaying from the 11th row, and display 5 rows. (11th, 12th, 13th, 14th, 15th) - It might be possible that you actually, don't have enough rows.
Try something like, LIMIT 0, 5 - this will display the first 5 rows from the beginning, but 10, 5 will only work if you have more than 10 rows for the search query.
Read more here: http://www.mysqltutorial.org/mysql-limit.aspx
The Limit clause arguments are offset and number of items respectively.
So the query you write would show 5 rows starting from the 10th row.
How many rows does your original query show. If it is less then 10 rows then the limit query you provide wont work as the number of rows are less.