There is no guaranteed sorting order unless you specify one. Even if it seems like they have some sort of order, there's no guarantee that will not change in the next version of MySQL, or the next time you add a row, or delete a row, or change a row, or change an index, or MySQL decides to repack your data.
You must specify a sort order. You also must do your work in a transaction otherwise some other process could add or delete rows while you're running your queries. I chose the primary key because it's likely to already be in order and already be indexed.
BEGIN;
select * from tlogs ORDER BY id LIMIT 1,100000
...
COMMIT;
I would point out that getting your rows a few at a time is a waste. The ORDER BY
and LIMIT
s will be expensive. Instead, you should just query all 1,000,000 rows and fetch them one at a time. In Perl this would be...
my $sth = $dbh->prepare("SELECT * FROM tlogs LIMIT 1000000");
$sth->execute;
while( my $row = $sth->fetch ) {
... do something with the $row ...
}
This avoids the need for a transaction, ordering and limits. The query should execute almost immediately. The major cost now is your database API's cost for fetching.
All this just gets you the first 1,000,000 rows MySQL decides to give you. That doesn't seem very useful. It will probably be the last 1,000,000 rows inserted, but that's not guaranteed. There's also no guarantee that the last 1,000,000 rows inserted is the newest 1,000,000 log entries.
If you want the newest 1,000,000 log entries you need an ORDER BY
and I hope that field is indexed.
SELECT * FROM tlogs ORDER BY date desc LIMIT 1000000
Finally, you should see if SELECT INTO OUTFILE
can be used to export your data. It will likely be the most efficient.