Retrieve data in chunks in postgresql - database

I want to fetch the data in chunks (using a select query) like in first attempt from 1 to 50 records and in second attempt from 51 to 100 records.

Use LIMIT and OFFSET. The following query returns 50 records after skipping the first 50, so records 51 - 150 are returned.
SELECT fname, lname
FROM students
ORDER BY ssn
LIMIT 100 OFFSET 50;
https://www.postgresql.org/docs/current/static/queries-limit.html

Related

How to speed up LIMIT & OFFSET query in a big database

I have a table which can contain up to billions rows
CREATE TABLE "Log4DataUsb" (
"Time" integer primary key not null ,
"Microseconds" integer ,
"Current" integer ,
"Voltage" integer )
Usually a user will want to query the data within a specific range, for example Time <= 123456789 and Time >= 0, because this may return billions rows, I want to segment the rows and only return a batch each time, like LIMIT 10,000, LIMITE 10,000 OFFSET X until it reaches the end of this time-range query.
I notice that when the number of rows goes up, this query can be quite slow, executing the queries below will take seconds even though I just want to move to the next batch.
SELECT * FROM TABLE WHERE Time <= 123456789 and Time >= 0 LIMIT 10,000
SELECT * FROM TABLE WHERE Time <= 123456789 and Time >= 0 LIMIT 10,000 OFFSET 10,0000
If the database is supposed to have 2 billion rows in total, is there any way it can largely increase the query performance?

How to get last 25 item of an array?

{1,2,3,4,5,6,7,8,9,10}
Let's say i need to get last 3 items in this array.
{8,9,10}
How can I do that with high performance approach when the array has millions of items?
SELECT *
FROM
unnest((select arraycolumn from table where id=1))
WITH ORDINALITY as t(a1, num)
ORDER BY t.num DESC
LIMIT 25;
This takes 8 seconds to get last 25 items from an array that has 16 million items. I think there should be a much faster way than that.
Try a slice:
select arraycolumn[array_upper(arraycolumn,1)-24 : array_upper(arraycolumn,1)]
from my_table
where id = 1

Total length off all characters in all columns of each row

I'm new to SQL Server so I apologize if my question seems too easy. I tried finding and answer on my own, but I'm failing miserably. I am trying to create a query which will return total size on the drive of each row in the table.
i thought about using dbcc showconting but it doesn't work for varchar(max) which appears in my table. Also, it doesn't return size for each row, but rather the average, max and min size. My reading so far suggests that it is not possible to get query that could show the size of each individual row in the table so I decided to settle for the total length of all characters in each column in each row. Indirectly it will give me idea about the size of each row.
I have a table with some varchar(500) and varchar(max) columns. I noticed that some of the rows are a lot bigger than others.
What I need is top 1000 longest rows in the table, preferably in an output showing two columns:
Column 1 showing EntryID
Column 2 showing total length of the characters in all columns together for that record (eg total length of the characters in the column 1 + total length of the characters in the column 2 + column3 + column4 etc...) It would be great if this could be aliased RowLength.
What I tried so far is:
SELECT TOP 1000
(LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)) as RowLength,
FROM dbo.tablename
ORDER BY Length Desc
It works, but it doesn't show entry ID corresponding to the total length of all characters in the row. How do I add it?
It also doesn't show the alias for the column showing number of characters in the row.
Could you please suggest how I can change the query to get the expected outcome? I'll be very grateful for any suggestions.
it doesn't show EntryID corresponding to the total length of all
characters in the row. It also doesn't show the alias for the column
showing number of characters in the row.
You have not specified an alias, so what should it show? You also haven't selected EntryID. If you want the longest 1000 rows you have to order by the length:
SELECT TOP 1000
EntryID,
Length = LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)
FROM dbo.tablename
ORDER BY Length DESC
SELECT TOP 1000 EntryID,
(LEN(columnname1) + LEN(columnname2) + LEN(columnname3) + LEN(columnname4)) AS RowLength,
FROM dbo.tablename
ORDER BY EntryID

MSSQL_How to show number's range by a loop?

I am using MSSQL to run a query, however I want to simply my current steps by using a loop.
-- My current script
select product, price,price_range
from
(select
product,
price,
case
when price <= 100 then 'upto100'
when price between 101 and 200 then '101-200'
when price between 201 and 300 then '201-300'
when price between 301 and 400 then '301-400'
when price between 401 and 500 then '401-500'
when price between 501 and 600 then '501-600'
when price between 601 and 700 then '601-700'
when price between 701 and 800 then '701-800'
when price >= 801 then '800+EURO'
end as price_range
from DATA) as A
Now my script works, it returns me correct result as I wanted:
product price price_range
shoes 50 upto100
clothes 456 401-500
computer 1500 800+EURO
BUT, can I make it simple? Can I somehow use loops instead of 'case...when...then'? Then if segments of price increase, I don't have to write lots of 'case..'.
I tried to use 'declare' and 'while' but didn't get it worked. How to set variable for loops in this case?
You can't get much simpler than that. The only thing neater would be to create a table with the price ranges and to join against it instead of hard-coding it in the query. You should do that if this query is used in multiple places, or rather often.
Something like:
CREATE TABLE price_ranges (
lowest INT,
highest INT,
name VARCHAR
)
INSERT INTO price_ranges VALUES (
(NULL, 100, 'upto100'),
(101, 201, '101-200),
...
(801, NULL, '801+EURO')
)
SELECT DATA.product, DATA.price, RANGES.name AS price_range
FROM DATA,
price_ranges RANGES
WHERE (DATA.price >= RANGES.lowest OR RANGES.lowest IS NULL)
AND (DATA.price <= RANGES.highest OR RANGES.highest IS NULL)
AS A

How to limit rows in SELECT with TRANSBASE

I'm working with huge records amount in one table and i need to SELECT them in 50000 pages. Is it possible somehow to limit these pages so the query would be faster and row count will be limited and offsetted?
I'm using ODBC with php like this:
$odbc_query = "SELECT * FROM " . $table_name;
$data = odbc_exec($this->odbc_id, $odbc_query);
while($row = odbc_fetch_array($data))
{
The limit is possible by FIRST(limit):
SELECT * FROM tablename FIRST(1000);
u can use limit keyword to limit the number of records...
select * from tablename limit 1000;
this will give u first 1000 rows..
now next time u want next thousand for this u have to keep track of ur last position.
so the query becomes...
select * from tablename limit 1000 offset by lastposition;

Resources