Skip to content

Conversation

@cristijora
Copy link
Contributor

Search Performance Optimization config option

Overview

By default, when searching through BelongsTo relationship fields, Laravel Restify uses subqueries which can be slow for large datasets:

-- Default behavior (subqueries)
SELECT * FROM `invoices` WHERE (
    UPPER(`invoices`.`gross_amount`) LIKE '%CSC%'
    OR (SELECT `vendors`.`code` FROM `vendors` WHERE `vendors`.`id` = `invoices`.`vendor_id` LIMIT 1) LIKE '%csc%'
    OR (SELECT `vendors`.`name` FROM `vendors` WHERE `vendors`.`id` = `invoices`.`vendor_id` LIMIT 1) LIKE '%csc%'
)

With JOIN optimization enabled, these become efficient JOIN-based queries:

-- Optimized behavior (JOINs)
SELECT invoices.* FROM `invoices`
LEFT JOIN `vendors` AS vendors_for_vendor ON invoices.vendor_id = vendors_for_vendor.id
WHERE (
    UPPER(invoices.gross_amount) LIKE '%CSC%'
    OR UPPER(vendors_for_vendor.code) LIKE '%CSC%'
    OR UPPER(vendors_for_vendor.name) LIKE '%CSC%'
)

Configuration

Enabling JOIN Optimization

Add the following to your .env file:

RESTIFY_SEARCH_USE_JOINS=true

Or configure it directly in config/restify.php:

'search' => [
    'case_sensitive' => true,
    'use_joins' => true, // Enable JOIN optimization
],

Default Behavior

By default, JOIN optimization is disabled to maintain backward compatibility. The system will continue using subqueries until explicitly enabled.

@what-the-diff
Copy link

what-the-diff bot commented Jul 31, 2025

PR Summary

  • Configuration Update
    A newly added setting 'use_joins' in config/restify.php will enable a search optimization technique for related 'BelongsTo' fields. This improvement can speed up data search processes significantly.

  • Documentation Addition
    A document was created, docs/eager-loading-optimization.md, that explains the new search optimization and how to configure it. The document also covers the benefits of using this feature and how to test it.

  • Search Mechanism Improved
    The SearchableFilter class was modified to use our new optimization method for search queries. This change makes the system more efficient by replacing the previous method used for connected models.

  • RepositorySearchService Update
    The way RepositorySearchService handles data has been improved. It is now smarter and avoids processing repeated data when using the new optimization.

  • Utility Addition
    A new method getJoinedRelationships was added to RepositorySearchService to help track data relations. This function adds depth to the optimization by keeping tabs on the connections in the database.

  • Testing Update
    New tests tests/Unit/RelatedEagerLoadingTest.php were added to ensure the optimization feature is functioning correctly. They measure the reduction in database inquiries and verify effective data relation handling.


  • New Testing Class
    Another class SearchableFilterTest was added in the tests/Unit directory for rigorous testing.

  • Testing Environment Update
    The setup configuration for restify.search.use_joins, integral to the optimization feature, is reset during setUp and tearDown testing phases.

  • Testing Methods Added
    Several new methods were added to further test the new feature. These methods scrutinize different aspects of the optimization process, verifying that it defaults to the old method when necessary and accurately handles different types of data fields. The tests confirm that the new feature operates as expected and beneficially adjusts the database queries.

cristijora and others added 3 commits July 31, 2025 12:43
…optimizations

# Conflicts:
#	src/Filters/SearchableFilter.php
#	tests/Controllers/Index/EagerLoadingOptimizationTest.php
@cristijora cristijora closed this Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants