3) If you have enough memory, it should be possible to do this. "Enough memory" would not necessarily mean 1GB free before your script starts, since other things needs resources as well. And especially if you expect to handle your data with built in php functions such as explode. You will end up with two copies of your data (string + array), thus doubling memory requirements. Depending on algorithms used by you or those of the built in functions you use, considerably more than this might be needed as well.
But, if you're not using a production machine, you can always try and see what happens. Worst case scenario is that your script runs out of memory.
If you're running your script via a web server, max execution time has to be altered to allow for long duration as well, and I'd rather recommend you run this from CLI.
But there are various ways to deal with reading the data in chunks.
file_get_contents lets you specify max number of characters to read, which you could set at something more reasonable than half a gig, e.g. 5MB.
But, unless you have fixed size entries, you can't know if you end up with whole records, and as such you'd have to perform a string search to find these. Something along the lines of
$nextPart = '';
while ($str = read next 5 mb of data):
if ($nextPart)
$str = $nextPart . $str;
$nextPart = substr of $str, from last occurence of ^L
$str = substr of $str, to last occurence of ^L
parse each record and put it into DB. (see 1)
endWhile
Another way would be to use fgets to read one line at the time from the file. Parse each line as you go. When you reach every second L, you have a record. Insert to DB and continue. No problems parsing in this way.
1.
If you go with the "read large chunks of data" you will need to parse it as well.
$str = 'chunk of data, starting and ending with ^L, with 2 ^L between each record'.
$str = trim($str, chr(12)); # strip form feed from start and end of string
$records = explode(chr(12).chr(12), $str);
foreach ($records as $r) {
# $r is a string where each line is a field value pair from the current record.
}
2.
First off, I'm assuming that when you wrote
RecordA
FieldA1 ValueA1
FieldA2 ValueA2
RecordB
FieldB1 ValueB1
FieldB2 ValueB2
"FieldA1" and "FieldB1" would actually be the same identical text, such that the file actually look like (example)
^L
name john
age 20
location new york
^L
^L
name jane
age 25
location paris
^L
Assuming you put file1 into table t1, file2 into table t2, and that the field names are col1, col2 and col3
To get all records appearing in file 1, but not in file 2
SELECT t1.col1 AS t1c1, t1.col2 AS t1c2, t1.col3 AS t1c3,
t2.col1 AS t2c1, t2.col2 AS t2c2, t2.col3 AS t2c3
FROM t1
LEFT JOIN t2 ON t1c1 = t2c1 AND t1c2=t2c2 AND t1c3=t3c3
WHERE t2c1 IS NULL OR t2c2 IS NULL OR t2c3 IS NULL
And to get all records appearing in file1 but not in file 2, just replace the columns in the where clause and use FROM t2 LEFT JOIN t1