Further perf changes and more denormalization

This commit is contained in:
nelsonlaquet 2013-08-21 15:45:33 -05:00
parent e06e7da234
commit 22818261ee
13 changed files with 56 additions and 12 deletions

View file

@ -28,7 +28,7 @@
} }
public function getShow($id) { public function getShow($id) {
$album = Album::with(['tracks' => function($query) { $query->details(); }, 'user', 'comments' => function($query) { $query->with('user'); }])->details()->find($id); $album = Album::with(['tracks' => function($query) { $query->details(); }, 'tracks.cover', 'tracks.genre', 'tracks.user', 'user', 'comments' => function($query) { $query->with('user'); }])->details()->find($id);
if (!$album) if (!$album)
App::abort(404); App::abort(404);
@ -48,10 +48,10 @@
$page = Input::get('page'); $page = Input::get('page');
$query = Album::summary() $query = Album::summary()
->with(['tracks' => function($query) { $query->details(); }, 'user']) ->with('user', 'user.avatar', 'cover')
->details() ->details()
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->whereRaw('(SELECT COUNT(id) FROM tracks WHERE tracks.album_id = albums.id) > 0'); ->where('track_count', '>', 0);
$count = $query->count(); $count = $query->count();
$perPage = 15; $perPage = 15;

View file

@ -68,9 +68,9 @@
} }
$query = Album::summary() $query = Album::summary()
->with('tracks', 'user') ->with('user')
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->whereRaw('(SELECT COUNT(id) FROM tracks WHERE tracks.album_id = albums.id) > 0') ->where('track_count', '>', 0)
->whereUserId($user->id); ->whereUserId($user->id);
$albums = []; $albums = [];
@ -125,7 +125,7 @@
$page = Input::get('page'); $page = Input::get('page');
$query = User::orderBy('created_at', 'desc') $query = User::orderBy('created_at', 'desc')
->whereRaw('(SELECT COUNT(id) FROM tracks WHERE tracks.user_id = users.id) > 0'); ->where('track_count', '>', 0);
$count = $query->count(); $count = $query->count();
$perPage = 15; $perPage = 15;

View file

@ -48,8 +48,8 @@
public function getPinned() { public function getPinned() {
$query = Playlist $query = Playlist
::with(['tracks.user', 'tracks' => function($query) {}, 'comments', 'comments.user']) ::details()
->details() ->with('tracks', 'tracks.cover', 'tracks.user', 'user')
->join('pinned_playlists', function($join) { ->join('pinned_playlists', function($join) {
$join->on('playlist_id', '=', 'playlists.id'); $join->on('playlist_id', '=', 'playlists.id');
}) })

View file

@ -18,6 +18,7 @@ class CreateUsersTable extends Migration {
$table->boolean('uses_gravatar')->default(true); $table->boolean('uses_gravatar')->default(true);
$table->boolean('can_see_explicit_content'); $table->boolean('can_see_explicit_content');
$table->text('bio'); $table->text('bio');
$table->integer('track_count')->unsigned();
$table->integer('comment_count')->unsigned(); $table->integer('comment_count')->unsigned();
$table->timestamps(); $table->timestamps();
}); });

View file

@ -12,6 +12,7 @@ class CreateAlbums extends Migration {
$table->text('description'); $table->text('description');
$table->integer('cover_id')->unsigned()->nullable(); $table->integer('cover_id')->unsigned()->nullable();
$table->integer('track_count')->unsigned();
$table->integer('view_count')->unsigned(); $table->integer('view_count')->unsigned();
$table->integer('download_count')->unsigned(); $table->integer('download_count')->unsigned();
$table->integer('favourite_count')->unsigned(); $table->integer('favourite_count')->unsigned();

View file

@ -12,6 +12,7 @@ class CreatePlaylists extends Migration {
$table->text('description'); $table->text('description');
$table->boolean('is_public'); $table->boolean('is_public');
$table->integer('track_count')->unsigned();
$table->integer('view_count')->unsigned(); $table->integer('view_count')->unsigned();
$table->integer('download_count')->unsigned(); $table->integer('download_count')->unsigned();
$table->integer('favourite_count')->unsigned(); $table->integer('favourite_count')->unsigned();

View file

@ -7,6 +7,7 @@
use Entities\Playlist; use Entities\Playlist;
use Entities\Track; use Entities\Track;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class AddTrackToPlaylistCommand extends CommandBase { class AddTrackToPlaylistCommand extends CommandBase {
private $_track; private $_track;
@ -32,6 +33,11 @@
public function execute() { public function execute() {
$songIndex = $this->_playlist->tracks()->count() + 1; $songIndex = $this->_playlist->tracks()->count() + 1;
$this->_playlist->tracks()->attach($this->_track, ['position' => $songIndex]); $this->_playlist->tracks()->attach($this->_track, ['position' => $songIndex]);
Playlist::whereId($this->_playlist->id)->update([
'track_count' => DB::raw('(SELECT COUNT(id) FROM playlist_track WHERE playlist_id = ' . $this->_playlist->id . ')')
]);
return CommandResponse::succeed(['message' => 'Track added!']); return CommandResponse::succeed(['message' => 'Track added!']);
} }
} }

View file

@ -6,6 +6,7 @@
use Entities\Image; use Entities\Image;
use Entities\Track; use Entities\Track;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
@ -61,6 +62,10 @@
$this->_album->syncTrackIds($trackIds); $this->_album->syncTrackIds($trackIds);
$this->_album->save(); $this->_album->save();
Album::whereId($this->_album->id)->update([
'track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE album_id = ' . $this->_album->id . ')')
]);
return CommandResponse::succeed(['real_cover_url' => $this->_album->getCoverUrl(Image::NORMAL)]); return CommandResponse::succeed(['real_cover_url' => $this->_album->getCoverUrl(Image::NORMAL)]);
} }
} }

