PHP Connection Oracle Export millions data

Source: Internet
Author: User

1, we generally do the idea of exporting is, according to the data we want, all the query out, and then export, this is a large amount of data will be very slow, here I put forward the idea is paging and buffering to achieve dynamic output .

2. Ordinary I will not say, below I say the paging and memory refresh ideas. The code is as follows:

$conn = Oci_connect (' Fin_data ', ' Fin_data ', "(Description= (address= (PROTOCOL =tcp) (host=192.168.6.65) (PORT = 1521)) ( Connect_data = (SID=HQYGDW)));
$sqlName = "SELECT *
From User_tab_columns
where Table_name= ' S_sale_receivable_detail '
ORDER BY column_name ";

$filedName = Oci_parse ($conn, $sqlName);
Oci_execute ($filedName, Oci_default); Number of rows Oci_default means no automatic commit

Header (' CONTENT-TYPE:APPLICATION/VND.MS-EXCEL;CHARSET=GBK ');
Header (' Content-disposition:attachment;filename=test_ '. Time (). '. csv ');
Header (' cache-control:max-age=0 ');

$fp = fopen (' php://output ', ' a ');
$head = Array ();
$i = 0;
while ($row = Oci_fetch_array ($filedName, OCI_ASSOC + oci_return_nulls)) {
$head [$i] = iconv (' utf-8 ', ' GBK ', $row [' column_name ']);
$i + +;
}
Fputcsv ($fp, $head);

$selectCount = "SELECT count (*) as counts from S_sale_receivable_detail T";
$resultCount = Oci_parse ($conn, $selectCount); Configuring SQL statements, executing SQL
Oci_execute ($resultCount, Oci_default); Number of rows Oci_default means no automatic commit
$count = Oci_fetch_array ($resultCount, Oci_assoc + oci_return_nulls);

$count [' COUNTS ']=10;
$pageNum = 50000;
$pagePre = Ceil ($count [' COUNTS ']/$pageNum);
for ($i = 1; $i <= $pagePre; $i + +) {
$pageStart = ($i-1) * $pageNum + $pageNum;
$pageEnd = ($i-1) * $PAGENUM;

$select = "SELECT *
From (SELECT a.*, ROWNUM RN
From (SELECT * from S_sale_receivable_detail) A
WHERE ROWNUM <={$pageStart})
WHERE RN >= {$pageEnd} ";

$result = Oci_parse ($conn, $select); Configuring SQL statements, executing SQL
Oci_execute ($result, Oci_default); Number of rows Oci_default means no automatic commit
$count = 0;
while ($rowResult = Oci_fetch_array ($result, OCI_ASSOC + oci_return_nulls)) {
Print_r (Explode (', ', $rowResult)); exit;
Fputcsv ($fp, $rowResult);
$count + +;
if ($count >= $pageNum) {
Ob_flush ();
flush ();
}
}
}

Fputcsv ($fp, $head);

PHP Connection Oracle Export millions data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.