Question

I'm trying to fix some issues with a script I didn't make where the site seems to be filling up the mysql /tmp dir very fast.

I checked the slow query log and it has a lot of queries like this..

SELECT
    COUNT(*) AS `total`
FROM
    (
        SELECT
                *
        FROM
            `Advertising_Business_and_Retail`
        WHERE
            MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                *
        FROM
            `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                *
        FROM `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    )
AS t;

Except it has I believe 45 unions in total (1 for each category table), these tables aren't huge, but the Advertising_Business_and_Retail table has about 450k listings by itself.

I ran this query manually and it took about 2 and a half minutes.

I'm guessing this is why the /tmp dir is filling up so fast?

Any tips on what I can do to solve the issue?

Was it helpful?

Solution

If you know for sure that there are no duplicates among the rows that you are selecting, try replacing UNION with UNION ALL.

Moreover, you could also replace UINON ALL with simple addition:

select (
  (select count(1) from `Advertising_Business_and_Retail` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
+ (select count(1) from `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
+ (select count(1) from `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company'))
) as total

OTHER TIPS

Is this any faster?

SELECT
    SUM(`count`) AS `total`
FROM
    (
        SELECT
               COUNT(*) AS `count`
        FROM
            `Advertising_Business_and_Retail`
        WHERE
            MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
               COUNT(*) AS `count`
        FROM
            `Chemical` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    UNION
        SELECT
                COUNT(*) AS `count`
        FROM `Clothing` WHERE MATCH (`wm`, `locn`, `gns`) AGAINST('outdoor apparel company')
    )
AS t;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top