mongodb - PHP PDO Search backwards in MySQL without ORDER -
i have big ass mysql table, , need recent 1000 rows match condition. obvious way doing
try { $stmt = $dbh->prepare("select id, date, field1, field2 big_ass_table field1 >= 1000 , field2 <=4550 order date desc limit 0,1000"); $stmt->execute(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { print_r($row); } $stmt = null; } catch (pdoexception $e) { print $e->getmessage(); } but table 15m rows , temporary sort date taking forever. casting unix timestamp integer doesn't either.
i've tried using mongodb task , building index reverse order on date trick without sorting:
$cursor=$mongodb->big_ass_table ->find(array('$and'=>array($conditions))) ->hint( "date_-1" ) ->limit(1000); there number of reasons keep using mysql task (although i'm growing more , more fond of mongodb) i'm hoping there's way mysql, specially pdo mysql, search backwards.
according mysql documentation:
an index_col_name specification can end asc or desc. these keywords permitted future extensions specifying ascending or descending index value storage. currently, parsed ignored; index values stored in ascending order.
so i'm @ deadend here. resorted pdo. possible make work like
try { $stmt = $dbh->prepare("select id, date, field1, field2 big_ass_table field1 >= 1000 , field2 <=4550 limit 0,1000", array(pdo::attr_cursor => pdo::cursor_scroll)); $stmt->execute(); $row = $stmt->fetch(pdo::fetch_assoc, pdo::fetch_ori_last); { print_r($row); } while ($row = $stmt->fetch(pdo::fetch_assoc, pdo::fetch_ori_prior)); $stmt = null; } catch (pdoexception $e) { print $e->getmessage(); } note i've removed order clause last one. believe way i'll still capturing first 1000 rows , printing them backwards, isn't need do. ¿perhaps if remove limit clause , manually close cursor when reach 1000 rows work? ¿or overloading db engine?
there @ least 2 tricks speed query.
- to add condition uses index yet reduces number of rows sort. say, limit date guaranteed out range.
- to create reverse index - unix timestamp, negated one,
-in front. way have index data in reverse order. don't forget make field of signed int type.
Comments
Post a Comment