0

有一个查询这两天一直在困扰我,它曾经工作得很好,但现在它减慢了整个集群环境,查询如下所示:

SELECT  userUploads.*,
        users_avatar.avatar AS avatar
FROM    userUploads
        LEFT JOIN users_avatar
            ON userUploads.udid = users_avatar.udid
        INNER JOIN user_subscription
            ON (
                    user_subscription.sub_1 = 'G:123456789'
                    AND user_subscription.sub_2 = userUploads.udid
                )
WHERE   userUploads.platform = 'Private'
        AND userUploads.STATUS IN  ( 'featured', 'approved' )
ORDER   BY userUploads.id DESC 
LIMIT 50 OFFSET 0

如果有人可以帮助解决此查询,我将不胜感激。

以下是查询的解释:

+----+-------------+-------------------+--------+----------------------+----------+---------+------------------------+------+-----------------------------+
| id | select_type | table             | type   | possible_keys        | key      | key_len | ref                    | rows | Extra                       |
+----+-------------+-------------------+--------+----------------------+----------+---------+------------------------+------+-----------------------------+
|  1 | SIMPLE      | userUploads       | range  | platform,udid,status | platform | 154     | NULL                   |   12 | Using where; Using filesort |
|  1 | SIMPLE      | users_avatar      | eq_ref | PRIMARY              | PRIMARY  | 182     | Seeds.userUploads.udid |    1 |                             |
|  1 | SIMPLE      | user_subscription | ref    | sub_1,sub_2          | sub_1    | 93      | const                  |    7 | Using where                 |
+----+-------------+-------------------+--------+----------------------+----------+---------+------------------------+------+-----------------------------+

提前致谢

编辑**显示创建表可以在下面看到

下面是表格的展示创建表格,希望您对 dancrumb 有任何想法。

| users_avatar | CREATE TABLE `users_avatar` (
`udid` varchar(60) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`avatar` varchar(448) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`udid`)
) ENGINE=ndbcluster DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

| userUploads | CREATE TABLE `userUploads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bdaha` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`user` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`direktoren` text COLLATE utf8_unicode_ci,
`filnamnet` varchar(180) COLLATE utf8_unicode_ci DEFAULT NULL,
`karhes` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
`version` char(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`rostat` int(10) DEFAULT NULL,
`stars` int(11) DEFAULT NULL,
`statyn` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`platform` char(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`images` int(2) DEFAULT NULL,
`date` char(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`udid` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`favorirepris` int(8) DEFAULT NULL,
`hikes` char(4) COLLATE utf8_unicode_ci DEFAULT 'no',
`dbn` char(6) COLLATE utf8_unicode_ci DEFAULT NULL,
`timestamp` char(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`comments` int(5) DEFAULT NULL,
`klistret` enum('no','yes') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
KEY `platform` (`platform`,`status`),
KEY `udid` (`udid`),
KEY `hikes` (`hikes`),
KEY `bdaha` (`bdaha`),
KEY `statyn` (`statyn`),
KEY `version` (`version`)
) ENGINE=ndbcluster AUTO_INCREMENT=118831 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |


| user_subscription | CREATE TABLE `user_subscription` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sub_1` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`sub_2` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `sub_1` (`sub_1`),
KEY `sub_2` (`sub_2`)
) ENGINE=ndbcluster AUTO_INCREMENT=155184 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
4

2 回答 2

0

好吧,您在 userUploads 上有一个文件排序,它总是很慢。您可能想使用索引来删除它。例如,您可能希望从 udid、平台和状态的索引开始。

于 2013-01-19T16:51:42.510 回答
0

通常,在查询时,您希望首先返回的数据执行最有限的操作,以便仅对实际在结果中的数据执行其他操作。

在这种情况下,尝试将内连接重新排序到 user_subscription,将左连接重新排序到 users_avatar。这样,如果用户实际上在结果集中,它只会尝试获取用户的头像,而不是首先查找所有头像,然后根据连接和 where 子句进行过滤。

SELECT  userUploads.*,
    users_avatar.avatar AS avatar
FROM    userUploads
INNER JOIN user_subscription
            ON (
                    user_subscription.sub_1 = 'G:123456789'
                    AND user_subscription.sub_2 = userUploads.udid
                )
LEFT JOIN users_avatar
            ON userUploads.udid = users_avatar.udid

WHERE   userUploads.platform = 'Private'
        AND userUploads.STATUS IN  ( 'featured', 'approved' )
ORDER   BY userUploads.id DESC 
LIMIT 50 OFFSET 0
于 2013-01-19T17:05:59.843 回答