We are using a custom module to generate category/subcategory wise sales report. For this we are using following query.
$sql = "SELECT sfoi.created_at, sfoi.name, sfoi.sku, sfoi.price, sfoi.qty_ordered FROM sales_flat_order_item as sfoi
left join sales_flat_order as sfo on sfo.entity_id=sfoi.order_id
$category
WHERE sfoi.created_at between '$fromDate' and '$toDate' $store_id $categoryWhere
order by sfoi.created_at";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$reportArr = $connection->fetchAll($sql);
Using date range selector, users select dates and sales report generated between two selected dates. We have provided option to export to csv. When a large amount of order comes, system got stuck. I understand it's because query selecting lot of orders between two dates(say orders in last 5 months). What would be best way to export to csv for such a case in magento ?