V
V
viktorross2020-01-31 08:49:32
MySQL
viktorross, 2020-01-31 08:49:32

Long mysql query, what to do?

hello, could you tell me how this query should look in its normal form in order to work quickly?
Now it is 6 seconds, if you remove class_ads.* at the very beginning, then 1.5 seconds, but then only a few columns will be displayed, how to optimize everything

select class_ads.*, class_ads.id as adid, date_format(class_ads.`date_added`,'%d.%m РІ %H:%i') as date_nice, date_format(class_ads.`date_expires`,'%d.%m РІ %H:%i') as date_expires_nice, UNIX_TIMESTAMP(class_ads.`date_added`) as `time_added`, (class_ads.date_expires < '2020-01-31 08:13:36' and class_ads.date_expires!='0000-00-00 00:00:00' and class_ads.active=0) as expired
    , class_slugs.slug 
    
    from class_ads 
       inner join class_categories_lang on class_categories_lang.id = class_ads.category_id  
    inner join class_slugs on class_ads.id=class_slugs.object_id
      where class_ads.active=1  and class_slugs.`type`='listing'  order by  `priority` desc,  date_added  desc  limit 0, 36;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ettychel, 2020-01-31
@viktorross

The solution was suggested in the comments.

inner join class_categories_lang on class_categories_lang.id = class_ads.category_id

Is this join really needed?
Maybe where is enough?
from class_categories_lang l, class_ads a
where l.id = a.category_id

well, respectively, in the select, specify the columns that are needed

D
Dmitry, 2020-01-31
@thewind

Without a database structure - Fortune telling on the coffee grounds. We need table schemas, indexes, constraints.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question