in php to get last line and line count from big file with low memory footprint, we can do like this:
$file = new SplFileObject('/path/to/file.txt');
$lastLine = $file->seek($file->getSize()); //get last line data
$linesCount = $file->key(); //count how many lines
For some reason I can only store this text data in sql. Is there any way to read only our preferred line and get the total count without loading complete data to memory first? my 2GB LONGTEXT have millions line of text.
My Answer:
Yes, you can achieve this by using SQL queries with LIMIT and COUNT functions. Here is an example of how you can read a specific line and get the total count of lines in a 2GB LONGTEXT column in SQL:
To read a specific line (for example, the last line):
sql
SELECT column_name
FROM table_name
ORDER BY id DESC
LIMIT 1;
To get the total count of lines:
sql
SELECT COUNT(*)
FROM table_name;
By using these SQL queries, you can read specific lines and get the total count without loading the complete data into memory first.
Rate this post
4 of 5 based on 9998 votesComments