这对我来说感觉像是一个初学SQL问题,但是这里有。这就是我想要做的事情:
我已经在本文的底部描述了所涉及的表格,但这里是我正在执行的查询
SELECT p.*, GROUP_CONCAT(pt.name)
FROM products p
LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
GROUP BY p.id
ORDER BY p.created
LIMIT 30;
大约有280,000个产品,130个标签,524,000个链接记录,我已经分析了这些表格。问题是它需要花费80多秒才能运行(在合适的硬件上),这对我来说是错误的。
这是EXPLAIN的结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE p index NULL created 4 NULL 30 "Using temporary"
1 SIMPLE pt4p ref idx_product_tags_for_products idx_product_tags_for_products 3 s.id 1 "Using index"
1 SIMPLE pt eq_ref PRIMARY PRIMARY 4 pt4p.product_tag_id 1
我认为它正在以错误的顺序执行操作,即在连接后使用大型临时表对结果进行ORDER,然后使用LIMITing。我脑子里的查询计划会是这样的:
这听起来很简单,但似乎并没有这样 - 我错过了什么吗?
CREATE TABLE `products` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`rating` float NOT NULL,
`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `product_tags_for_products` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`product_id` mediumint(8) unsigned NOT NULL,
`product_tag_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_product_tags_for_products` (`product_id`,`product_tag_id`),
KEY `product_tag_id` (`product_tag_id`),
CONSTRAINT `product_tags_for_products_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),
CONSTRAINT `product_tags_for_products_ibfk_2` FOREIGN KEY (`product_tag_id`) REFERENCES `product_tags` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `product_tags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
更新了Salman A要求的分析信息:
Status,
Duration,CPU_user,CPU_system,Context_voluntary,Context_involuntary,Block_ops_in,Block_ops_out,Messages_sent,Messages_received,Page_faults_major,Page_faults_minor,Swaps,Source_function,Source_file,Source_line
starting,
0.000124,0.000106,0.000015,0,0,0,0,0,0,0,0,0,NULL,NULL,NULL
"Opening tables",
0.000022,0.000020,0.000003,0,0,0,0,0,0,0,0,0,"unknown function",sql_base.cc,4519
"System lock",
0.000007,0.000004,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",lock.cc,258
"Table lock",
0.000011,0.000009,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",lock.cc,269
init,
0.000055,0.000054,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,2524
optimizing,
0.000008,0.000006,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,833
statistics,
0.000116,0.000051,0.000066,0,0,0,0,0,0,0,1,0,"unknown function",sql_select.cc,1024
preparing,
0.000027,0.000023,0.000003,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,1046
"Creating tmp table",
0.000054,0.000053,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,1546
"Sorting for group",
0.000018,0.000015,0.000003,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,1596
executing,
0.000004,0.000002,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,1780
"Copying to tmp table",
0.061716,0.049455,0.013560,0,18,0,0,0,0,0,3680,0,"unknown function",sql_select.cc,1927
"converting HEAP to MyISAM",
0.046731,0.006371,0.017543,3,5,0,3,0,0,0,32,0,"unknown function",sql_select.cc,10980
"Copying to tmp table on disk",
10.700166,3.038211,1.191086,538,1230,1,31,0,0,0,65,0,"unknown function",sql_select.cc,11045
"Sorting result",
0.777887,0.155327,0.618896,2,137,0,1,0,0,0,634,0,"unknown function",sql_select.cc,2201
"Sending data",
0.000336,0.000159,0.000178,0,0,0,0,0,0,0,1,0,"unknown function",sql_select.cc,2334
end,
0.000005,0.000003,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,2570
"removing tmp table",
0.106382,0.000058,0.080105,4,9,0,11,0,0,0,0,0,"unknown function",sql_select.cc,10912
end,
0.000015,0.000007,0.000007,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,10937
"query end",
0.000004,0.000002,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_parse.cc,5083
"freeing items",
0.000012,0.000012,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_parse.cc,6107
"removing tmp table",
0.000010,0.000009,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_select.cc,10912
"freeing items",
0.000084,0.000022,0.000057,0,1,0,0,1,0,0,0,0,"unknown function",sql_select.cc,10937
"logging slow query",
0.000004,0.000001,0.000001,0,0,0,0,0,0,0,0,0,"unknown function",sql_parse.cc,1723
"logging slow query",
0.000049,0.000031,0.000018,0,0,0,0,0,0,0,0,0,"unknown function",sql_parse.cc,1733
"cleaning up",
0.000007,0.000005,0.000002,0,0,0,0,0,0,0,0,0,"unknown function",sql_parse.cc,1691
表格是:
产品= 84.1MiB(产品表中有额外字段,为清晰起见,我省略了这些字段) 标签= 32KiB 链接表= 46.6MiB
答案 0 :(得分:3)
我会尝试将产品数量限制为30 第一,然后只加入30种产品:
SELECT p.*, GROUP_CONCAT(pt.name) as tags
FROM (SELECT p30.* FROM products p30 ORDER BY p30.created LIMIT 30) p
LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
GROUP BY p.id
ORDER BY p.created
我知道你说没有子查询,但你没有解释原因,我也没有看到任何其他方法来解决你的问题。
请注意,您可以通过将其放在视图中来消除子选择:
CREATE VIEW v_last30products AS
SELECT p30.* FROM products p30 ORDER BY p30.created LIMIT 30;
然后将查询简化为:
SELECT p.*, GROUP_CONCAT(pt.name) as tags
FROM v_last30products p
LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
GROUP BY p.id
ORDER BY p.created
其他问题,您的n-to-n
表product_tags_for_products
没有意义,我会像这样重组它:
CREATE TABLE `product_tags_for_products` (
`product_id` mediumint(8) unsigned NOT NULL,
`product_tag_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`product_id`,`product_tag_id`),
CONSTRAINT `product_tags_for_products_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),
CONSTRAINT `product_tags_for_products_ibfk_2` FOREIGN KEY (`product_tag_id`) REFERENCES `product_tags` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
这应该通过以下方式加快查询速度:
- 缩短使用的密钥(在InnoDB上,PK总是包含在辅助密钥中);
- 允许您使用比使用辅助键更快的PK;
更多速度问题
如果您仅使用select *
所需的字段替换select p.title, p.rating, ... FROM
,这也会加快一些速度。
答案 1 :(得分:0)
啊 - 我发现GROUP BY上的所有键都不是BTREE,默认情况下PRIMARY键是哈希值。 它有助于在有订购索引时分组...否则它必须扫描......
我的意思是,如果您为p.id和p.created添加了基于BTREE的索引,我认为这将有很大帮助。在这种情况下,我认为引擎将避免必须扫描/排序所有这些键以执行分组和排序。
答案 2 :(得分:0)
关于对标签进行过滤(您在Johan's answer的评论中提到),如果是明显的
SELECT p.*, GROUP_CONCAT(pt.name) AS tags
FROM products p
JOIN product_tags_for_products pt4p2 ON (pt4p2.product_id = p.id)
JOIN product_tags pt2 ON (pt2.id = pt4p2.product_tag_id)
LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
WHERE pt2.name IN ('some', 'tags', 'here')
GROUP BY p.id
ORDER BY p.created LIMIT 30
跑得不够快,你总是可以试试这个:
CREATE TEMPORARY TABLE products30
SELECT p.*
FROM products p
JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
WHERE pt.name IN ('some', 'tags', 'here')
GROUP BY p.id
ORDER BY p.created LIMIT 30
SELECT p.*, GROUP_CONCAT(pt.name) AS tags
FROM products30 p
LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id)
LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id)
GROUP BY p.id
ORDER BY p.created
(我使用临时表,因为你说“没有子查询”;我不知道它们在Active Record框架中是否更容易使用,但至少它是另一种方法。)
聚苯乙烯。关于原始问题的一个非常隐蔽的想法:如果您将GROUP BY p.id
子句更改为GROUP BY p.created, p.id
,会有什么不同吗?可能不是,但我至少会尝试一下。