I have large log file 300-500 megs and need to do searches in it. I am currently doing this using grep in php code, but I want to further optimize the code. Data I need is always within last 10000 lines in the file so I want the search to start from the end and move forward until pattern is found and stop.
I found some info that tac can do this but my performance tests show using tac is 4 times slower. Currently my search takes about 0.13 sec to complete with tac added on it takes 0.50. I feel like I am missing something, I would appreciate help.
My expectation is that if I get file read from the end properly this will increase the speed of the process considerably.
here is the code and it includes the timer
<?php
exec('grep -r "search var" file.log -b | head -1', $result);
echo $result[0];
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "<br>".$time;
?>
You can avoid the -r
flag with grep
. And you only want to search within the last 10000 lines right? So directly tail
it and grep
the pattern.
tail file.log -n 10000 | grep "search var"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.