laravel在基本查询时出现了“Allowed memory size of 134217728 bytes exhausted”的错误。

8 浏览
0 Comments

laravel在基本查询时出现了“Allowed memory size of 134217728 bytes exhausted”的错误。

我不知道为什么下面的代码无法工作:

DB::table('twitter_hashtags')->paginate(5);

每次都会出现以下错误(第二个数字往往不同):

Allowed memory size of 134217728 bytes exhausted (tried to allocate 95735352 bytes)

我尝试使用以下代码(来自18776710),但没有任何区别:

DB::connection()->disableQueryLog();

删除->paginate(5)也没有任何区别。

当我尝试:

DB::select('SELECT * FROM twitter_hashtags');

可以正常工作,但是我无法使用内置的分页选项。

有人有建议吗?

目前,twitter_hashtags表中有5500条记录。保存了id、tweet_id和hashtag,所以表太大不应该是问题。

表的大小:

Data    384,0   KB
Index   464,0   KB
Total   848,0   KB

更新

根据要求提供更多信息

这是动作:

public function getHashtags()
{           
    DB::connection()->disableQueryLog(); // 使用或不使用都没有区别
    $retweets = DB::table('twitter_hashtags')->paginate(10);        
    // 显示页面
    return View::make('twitter/retweets', compact('retweets'));
}

如您所见,我使用了retweets的视图,问题也存在于retweets或我尝试从中获取数据的任何其他表中。

视图:

我用来创建表的迁移:

public function up()
{
    Schema::create('twitter_hashtags', function($table)
    {
        // 基本运行信息
        $table->increments('id');
        $table->string('status_id')->index();
        $table->string('hashtag')->index();
        // 杂项
        $table->timestamps();
    });
}

当我将内存限制增加到256M时,以下是响应的前100行左右:

Illuminate\Database\Query\Builder Object
(
    [connection:protected] => Illuminate\Database\MySqlConnection Object
        (
            [pdo:protected] => PDO Object
                (
                )
            [queryGrammar:protected] => Illuminate\Database\Query\Grammars\MySqlGrammar Object
                (
                    [wrapper:protected] => `%s`
                    [selectComponents:protected] => Array
                        (
                            [0] => aggregate
                            [1] => columns
                            [2] => from
                            [3] => joins
                            [4] => wheres
                            [5] => groups
                            [6] => havings
                            [7] => orders
                            [8] => limit
                            [9] => offset
                            [10] => unions
                        )
                    [tablePrefix:protected] => 
                )
            [schemaGrammar:protected] => 
            [postProcessor:protected] => Illuminate\Database\Query\Processors\Processor Object
                (
                )
            [events:protected] => Illuminate\Events\Dispatcher Object
                (
                    [container:protected] => Illuminate\Foundation\Application Object
                        (
                            [booted:protected] => 1
                            [bootingCallbacks:protected] => Array
                                (
                                    [0] => Closure Object
                                        (
                                            [parameter] => Array
                                                (
                                                    [$app] => 
                                                )
                                        )
                                    [1] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Log\LogServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )
                                                )
                                        )
                                    [2] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Mail\MailServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )
                                                )
                                        )
                                    [3] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Queue\QueueServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )
                                                )
                                        )
                                    [4] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [instance] => Illuminate\Translation\TranslationServiceProvider Object
                                                        (
                                                            [defer:protected] => 1
                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                        )
                                                )
                                        )

更新2

根据要求,这是响应:

Array
(
    [total] => 5689
    [per_page] => 5
    [current_page] => 1
    [last_page] => 1138
    [from] => 1
    [to] => 5
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [status_id] => 384992474579484672
                    [hashtag] => Twenterand
                    [created_at] => 2013-10-01 11:00:02
                    [updated_at] => 2013-10-01 11:00:02
                )
            [1] => stdClass Object
                (
                    [id] => 2
                    [status_id] => 384992323190280192
                    [hashtag] => Twenterand
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )
            [2] => stdClass Object
                (
                    [id] => 3
                    [status_id] => 384989174014545921
                    [hashtag] => PVDA
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )
            [3] => stdClass Object
                (
                    [id] => 4
                    [status_id] => 384988499188801536
                    [hashtag] => GR2014
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )
            [4] => stdClass Object
                (
                    [id] => 5
                    [status_id] => 384986184092356608
                    [hashtag] => GR2014
                    [created_at] => 2013-10-01 11:00:03
                    [updated_at] => 2013-10-01 11:00:03
                )
        )
)
                                )
                                        )

更新3

这是我用于getStatuses的代码:

public function getStatuses()
{           
    // 获取所有分页的statuses
    $statuses = DB::table('twitter_statuses')
            ->select('status_id', 'text', 'user_screen_name','datetime','place')
            ->orderBy('datetime', 'DESC')
            ->paginate(10);
    // 显示页面
    return View::make('twitter/statuses', compact('statuses'));
}

以及完整的视图文件:

@extends('layouts/default')
{{-- 页面标题 --}}
@section('title')
Twitter Statuses ::
@parent
@stop
{{-- 页面内容 --}}
@section('content')

Twitter Statuses

Datum Gebruiker Tweet Locatie
{{ $status->datetime; }} user_screen_name; ?> text; ?> place; ?>
links(); ?> @stop

0
0 Comments

问题出现的原因是由于内存不足导致的,解决方法是在查询的最后添加->get()。但是对于某些情况下,这个解决方法可能不适用,比如使用Eloquent查询时出现的内存不足问题。无法确定具体原因,因为代码没有改动,但是在过去的五个月里一直没有问题。即使内存限制远远超过尝试分配的内存量,测试时加载一个50MB的CSV文件并将内存限制设置为49MB时也会出现错误,将内存限制提高到51MB时就不会出错,但是使用Eloquent查询时仍然会出现问题。

0