0

Hi I have more than 500k records in items table Its takes more than 9 seconds to execute this query ineed to make it milliseconds to execute this query using mysql index

$products = \App\items::with([
    'item_store' => function($query) {
        $query->select('size', 'item_id', 'item_store_id');
    },

    'pics' => function($query) {
        $query->select('img_url', 'item_id');
    },

    'brand' => function($query) {
        $query->select('item_id', 'brand_id');
    },
    'brand.brand' => function($query) {
        $query->select('brand_id', 'brand_name');
    }

])
    ->select('item_id', 'short_name', 'price', 'price_above')
    ->orderBy('Price', 'Asc')->whereIn('category_id', $arr)
    ->groupBy('Sku')
    ->paginate(20);

my database structure is [st] https://screenshots.firefox.com/JAmaKENMYRhQkEjx/ourweds.com

this is item table migration

Schema::create('item', function (Blueprint $table) {
            $table->bigIncrements('item_id');
            $table->string('item_name');
            $table->integer('Sku');
            $table->text('Description');
            $table->text('short_description');
            $table->text('category_id');
            $table->string('color');
            $table->double('price');
            $table->double('indian_price');

           $table->string('old_price');

           $table->string('indian_old_price');


            $table->timestamps();


        });

item eloquent model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class items extends Model
{
    //

     protected $table = 'items';
      protected $primaryKey = 'item_id';

      protected $fillable = [
       'category_id',
       'item_name',
       'Sku',
       'Description',
       'short_description',
       'color',
       'kik_cash_percentage',
        'status',
          'price',
          'price_old',
          'indian_price',
          'short_name',
          'price_above',
          'short_name_alter',
          'availability'
    ];

    public function pics(){
  return $this->hasOne( 'App\item_pics', 'item_id' );
}


 public function item_store()
    {
        return $this->hasMany('App\item_store','item_id');
    }

     public function category()
    {
        return $this->belongsTo('App\categories','category_id');
    }

  public function brand()
    {
        return $this->hasOne('App\item_has_brand','item_id');
    } 

}
Arshak Ahamed
  • 73
  • 1
  • 8
  • Hi Arshak, can you explain what you want in a bit more detail? Can you also explain why you want it? Can you also explain what happens now because you done have what you want? Thank you in advance – Techno Oct 27 '19 at 19:05
  • Hi I have more than 500k records in items table Its takes more than 9 seconds to execute this query ineed to make it milliseconds to execute this query using mysql index – Arshak Ahamed Oct 27 '19 at 19:25
  • Allright, that sounds like a fair request. Could you share the 'create table' statements of all the involved tables, indices and relationships? It is hard to provide 'upgrades' when the original state is unknown :) – Techno Oct 27 '19 at 19:31
  • @RobBiermann i have added migration and eloquent model on above – Arshak Ahamed Oct 27 '19 at 19:46
  • Another question: What database engine do you use? MySql has multiple engines. Can you specify the right one? – Techno Oct 27 '19 at 20:28
  • I think you may need to take this question into account: https://stackoverflow.com/questions/4093788/how-do-i-know-when-to-index-a-column-and-with-what – Techno Oct 27 '19 at 20:29
  • Did you define your foreign keys? I see no definition of those. If not, than you can A: create foreign keys. Or B: Index the columns you use to modify your results(category_id, Price and Sku) Note that with option A you still need to index Price and Sku – Techno Oct 27 '19 at 20:32
  • When I use Groupby Sku its take 5 seconds late – Arshak Ahamed Oct 27 '19 at 20:34
  • By 'late' do you mean longer after you added the index? Or 5 seconds without the index? Please be more specific as it helps communication ;) – Techno Oct 27 '19 at 20:53

0 Answers0