Home > OS >  how do I make this query run faster?
how do I make this query run faster?

Time:11-11

I have this SQL query running on a PHP website. This is an old site and the query was build by previous developer few years ago. But now as the site data is increased to around 230mb, this query has become pretty slow to execute. Take around 15-20 seconds. Is there any way I can make this run faster?

SELECT DISTINCT
    NULL AS bannerID,
    C1.url AS url,
    LOWER(C1.Organization) AS company_name,
    CONCAT(
        'https://mywebsite.co.uk/logos/',
        C1.userlogo
    ) AS logo_url
FROM
    Company AS C1
INNER JOIN Vacancy AS V1 ON LOWER(V1.company_name) = LOWER(C1.Organization)
WHERE
    V1.LiveDate <= CURDATE() 
AND url = '' 
AND V1.ClosingDate >= CURDATE()
AND C1.flag_show_logo = 1

CodePudding user response:

As commented, your query is suffering from being non-sargable due to the use of lower function.

Additionally I suspect you can remove the distinct by using exists instead of joining your tables

select null as bannerID,
    C1.url as url,
    Lower(C1.Organization) as company_name,
    Concat('https://mywebsite.co.uk/logos/', C1.userlogo) as logo_url
from Company c
where c.flag_show_logo = 1
  and c.url = '' 
and exists (
    select * from Vacancy v 
    where v.LiveDate <= CURDATE() 
    and v.ClosingDate >= CURDATE()
    and v.company_name = c.Organization
)
  • Related