提取100,000行而不会在MySQL中停顿

时间:2013-09-01 14:03:24

标签: php mysql

我的数据库目前有超过200,000条记录,我有这个查询有3个连接,我担心这些连接选择行可能需要太长时间并停止我的查询。是否有推荐的方法在执行SELECT时选择一些预加载器等记录?我对任何事都持开放态度。

我的查询

-- Select all records
SELECT cust.firstname, cust.lastname, cust.phone1, cust.phone2, cust.mobile1, cust.mobile2, cust.regas, cust.regpol,
    cust.province, cust.city, cust.brgy, cust.unit, cust.vill, cust.condo, cust.servtype,
    cust.primary, cust.override_pst, dist.name, order.created, order.branch_id 'selectedbranch', order.status,
    order.delivery_date, order.date_acknowledged, order.date_dispatched, order.date_delivered, order.date_cancelled
    FROM sl_customers cust
    LEFT JOIN sl_orders `order` ON order.customer_id = cust.id
    LEFT JOIN sl_branches branch ON order.branch_id = branch.id
    LEFT JOIN sl_distributors dist ON branch.distributor_id = dist.id
    ORDER BY order.id DESC

并且,是的,用于JOIN的所有列都已编入索引。

1 个答案:

答案 0 :(得分:0)

您可以考虑将查询作为无缓冲查询。大型结果集可能占用大量内存,而在php中,大多数常见数据库扩展默认将结果缓冲到内存中以提高速度和易用性,但代价是内存消耗和响应延迟。对于大多数小的结果集来说,它是一个很好的默认值。

http://php.net/manual/en/mysqlinfo.concepts.buffering.php

如果当前的内存使用率水平可以接受,我会坚持使用缓冲查询。从程序员的角度来看,它们处理起来要好得多。