1

我正在尝试使用我没有制作的脚本解决一些问题,该脚本似乎/tmp很快就填满了 mysql 目录。

我检查了slow query log它,它有很多这样的查询..

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;

除了我相信45总共有工会(每个类别表 1 个)之外,这些表并不大,但该Advertising_Business_and_Retail表本身就有关于450k列表的信息。

我手动运行了这个查询,大约花了 2 分半钟。

我猜这就是为什么 /tmp 目录填得这么快的原因?

关于我可以做些什么来解决这个问题的任何提示?

4

2 回答 2

1

如果您确定您选择的行之间没有重复项,请尝试替换UNIONUNION ALL.

此外,您还可以UINON ALL用简单的添加替换:

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
于 2012-05-11T09:27:09.883 回答
1

这是不是更快?

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;
于 2012-05-11T09:27:45.610 回答