Laravel pagination slowing website down (plus how to cache raw SQL queries)

by lars on 28/11/2016

I know, I know. It clearly states that the paginator isn’t supposed to be used on large tables. It selects everything and works on that, so you will get out of memory errors/”Allowed memory size of X bytes exhausted” at some point. Write your own paginator.

Suppose you have this very complex Eloquent query that you want paginated. Suppose it contains a group by – You can’t count() one of those queries properly.

This is what I did to fix my problem. It’s a quick and dirty solution, that I might remedy later. It uses SQL_CALC_FOUND_ROWS and FOUND_ROWS().

...lots of Eloquent stuff...
$items->paginate(25);

Comment out the

$items->paginate(25);

and do this:

$page = Input::get("page", 1);
$page = intval($page);
$limit = 25;
$sql = str_replace("select", "select SQL_CALC_FOUND_ROWS", $items->toSql());
$sql .= sprintf(" limit %d,%d", $limit * ($page - 1), $limit);
$items = DB::select(DB::raw($sql), $items->getBindings());
$counter = DB::select("SELECT FOUND_ROWS() AS total_rows");
$items = Paginator::make($items, $counter[0]->total_rows, $limit);

Now, if you want to cache the query for 3 minutes replace:

$items = DB::select(DB::raw($sql), $items->getBindings());
$counter = DB::select("SELECT FOUND_ROWS() AS total_rows");

with:

$cacheKey = md5($sql . json_encode($recipes->getBindings()));
$items = Cache::remember("items_".$cacheKey, 3, function () use ($sql, $items) { return DB::select(DB::raw($sql), $items->getBindings()); });
$counter = Cache::remember("items_count_".$cacheKey, 3, function () use { return DB::select("SELECT FOUND_ROWS() AS total_rows"); });

I hope this saves you some time.

No Comments

Extremely slow Vagrant on Windows

by lars on 26/11/2016

Do you run a development environment on vagrant on Windows, and is it EXTREMELY slow? You might want to experiment with changing the virtualbox container away from a dynamically sized disk, or switch to NFS (look for the plugin vagrant-winnfsd) – Did you try all that, and is it still slow? Look for large log files in your synced folder. Eliminate them. I went from many many seconds response time, to near instant.

No Comments

Review: Vacation apartment in Dubrovnik, Croatia

by lars on 9/07/2015

view So, we got tired of the weather here in Denmark, and wanted some heat, beach and interesting experiences. We decided to go to Croatia for vacation. We chose the southern part of Croatia, specifically Dubrovnik, which has an interesting coast line and nice places. We looked at a lot of places to stay.
We’re a family of 4 – two kids 12 and 14 – it was important that there was lots of space, separate bedrooms and a nice, quiet place to base our adventures from.

We ended up choosing an two bedroom apartment just outside of Dubrovnik, in a town called Lozica. Even though it’s a different town, I would class it as being in Dubrovnik. It takes 5-10 minutes to drive to the center, which is perfect.

The place is called SunDeck Comfort Apartment, and the name fits! We could not have chosen a better place to stay. The photo above is the view from the apartment.

Read the rest of this article »

No Comments

Brev til Ekstrabladet omkring deres nikotinskræmmekampagne

by lars on 20/03/2015

Jeg skrev et lille brev til Ekstrabladet, som kommentar til deres artikler om deres skræmmekampagner omkring nikotinflasker.

Read the rest of this article »

No Comments

Brev til sundhedsministeren om e-cigaretter

by lars on 8/03/2015

Jeg har sendt følgende ind til sundhedsministeren. Om et par dage bør det være at finde på folketingets hjemmeside.

Read the rest of this article »

No Comments

Win a ProVari 2.5 Starter Kit on E-Liquid Recipes Forum

by lars on 8/03/2015

Right now you can win a ProVari 2.5 Starter Kit on the E-Liquid Recipes Forum.

All you have to do is submit an original photograph with the theme “Everyday Vaping”. The contest runs until March 15th 2015 at midnight GMT.

No Comments

Get $30 off a ProVari P3!

by lars on 8/02/2015

The ProVari P3 – one of the sturdiest, most reliable, high quality mods out there. A workhorse that will stay with you for years.

provape-provari-p3-v3

Yes, it’s only 20W, but it is perfect when you’re on the go with your lower wattage tank! Right now you can get $30 off, by heading to the E-Liquid Recipe Forum! The offer is valid until Feb. 12th 2015, so hurry on over!

No Comments

There is now an DIY e-liquid forum!

by lars on 7/02/2015

A few years ago I launched e-liquid-recipes.com – ever since I launched it, I’ve wanted to add a forum to accompany it. But I was too busy with work and with ELR, and I didn’t feel the forum software was good enough.

All the major forums out there, have a small DIY-section – and that’s great.

The time has come for the DIY-community to have their own DIY-centric forum, where you can not only discuss DIY but other stuff as well, but all the time with like-minded DIY-people! :-)

ELR now has a forum! You can access it by clicking the “Forum”-link at the top, or go to forum.e-liquid-recipes.com – It’s based on an truly excellent “next-generation” piece of forum-software called Discourse.
It will be a little confusing at first, when you’re used to the old-school, somewhat cumbersome, forums – This just feels …new!

All users on ELR have a user account on the forum – though you should probably see if you want to change your username on the forum.

Now, it’s just been laucnhed, so it is not very active right now – Don’t let that stop you! Leave a few posts or comment on existing ones, and in no time more will join in :-)

In the coming weeks there will be some competitions and give-aways as well! They’re already in the pipeline!

No Comments

Cloupor Mini mini-review

by lars on 21/01/2015

So I thought I’d make a small review of the Cloupor Mini.

The build quality is great. It’s a little bit bigger than the iStick. I got mine in black, so it’s a nice thick coat of paint over the aluminium body. The battery cover is held in place by two fairly strong magnets, and I haven’t experienced any rattling or looseness from that. I really like the build quality. The screen, which looks great, is pretty flush with the device. The buttons are nice and don’t rattle.

Read the rest of this article »

No Comments

Cheap replacement electric toothbrush heads

by lars on 20/02/2014

I recently bought an Oral-B Triumph 5500 electrical toothbrush – Replacement brush heads are really expensive. For a TENTH of the price you can get exact copies that doesn’t say Oral-B on them from my favorite Chinese webshop FastTech :) Click here to go to the oral care category!

No Comments