Laravel trying to query multiple tables

Not really sure how to fix this, and if it is even a problem with my query or database, so is here. There are 3 tables, products, relationships and tags.

'products' (
    'ID' bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    'user_id' bigint(20) unsigned NOT NULL,
    'name' varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'primary_image' bigint(20) DEFAULT NULL,
    'description' longtext COLLATE utf8mb4_unicode_ci,
    'price' float DEFAULT NULL,
    'sale_price' float DEFAULT NULL,
    'currency' varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'primary_color' varchar(7) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'secondary_color' varchar(7) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'status' varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'quantity' bigint(20) DEFAULT NULL,
    'origin' varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'type' varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'size' varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'processing_time' varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    'date_added' int(11) DEFAULT NULL,
    PRIMARY KEY ('ID')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

'relations' (
    'ID' bigint(20) unsigned NOT NULL,
    'relation_id' bigint(20) unsigned NOT NULL,
    'type' varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    'options' text COLLATE utf8mb4_unicode_ci,
    KEY 'ID' ('ID'),
    KEY 'relation_id' ('relation_id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

'tags' (
    'ID' bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    'name' varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
    'slug' varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
    PRIMARY KEY ('ID')
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

      

The relationship between these categories is as follows:

  • products.ID = relations.ID
  • relations.relation_id = tags.ID

For testing purposes, 50 thousand products, 250 thousand relationships and 25 thousand tags are used.

I am creating a query that searches inside the product name, description and tags.

Executing this request:

Product::select('ID', 'name')->where( 'name', 'like', '%'.$search_query.'%' )->orWhere( 'description', 'like', '%'.$search_query.'%' )->orWhereHas('relations', function( $query ) use( $search_query ) {
    $query->where('type', 'tags')->whereHas('tags', function( $query ) use( $search_query ) {
        $query->where( 'name', 'like', '%'.$search_query.'%' );
    });
})->paginate(25);

      

  • This request takes about 0.8s to find the data, if I ask for a specific tag, it takes as long as 1.8s
  • I built a similar query with joins and it took even longer so I was left with the above query.

Do any of you have an idea what I might be doing wrong? The main problem here is query execution time.

+3


source to share


1 answer


There is no way to optimize this query that I know. If you have 'LIKE', searchquery.'%'

(no leading%) you can index columns of text for better speed. If you want this functionality with a wildcard on both ends, you will have to use a full text search provider. Algolia https://www.algolia.com/ is the one I know and Laravel Scout is built to work with finding them. This other question also links to http://sphinxsearch.com/ and http://lucene.apache.org/core/ , although I don't know what they do.



Edit: also https://www.elastic.co/products/elasticsearch

0


source







All Articles