当前位置: 首页 > 知识库问答 >
问题:

laravel在基本查询上“允许的内存大小为134217728字节已用尽”

马和硕
2023-03-14

我不知道为什么我不能让下面的工作:

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

每次我得到(第二个数字往往不同)

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

我尝试使用as in(18776710),但没有任何区别

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

删除-

当我尝试:

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

它工作正常,但是我不能在分页选项中使用构建。

有人有什么建议吗?

表twitter_hashtags目前有5500条记录。一个id,tweet_id和主题标签都保存了,所以不可能是表太大的问题。

表的大小:

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

使现代化

应要求提供更多信息

这就是行动

public function getHashtags()
{           
    DB::connection()->disableQueryLog(); // With or without does not make a difference
    $retweets = DB::table('twitter_hashtags')->paginate(10);        

    // Show the page
    return View::make('twitter/retweets', compact('retweets'));

}

如你所见,我使用了转发的视图,问题也出现在转发中,或者几乎任何其他表,我试图从这种方式获取数据。

“观点”

我用来创建表的迁移

public function up()
{
    Schema::create('twitter_hashtags', function($table)
    {
        // Basic run information
        $table->increments('id');
        $table->string('status_id')->index();
        $table->string('hashtag')->index();

        // Misc.
        $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

这里是我用于GetStatus的代码

public function getStatuses()
{           
    // Get all the paginated statuses
    $statuses = DB::table('twitter_statuses')
            ->select('status_id', 'text', 'user_screen_name','datetime','place')
            ->orderBy('datetime', 'DESC')
            ->paginate(10);

    // Show the page
    return View::make('twitter/statuses', compact('statuses'));
}

和完整的视图文件

@extends('layouts/default')

{{-- Page title --}}
@section('title')
Twitter Statuses ::
@parent
@stop

{{-- Page content --}}
@section('content')
<h1>Twitter Statuses</h1>
<div class="container">
    <table class="table">
        <tr>
            <th>Datum</th>
            <th>Gebruiker</th>
            <th>Tweet</th>
            <th>Locatie</th>
        </tr>
    <?php foreach ($statuses as $status): ?>
        <tr>
            <td>{{ $status->datetime; }}</td>
            <td><?php echo $status->user_screen_name; ?></td>
            <td><?php echo $status->text; ?></td>
            <td><?php echo $status->place; ?></td>
        </tr>
    <?php endforeach; ?>
    </table>
    <?php echo $statuses->links(); ?>
</div>
@stop

共有1个答案

袁玮
2023-03-14

我也有同样的问题。解决方案很简单:只需添加-

public function getStatuses()
{           
    // Get all the paginated statuses
    $statuses = DB::table('twitter_statuses')
            ->select('status_id', 'text', 'user_screen_name','datetime','place')
            ->orderBy('datetime', 'DESC')
            ->paginate(10)
            ->get() 
;

    // Show the page
    return View::make('twitter/statuses', compact('statuses'));
}

 类似资料: