[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

When finding ('all') a large amount of data with CakePHP, I ran out of memory [I tried...edit]

This is Hase from the development team.

The other day,
when I tried to use find('all') to retrieve nearly 100,000 pieces of data using CakePHP, I ran out of memory and
got the following error. .

Allowed memory size of 134217728 bytes exhausted

So, I researched various ways to save memory and be able to retrieve nearly 100,000 rows of data.

current code

$data = $this->Model->find('all'); // Some processing ......

Use query()

It seems that retrieving with query() is easier to process than retrieving with find('all').
So, I modified it to use query() to retrieve what was being retrieved using find.

$data = $this->Model->query("SELECT * FROM hogehoge;"); // Some processing ......

result

Emotional processing became easierI felt likebut,
Same as when I retrieved it using find('all'), there was not enough memory and an error occurred.

Disable cache for query()

When I looked at the official version, it seems that query() has a mechanism to cache queries by default.
To disable caching this query,
specify false as the second argument like query($query, $cachequeries = false).
So, I tried specifying false as the second argument of query().

$data = $this->Model->query("SELECT * FROM hogehoge;", $cachequeries = false); // Some processing ......

result

In conclusion, there was no particular change. .

Use a loop to divide and retrieve data

In the first place, with the above method, 100,000 pieces of data were acquired at once, so
it was natural that the memory would run out.
So I tried using limit and offset to divide the data and obtain it using loop processing.

// Get the number of data items $count = $this->Model->find('count'); // Get 5000 pieces of data at a time $limit = 5000; // Number of times to loop ({data number of items ÷ number of items retrieved per time} rounded up) $loop = ceil($count / $limit); for ($i = 0; $i < $loop; $i++){ // Offset $offset = $limit * $i; $data = $this->Model->query("select * from hogehoge as limit {$limit} offset {$offset};", $cachequeries = false); // Some processing ... ...... }

result

It was a great result.
There were no memory errors and the data was properly retrieved, so there is no problem! !

So, if there is someone who is having trouble with the same phenomenon, I would appreciate it if you could use this as a reference.

That's it.

However, with this method...
Read more.
When I found ('all') a large amount of data with CakePHP, I ran out of memory [Solved] | Beyond Co., Ltd.

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
7,400
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Tatsuya Hase

Joined Beyond Co., Ltd. as a new graduate.

We develop web systems (development of services and systems that run on browsers, such as web services, digital content, and business management systems) and game APIs (development of programs that communicate with application games).

We also develop private/custom apps for Shopify.

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
His hobbies are baseball/karaoke/anime