M
M
Mokhirjon Naimov2016-06-16 21:25:29
MySQL
Mokhirjon Naimov, 2016-06-16 21:25:29

Is it possible to optimize this query to the database?

There is a directory of sites (sites are checked before being added to the directory, that is, with the status `rejected = 0` and `accepted = 1`), the sites have tags. So I want to display all tags with relationships with sites with the status `accepted` (randomly 50 pcs.).
For tagging, I use the `cartalyst/tags: v2.1` package, and for convenience, I created a `Tag` model and set up relationships (since the package itself does not provide a model for working with tags directly)...
The request itself looks like this:

$tags = Tag::whereHas('catalogue', function ($query) {
        $query->where('status', 1);
    })
    ->orderBy(DB::raw('RAND()'))
    ->take(50)
    ->get();

But now `response time` has increased to ~9 seconds, before it was ~1.5 seconds (this is why my VM is so slow, ~0.4 ms in production).
How to optimize the above code?
I would like to cache for a maximum of 5 minutes, but 9 seconds for every 5 minutes is too much...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-06-16
@zvermafia

Slows down due to order by rand().
How many entries? Maybe it's easier to pull everything out and cache them? And then using PHP, just choose random ones.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question