View file

@ -5,8 +5,10 @@
use Entities\Album; use Entities\Album;
use Entities\Image; use Entities\Image;
use Entities\Track; use Entities\Track;
use Entities\User;
use External; use External;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class EditTrackCommand extends CommandBase { class EditTrackCommand extends CommandBase {
@ -76,6 +78,10 @@
$album = Album::find($this->_input['album_id']); $album = Album::find($this->_input['album_id']);
$track->track_number = $album->tracks()->count() + 1; $track->track_number = $album->tracks()->count() + 1;
$track->album_id = $this->_input['album_id']; $track->album_id = $this->_input['album_id'];
Album::whereId($album->id)->update([
'track_count' => DB::raw('SELECT COUNT(id) FROM tracks WHERE album_id = ' . $album->id)
]);
} }
} else { } else {
if ($track->album_id != null) { if ($track->album_id != null) {
@ -107,6 +113,10 @@
$track->updateTags(); $track->updateTags();
$track->save(); $track->save();
User::whereId($this->_track->user_id)->update([
'track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE deleted_at IS NULL AND published_at IS NOT NULL AND user_id = ' . $this->_track->user_id . ')')
]);
return CommandResponse::succeed(['real_cover_url' => $track->getCoverUrl(Image::NORMAL)]); return CommandResponse::succeed(['real_cover_url' => $track->getCoverUrl(Image::NORMAL)]);
} }
@ -122,5 +132,9 @@
$track->updateTags(); $track->updateTags();
$track->save(); $track->save();
} }
Album::whereId($album->id)->update([
'track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE album_id = ' . $album->id . ')')
]);
} }
} }

View file

@ -106,7 +106,7 @@
return [ return [
'id' => $album->id, 'id' => $album->id,
'track_count' => $album->tracks()->count(), 'track_count' => $album->track_count,
'title' => $album->title, 'title' => $album->title,
'slug' => $album->slug, 'slug' => $album->slug,
'created_at' => $album->created_at, 'created_at' => $album->created_at,

View file

@ -79,7 +79,7 @@
return [ return [
'id' => $playlist->id, 'id' => $playlist->id,
'track_count' => $playlist->tracks()->count(), 'track_count' => $playlist->track_count,
'title' => $playlist->title, 'title' => $playlist->title,
'slug' => $playlist->slug, 'slug' => $playlist->slug,
'created_at' => $playlist->created_at, 'created_at' => $playlist->created_at,

View file

@ -41,7 +41,19 @@
} }
public function after($request, $response) { public function after($request, $response) {
$this->_data['queries'] = DB::getQueryLog(); $this->_data['queries'] = [];
foreach (DB::getQueryLog() as $query) {
if (starts_with($query['query'], 'select * from `cache` where'))
continue;
if (starts_with($query['query'], 'delete from `cache` where'))
continue;
if (starts_with($query['query'], 'insert into `cache`'))
continue;
$this->_data['queries'][] = $query;
}
} }
public function log($level, $message, $context) { public function log($level, $message, $context) {

View file

@ -36,11 +36,15 @@ appendRequest = (method, url, req) ->
$liItem.appendTo $logItems $liItem.appendTo $logItems
for query in req.request.queries for query in req.request.queries
queryText = query.query
for binding in query.bindings
queryText = queryText.replace '?', '"' + binding + '"'
$liItem = $("<li>") $liItem = $("<li>")
($("<span class='time' />").text query.time).appendTo $liItem ($("<span class='time' />").text query.time).appendTo $liItem
$("<h4 class='prettyprint' />") $("<h4 class='prettyprint' />")
.html(prettyPrintOne(query.query, 'lang-sql')) .html(prettyPrintOne(queryText, 'lang-sql'))
.click () -> .click () ->
$(this).toggleClass 'open' $(this).toggleClass 'open'
.appendTo($liItem) .appendTo($liItem)