1

The queries on HomeController file slows down the site. It takes 20 seconds for the page to load fully. (Page size is 3.9 Mb only, and CPU load goes up to 80% every time when i load the page). I am told to use Query Builder which is faster than Elequant and join queries to send them as one query. I find this too difficult. Where can i see some examples for this ?

HomeController

public function index()
{
    $sliders = Post::where('post_type','slider')
        ->with('FeaturedImage','PostField')
        ->orderBy('created_at', 'desc')
        ->limit(4)
        ->get();

    $page1 = Post::where([
        ['post_type','=','custom_page'],
        ['slug','=','page1'],
    ])
        ->with('FeaturedImage','PostField')
        ->latest()
        ->first();


    $page2 = Post::where([
        ['post_type','=','custom_page'],
        ['slug','=','page2'],
    ])
        ->with('FeaturedImage','PostField')
        ->latest()
        ->first();


    $page3 = Post::where([
        ['post_type','=','custom_page'],
        ['slug','=','page-3'],
    ])
        ->with('FeaturedImage','PostField')
        ->latest()
        ->first();


    $compacts = array(
        'sliders',
        'page1',
        'page2',
        'page3',
    );
    return view('site.home')->with(compact($compacts));
}

edit: Post Migration

public function up()
{
    // Create table for storing roles
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('author_id');
        $table->integer('category_id')->nullable();
        $table->string('title');
        $table->text('excerpt')->nullable();
        $table->text('body')->nullable();
        $table->string('slug')->nullable();//unique()
        $table->string('post_type')->default('post');
        $table->enum('status', ['PUBLISHED', 'DRAFT', 'PENDING'])->default('DRAFT');
        $table->timestamps();
    });
}
7
  • Where can i see some examples for this ? have you tried google? the laravel documentation? Commented Nov 2, 2017 at 6:16
  • you are basically executing a query 3 time for no reason Commented Nov 2, 2017 at 6:18
  • How join this queries as one query? Commented Nov 2, 2017 at 6:25
  • Can you show your migration for your posts table? Commented Nov 2, 2017 at 6:55
  • @patricus yes, i updated Commented Nov 2, 2017 at 7:03

2 Answers 2

2

You're running four queries against the posts table, and none of them are using indexes. This means four full table scans of the posts table. Additionally, you're sorting on an non-indexed field. This can cause performance issues as well.

You need an index on post_type and on slug for your query conditions. You can either create two individual indexes, or one composite index. It depends on your application needs.

$table->string('slug')->nullable()->index();
$table->string('post_type')->default('post')->index();
Sign up to request clarification or add additional context in comments.

Comments

1

You may want to do something like this to get all pages, and then split the pages using the collection groupBy() function

Post::where('post_type','=','custom_page')
->whereIn('slug',['page1','page2','page-3'])
->whereRaw('id IN (select MAX(id) FROM post GROUP BY slug)')
->with('FeaturedImage','PostField')
->get()
->groupBy('slug');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.