Skip to content

Commit

Permalink
query optimization & adds indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Aug 20, 2024
1 parent b586aa8 commit a869c94
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/Models/ShortUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,15 @@ public function scopeExpiringInDays($query, $days)

public function scopeHasTracing($query, $search)
{
$query->whereHas('tracing')
->whereIn('short_urls.id', function ($subQuery) use ($search) {
$subQuery->from('short_url_tracings')
->where(function ($subWhereQuery) use ($search) {
$subWhereQuery->where('short_url_tracings.utm_source', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_medium', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_campaign', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_content', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_term', 'like', '%'.$search.'%');
})
->select('short_url_tracings.short_url_id');
});
$query->join('short_url_tracings', 'short_urls.id', '=', 'short_url_tracings.short_url_id')
->where(function ($subWhereQuery) use ($search) {
$subWhereQuery->where('short_url_tracings.utm_source', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_medium', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_campaign', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_content', 'like', '%'.$search.'%')
->orWhere('short_url_tracings.utm_term', 'like', '%'.$search.'%');
})
->whereNull('short_url_tracings.deleted_at'); // Handle soft deletes if necessary

return $query;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('short_url_clicks', function (Blueprint $table) {
$table->index('short_url_id');
$table->index('location_id');
$table->index('outcome_id');

$table->index(['short_url_id', 'outcome_id'], 'clicks_url_outcome_index');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('short_url_tracings', function (Blueprint $table) {
$table->index(['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'], 'utm_composite_index');
});
}
};

0 comments on commit a869c94

Please sign in to comment.